Module 2 Flashcards
variable
a storage location with a name. Each variable has a name and holds a value
Why is a variable similar to/compared to a parking space?
Parking space has an identifier and contents like a variable in a computer program
assignment statement
stores a value in a variable, aka you use it to place a value into a variable
example of an assignment statement
cansPerPack = 6
The left-hand side of an assignment statement consists of a variable. The right-hand side an expression that has a value. That value is stored in the variable.
When is a variable created
the first time it is assigned a value
What happens if you assign a value to an already existing variable?
It replaces the previously stored value
True or False: The assignment operator = does denote mathematical equality
FALSE: It does NOT denote mathematical equality. Assignment is an instruction to do something – namely, place a value into a variable, not the equal symbol in algebra to denote equality.
data type
- determines how the data is represented in the computer and what operations can be performed on that data.
- Specifies how the value is stored in the computer and what operations can be performed on the value.
primitive data type
a data type provided by the language itself
What data types does Python support?
numbers, text, strings, files, containers, and many others.
Integer
In python it is called int
Whole numbers without a fractional part. No fractions or decimals.
floating-point numbers
called float in Python
unlike integers (whole numbers), they are fractional numbers (such as 0.555)
OR
numbers with exponential notation
float vs int in Python
float is a floating-point number/value (fractional numbers or numbers with exponential notation)
int is an integer value/number (whole numbers)
Number literal
a fixed value in a program this is explicitly written as a number, such as -2 or 6.02214115E23
can be an integer or floating point number
Why should you always store values of the same type once a variable is initialized?
It helps avoid error in your code.
Example:
taxRate = 5.0
Although the number is a whole number, it is stored as a floating point number because you can get a fractional number later in your code. This avoids error by all number being unified by using floating point numbers and not a mix of integers and floating point numbers.
Naming something in Python rules:
- Names must start with a letter or an underscore, and remaining characters must be letters, numbers, or underscores.
- You cannot use symbols such as ? or %. Spaces are not permitted. You can use uppercase letters to denote word boundaries, as in cansPerPack.
- Names are case sensitive
4.You cannot used reserved words (words reserved for their special Python meanings), such as if or class.
Why should you start names with a lower case?
Names that are all uppercase indicate constants, and names that start with an uppercase letter are commonly used for user-defined data types.
Constant/constant variable
a value that should not/cannot by changed by a program. In Python, constants customarily have names consisting of all uppercase letters.
Comments
an explanation to help the human reader understand a section of a program; ignored by the interpreter.
uses a hashtag(#)
Why are comments a good practice?
It helps programmers who read your code understant your intent.
magic number
a numeric constant that appears in your code without explanation