Podcast
Questions and Answers
In programming, what is a 'name' primarily used for?
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?
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?
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?
What role do special characters like $, @, or % play at the beginning of a variable name in Perl?
In Python, how should words be separated to improve readability in function names, according to common naming conventions?
In Python, how should words be separated to improve readability in function names, according to common naming conventions?
What is the main characteristic of 'Pascal case' as a naming convention?
What is the main characteristic of 'Pascal case' as a naming convention?
What is the term for words that have predefined meanings in a programming language and cannot be used as variable names?
What is the term for words that have predefined meanings in a programming language and cannot be used as variable names?
Which of the following represents the correct analogy for a variable in programming?
Which of the following represents the correct analogy for a variable in programming?
Which attribute of a variable determines the permissible range of values and the set of operations that can be performed on it?
Which attribute of a variable determines the permissible range of values and the set of operations that can be performed on it?
In the context of variables, what does 'lifetime' refer to?
In the context of variables, what does 'lifetime' refer to?
What is the term for the association of an attribute to an entity in programming?
What is the term for the association of an attribute to an entity in programming?
What is the significance of 'binding time' in programming?
What is the significance of 'binding time' in programming?
What is an 'explicit declaration' in programming?
What is an 'explicit declaration' in programming?
What characterizes 'dynamic type binding'?
What characterizes 'dynamic type binding'?
Which concept defines the region of a program where a variable is accessible and can be used?
Which concept defines the region of a program where a variable is accessible and can be used?
What is meant by 'global scope'?
What is meant by 'global scope'?
According to the LEGB rule in Python, what does 'E' stand for?
According to the LEGB rule in Python, what does 'E' stand for?
If a variable in a local scope has the same name as a variable in an outer scope, what phenomenon occurs?
If a variable in a local scope has the same name as a variable in an outer scope, what phenomenon occurs?
What keyword is used in Python to modify a global variable from within a function?
What keyword is used in Python to modify a global variable from within a function?
Which data type is designed to represent numbers with a non-empty decimal fraction?
Which data type is designed to represent numbers with a non-empty decimal fraction?
Flashcards
Name (in programming)
Name (in programming)
A string of characters used to identify an entity in a program.
Camel Case
Camel Case
Words in a multi-word name are capitalized except the first word.
Reserved Word
Reserved Word
A special word that cannot be used as a name in a programming language.
Variable (in programming)
Variable (in programming)
Signup and view all the flashcards
Variable Address
Variable Address
Signup and view all the flashcards
Variable Type
Variable Type
Signup and view all the flashcards
Variable Value
Variable Value
Signup and view all the flashcards
Variable Lifetime
Variable Lifetime
Signup and view all the flashcards
Variable Scope
Variable Scope
Signup and view all the flashcards
Binding (in programming)
Binding (in programming)
Signup and view all the flashcards
Binding Time
Binding Time
Signup and view all the flashcards
Explicit Declaration
Explicit Declaration
Signup and view all the flashcards
Implicit Declaration
Implicit Declaration
Signup and view all the flashcards
Dynamic type binding
Dynamic type binding
Signup and view all the flashcards
Scope of a Variable
Scope of a Variable
Signup and view all the flashcards
Global Scope
Global Scope
Signup and view all the flashcards
Local Scope
Local Scope
Signup and view all the flashcards
Enclosing Scope
Enclosing Scope
Signup and view all the flashcards
Built-in Scope
Built-in Scope
Signup and view all the flashcards
LEGB Rule
LEGB Rule
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()
, andint()
. - 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.