Programming Names and Naming Conventions

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In programming, what is a 'name' primarily used for?

  • To identify an entity within a program. (correct)
  • To allocate memory space for data.
  • To define the scope of a variable.
  • To specify the data type of a variable.

Which naming convention capitalizes all words in a multiple-word name, except for the first word?

  • Pascal case
  • Snake case
  • Camel case (correct)
  • Upper case

Which of the following statements is true regarding naming conventions in programming languages?

  • C++ specifies strict length limits for identifiers that all compilers must adhere to.
  • PHP variable names must begin with an ampersand (&) sign.
  • Java and C# have no length limit for names, with all characters being significant. (correct)
  • All languages enforce the same length limit for names.

What role do special characters like $, @, or % play at the beginning of a variable name in Perl?

<p>They specify the data type of the variable. (C)</p> Signup and view all the answers

In Python, how should words be separated to improve readability in function names, according to common naming conventions?

<p>By using underscores (snake case). (B)</p> Signup and view all the answers

What is the main characteristic of 'Pascal case' as a naming convention?

<p>Each word starts with a capital letter. (B)</p> Signup and view all the answers

What is the term for words that have predefined meanings in a programming language and cannot be used as variable names?

<p>Reserved words (D)</p> Signup and view all the answers

Which of the following represents the correct analogy for a variable in programming?

<p>An abstraction of a memory location that can store a value. (D)</p> Signup and view all the answers

Which attribute of a variable determines the permissible range of values and the set of operations that can be performed on it?

<p>Variable type (C)</p> Signup and view all the answers

In the context of variables, what does 'lifetime' refer to?

<p>The period during which a variable exists and retains its value in memory. (A)</p> Signup and view all the answers

What is the term for the association of an attribute to an entity in programming?

<p>Binding (B)</p> Signup and view all the answers

What is the significance of 'binding time' in programming?

<p>It specifies when an association between an attribute and an entity takes place. (B)</p> Signup and view all the answers

What is an 'explicit declaration' in programming?

<p>A statement that lists variable names and specifies their types. (A)</p> Signup and view all the answers

What characterizes 'dynamic type binding'?

<p>The type of a variable is determined and can change during runtime. (C)</p> Signup and view all the answers

Which concept defines the region of a program where a variable is accessible and can be used?

<p>Variable scope (D)</p> Signup and view all the answers

What is meant by 'global scope'?

<p>Variables are accessible from anywhere in the program. (B)</p> Signup and view all the answers

According to the LEGB rule in Python, what does 'E' stand for?

<p>Enclosing (B)</p> Signup and view all the answers

If a variable in a local scope has the same name as a variable in an outer scope, what phenomenon occurs?

<p>The local variable shadows the outer variable. (C)</p> Signup and view all the answers

What keyword is used in Python to modify a global variable from within a function?

<p>global (D)</p> Signup and view all the answers

Which data type is designed to represent numbers with a non-empty decimal fraction?

<p>Float (D)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

Name (in programming)

A string of characters used to identify an entity in a program.

Camel Case

Words in a multi-word name are capitalized except the first word.

Reserved Word

A special word that cannot be used as a name in a programming language.

Variable (in programming)

An abstraction of a computer memory cell, characterized by name, address, type, value, lifetime, and scope.

Signup and view all the flashcards

Variable Address

The unique machine memory address associated with a variable.

Signup and view all the flashcards

Variable Type

Determines the range of values a variable can store and the set of operations defined for that type.

Signup and view all the flashcards

Variable Value

The contents of the memory cell associated with the variable.

Signup and view all the flashcards

Variable Lifetime

Amount of time a variable exists and retains value.

Signup and view all the flashcards

Variable Scope

The part of the program where a variable can be accessed.

Signup and view all the flashcards

Binding (in programming)

Association between an attribute and an entity.

Signup and view all the flashcards

Binding Time

Time when a binding takes place.

Signup and view all the flashcards

Explicit Declaration

A statement that lists variable names and specifies their particular type.

Signup and view all the flashcards

Implicit Declaration

Associating variables with types through default conventions.

Signup and view all the flashcards

Dynamic type binding

Variable type is not specified by a declaration and is bound to a type when assigned a value.

Signup and view all the flashcards

Scope of a Variable

Range of statements wherein the variable is visible.

Signup and view all the flashcards

Global Scope

Variables accessible from anywhere in the program.

Signup and view all the flashcards

Local Scope

Variables inside a function or block of code.

Signup and view all the flashcards

Enclosing Scope

Scope of a function that encloses another function.

Signup and view all the flashcards

Built-in Scope

Contains all the built-in functions and variables.

Signup and view all the flashcards

LEGB Rule

Local, Enclosing, Global, Built-in - order of variable lookup

Signup and view all the flashcards

Study Notes

Names

  • A name identifies an entity in a program, composed of a letter followed by letters, digits, and underscores.
  • Identifiers can be used instead of names
  • The use of underscores in names was common in the 1970s-1980s.
  • Camel case is now more common in C-based languages, capitalizing each word in a multi-word name except the first.

