Variables and Assignment Statements Quiz

RelaxedArtInformel avatar
RelaxedArtInformel
·
·
Download

Start Quiz

Study Flashcards

18 Questions

What does the left-hand side (LHS) of an assignment statement consist of?

A variable

Which data type is associated with a whole number without a fractional part?

Integer

What does the '=' sign do in an assignment statement?

Copies the value on the right side into the variable on the left side

If an existing variable is assigned a new value, what happens?

The previous contents of the variable are cleared

What happens when executing the assignment: 'cansPerPack = cansPerPack + 2'?

Adds 2 to cansPerPack

What is not a data type mentioned in the text?

"Alice"

Which of the following is NOT a valid variable name in Python?

2morrow

In Python, what is the importance of using descriptive variable names?

It improves code readability and understanding

What is the purpose of using named constants in Python?

To explain numerical values used in calculations

Why is it customary to use all UPPER_CASE letters for constants in Python?

To make constants easier to identify in the code

Which variable name is more self-descriptive?

canVolume = 0.35

What can happen if a programmer does not use descriptive variable names in Python?

Other programmers will not be able to understand the code

What is the correct syntax for naming variables in Python?

Variable names must start with a letter or underscore, and can contain letters, digits, and underscores

Which of the following is an example of a floating-point number in Python?

0.5

What is the term used for values like 6 or 0.355 that occur in a Python program?

Number literals

Which of the following is NOT allowed when naming variables in Python?

Including spaces in variable names

What will happen if a variable is assigned different data types at different points in a program in Python?

It will take the data type of the last assignment without issue

Which statement correctly describes the naming of variables in Python?

'canVolume' is preferred over 'cv'

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 then cansPerPack = 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 of cv.
  • 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 and canvolume 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.

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.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser