Chapter 2 Variables and assignments Flashcards
In a program, a variable is a …?
An assignment statement assigns…?
In a program, a variable is a named item, such as x or num_people, used to hold a value. A good variable name should describe the purpose of the variable, such as “temperature” or “age,” rather than just “t” or “A.”
An assignment statement assigns a variable with a value, such as x = 5. That statement means x is assigned with 5, and x keeps that value during subsequent statements until x is assigned again.
An assignment statement’s left side must be a …?
The right side can be …?
An assignment statement’s left side must be a variable. The right side can be an expression, so a statement may be x = 5, y = x, or z = x + 2. The 5, x, and x + 2 are each an expression that evaluates to a value.
= is not equals
In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as “x is assigned with 5,” and not as “x equals 5.” When one sees x = 5, one might think of a value being put into a box.
Define identifier
An identifier, also called a name, is a sequence of letters (a-z, A-Z), underscores (_), and digits (0–9), and must start with a letter or an underscore.
IS Python case sensative?
Yes
Reserved words, or keywords,
Certain words like “and” or “True” cannot be used as names. Reserved words, or keywords, are words that are part of the language, and thus, cannot be used as a programmer-defined name. Many language editors will automatically color a program’s reserved words.
Python 3 Reserved Keywords
False await else import pass None break except in raise True class finally is return and continue for lambda try as def from nonlocal while assert del global not with async elif if or yield
Define an object
An object represents a value and is automatically created by the interpreter when executing a line of code. For example, executing x = 4 creates a new object to represent the value 4. A programmer does not explicitly create objects; instead, the interpreter creates and manipulates objects as needed to run the Python code. Objects are used to represent everything in a Python program, including integers, strings, functions, lists, etc.
Define garbage collection
Above, the interpreter performs an addition of 2+2, resulting in a new object being created with a value of 4. Once 4 is printed the object is no longer needed, so the object is automatically deleted from memory and thrown away. Deleting unused objects is an automatic process called garbage collection that helps to keep the memory of the computer less utilized.
Define name binding
Name binding is the process of associating names with interpreter objects. An object can have more than one name bound to it, and every name is always bound to exactly one object. Name binding occurs whenever an assignment statement is executed, as demonstrated below.
Name the 3 properties of objects in Python
Each Python object has three defining properties: value, type, and identity.
Value: A value such as “20”, “abcdef”, or 55.
Type: The type of the object, such as integer or string.
Identity: A unique identifier that describes the object.
Define Mutability
The built-in function type() returns the type of an object.
Python provides a built-in function id() that gives the value of an object’s identity.
The type of an object also determines the mutability of an object. Mutability indicates whether the object’s value is allowed to be changed. Integers and strings are immutable; modifying their values with assignment statements results in new objects being created and the names bound to the new object.
Define Floating Point number
floating-point number is a real number, like 98.6, 0.0001, or -666.667. The term “floating-point” refers to the decimal point being able to appear anywhere (“float”) in the number. Thus, float is a data type for floating-point numbers.
A floating-point literal is written with the fractional part even if that fraction is 0, as in 1.0, 0.0, or 99.0. scientific notation is written using an e preceding the power-of-10 exponent, as in 6.02e23 to represent 6.02x1023. The e stands for exponent. Likewise, 0.001 is 1x10-3, so it can be written as 1.0e-3
Define OverflowError
Float-type objects have a limited range of values that can be represented. For a standard 32-bit installation of Python, the maximum floating-point value is approximately 1.8x10308, and the minimum floating-point value is 2.3x10-308. Assigning a floating-point value outside of this range generates an OverflowError. Overflow occurs when a value is too large to be stored in the memory allocated by the interpreter. For example, the program in the figure below tries to store the value 2.01024, which causes an overflow error
print(f’{math.pi:.4f}’) outputs Pi to only four digits after the decimal. The last digit is rounded up in the output, but the value of Pi remains the same.
Default output of Pi: 3.141592653589793
Pi reduced to 4 digits after the decimal: 3.1416
int(f’{9.1357:.3f}’)) outputs 9.136 because the third digit after the decimal point is rounded.