Naming Conventions

  • Java and C# names can be any length.
  • C++ has implementation-specific length limits.
  • PHP variable names must start with a $.
  • Perl uses special characters ($, @, %) to specify variable types.
  • Ruby uses @ or @@ to indicate instance or class variables.
  • C-based languages are case-sensitive.

Python Naming Styles

  • Function: lowercase words separated by underscores (snake case), e.g., function, pyt_function.
  • Variable: lowercase word(s) separated by underscores, e.g., z, var, pyt_variable.
  • Class: capitalized words without underscores (Pascal case), e.g., Model, PythonClass.
  • Method: lowercase words separated by underscores (snake case), e.g., class_method, method.
  • Constant: uppercase words separated by underscores, e.g., CONSTANT, PYTH_CONSTANT.
  • Module: short, lowercase words separated by underscores, e.g., module.py, python_module.py.
  • Package: short, lowercase words without underscores, e.g., package, pythonpackage.
  • Pascal case involves capitalizing the first letter of each word.

Special Words

  • Special words increase readability by naming actions, separate syntax, and are known as reserved words or keywords.
  • Reserved words cannot be redefined by programmers in most languages
  • COBOL has 300 reserved words
  • Programming environments will assign different colors to reserved words.
  • Python's reserved words include False, None, True, and, del, elif, etc.

Variables

  • A variable is an abstraction of a memory location, characterized by name, address, type, value, lifetime, and scope.
  • Variable Name: Identifies a value that can change, using letters, numbers, and underscores.
  • Variable Address: A unique memory address for accessing and manipulating data.
  • Variable Type: Determines the range of values and operations, such as int, float, or Boolean.
  • Variable Value: The content of the memory cell, also known as its r-value.
  • Variable Lifetime: The duration a variable exists in memory.
  • Variable Scope: The accessible part of the program, determined by where it’s declared.

Bindings

  • Binding associates an attribute with an entity.
  • Binding time varies such as at language design, implementation, compile, load, link, or run time.
  • The * symbol is often bound to multiplication at language design time.
  • int is bound to value ranges at language implementation, and variables are bound to data types at compile time.
  • count = count - 3 example: type of count (compile time), possible values of count (compiler design time), meaning of the operator (compile time), internal representation of the literal 3 (compile time), value of count (execution time).

Type Bindings

  • Variables must be bound to a data type before reference, specified through explicit or implicit declaration.
  • Explicit declaration: Defines the variable's type directly in a statement.
  • C examples: int number;, float decimal;, char letter;.
  • Implicit declaration: Associates variables with types through conventions without explicit statements.

Dynamic Typing

  • Python uses dynamic typing
  • Dynamic typing means declaring variable types isn't necessary.
  • Explicit declaration comes from binding a variable to a value, which shows how a variable can change type.

Python Binding Examples

  • A variable x is assigned an integer, a string, a float, and a list, showing how its type changes.
  • Python binds a value each time a value is is assigned to a variable
  • With implicit declaration, demonstrated by simply assigning a value, Python automatically assigns a type based on the value it holds.

Scope

  • Scope is the range of statements where a variable is visible in a program.
  • Scope refers to the region of a program where a particular variable or function is accessible and valid.
  • Global Scope: Variables are accessible anywhere if defined outside functions or blocks.
  • Local Scope: Variables are accessible only within the function/block where they are declared.
  • Enclosing Scope: In nested functions, the outer function's variables are available to the inner function.
  • Built-in Scope: Contains built-in functions and variables, such as print(), len(), and int().
  • LEGB Rule: Python looks up variables in order: Local, Enclosing, Global, Built-in.
  • Shadowing: A local variable with the same name as an outer variable shadows the outer one.
  • Global Keyword: Used to modify global variables inside a function in Python.

Data Types

  • Modern computers handle integers and floating points
  • Integers: whole numbers
  • Floating Points: Numbers containing a decimal component
  • Integers, the string of digits must not contain non-digit characters
  • Readability can be improved by underscores, i.e. 11_111_111
  • Negative numbers are preceded by a minus (-), positive numbers may optionally be preceded by a plus (+).
  • Octal numbers are preceded by "0o" or "0O" such as 0o123
  • Hexadecimal numbers are preceded by "0x" or "0X" such as 0x123
  • Floats: Numbers designed to represent numbers that include a decimal component
  • Floats should not contain commas
  • "0" can be omitted is after or before a number, such as .4 instead of 0.4
  • Exponents are incorporated into a number string using E/e, for example: 3E8
  • Planck’s constant can be formatted as 6.62607E-34.
  • Python will sometimes use a different notation to what was coded
  • Strings are used to process text and require quotes ("")
  • Use a backslash to escape special characters inside quotation marks
  • Strings can also be enclosed with apostrophes (')

Boolean Values

  • Boolean values come from George Boole (1815-1864)
  • The values of True and False are denoted as 1 and 0
  • It is case-sensitive and cannot be changed

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser