Podcast
Questions and Answers
What does the left-hand side (LHS) of an assignment statement consist of?
What does the left-hand side (LHS) of an assignment statement consist of?
Which data type is associated with a whole number without a fractional part?
Which data type is associated with a whole number without a fractional part?
What does the '=' sign do in an assignment statement?
What does the '=' sign do in an assignment statement?
If an existing variable is assigned a new value, what happens?
If an existing variable is assigned a new value, what happens?
Signup and view all the answers
What happens when executing the assignment: 'cansPerPack = cansPerPack + 2'?
What happens when executing the assignment: 'cansPerPack = cansPerPack + 2'?
Signup and view all the answers
What is not a data type mentioned in the text?
What is not a data type mentioned in the text?
Signup and view all the answers
Which of the following is NOT a valid variable name in Python?
Which of the following is NOT a valid variable name in Python?
Signup and view all the answers
In Python, what is the importance of using descriptive variable names?
In Python, what is the importance of using descriptive variable names?
Signup and view all the answers
What is the purpose of using named constants in Python?
What is the purpose of using named constants in Python?
Signup and view all the answers
Why is it customary to use all UPPER_CASE letters for constants in Python?
Why is it customary to use all UPPER_CASE letters for constants in Python?
Signup and view all the answers
Which variable name is more self-descriptive?
Which variable name is more self-descriptive?
Signup and view all the answers
What can happen if a programmer does not use descriptive variable names in Python?
What can happen if a programmer does not use descriptive variable names in Python?
Signup and view all the answers
What is the correct syntax for naming variables in Python?
What is the correct syntax for naming variables in Python?
Signup and view all the answers
Which of the following is an example of a floating-point number in Python?
Which of the following is an example of a floating-point number in Python?
Signup and view all the answers
What is the term used for values like 6 or 0.355 that occur in a Python program?
What is the term used for values like 6 or 0.355 that occur in a Python program?
Signup and view all the answers
Which of the following is NOT allowed when naming variables in Python?
Which of the following is NOT allowed when naming variables in Python?
Signup and view all the answers
What will happen if a variable is assigned different data types at different points in a program in Python?
What will happen if a variable is assigned different data types at different points in a program in Python?
Signup and view all the answers
Which statement correctly describes the naming of variables in Python?
Which statement correctly describes the naming of variables in Python?
Signup and view all the answers
Study Notes
Variables and Data Types
- A variable is defined and initialized using the assignment statement (=) and the data type is associated with the value, not the variable.
- There are three types of data: whole numbers (integers), numbers with fractional parts (floats), and sequences of characters (strings).
- Examples of integers: -7, 6, 24
- Examples of floats: 8.88, -9.4, 1E6, 2.96E-2
- Examples of strings: "Bob"
Updating a Variable
- If an existing variable is assigned a new value, the new value replaces the previous contents of the variable.
- Example:
cansPerPack = 6
thencansPerPack = 8
updates the value to 8. - Executing an assignment:
cansPerPack = cansPerPack + 2
updates the value in two steps:- Calculate the right-hand side of the assignment.
- Store the result in the variable on the left side.
Number Types in Python
- Integers: whole numbers without fractions, e.g. 6, -32
- Floats: used when a fractional part is required, e.g. 0.5, 1.0, -9.4
- Number literals: values that occur in a Python program, e.g. 6, 0.355
Naming Variables
- Variable names should describe the purpose of the variable, e.g.
canVolume
instead ofcv
. - Rules for naming variables:
- Must start with a letter or underscore (_)
- Can include letters, digits, or underscore, but no other symbols or spaces
- Case sensitive, e.g.
canVolume
andcanvolume
are different - Should not use reserved Python words (see Appendix C)
Constants
- A constant is a variable whose value should not be changed after assignment.
- Good practice to use all caps when naming constants, e.g.
BOTTLE_VOLUME = 2.0
. - Named constants can explain numerical values used in calculations, making code clearer.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on variables, assignment statements, and expressions in programming. Learn about defining and initializing variables, using the assignment operator, and differentiating between the left-hand side and right-hand side of an assignment statement.