1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
variable
is a named item, such as x or num_people, that holds a value.
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.
Processor
The processor runs the computer's programs, reading and executing instructions from memory, performing operations, and reading and writing data from and to memory. When powered on, the processor starts executing the program. The first instruction is typically at memory location 0. That program is commonly called the BIOS (basic input/output system), which sets up the computer's basic peripherals
Memory: RAM
(random-access memory) temporarily holds data read from storage and is designed so any address can be accessed much faster than from a disk. The "random access" term comes from accessing any memory location quickly and in arbitrary order, without spinning a disk to get a proper location under a head. RAM is more costly per bit than disk due to RAM's higher speed. RAM chips typically appear on a printed circuit board along with a processor chip. RAM is volatile, losing its contents when powered off. Memory size is typically listed in bits, or in bytes where a byte is 8 bits. Common sizes involve megabytes (million bytes), gigabytes (billion bytes), or terabytes (trillion bytes).
incrementing
Increasing a variable's value by 1, as in x = x + 1, is common and known as incrementing the variable.
Python is ____ meaning uppercase and lowercase letters differ. Ex: "Cat" and "cat" are different. The following are valid names: c, cat, Cat, n1m1, short1, and _hello. The following are invalid names: 42c (doesn't start with a letter or underscore), hi there (has a space), and cat$ (has a symbol other than a letter, digit, or underscore).
case sensitive
An ____ 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.
object
garbage collection
Deleting unused objects is an automatic process called garbage collection that frees memory space
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.
Mutability
indicates whether the object's value is allowed to be changed.
Integers and strings are ___; meaning integer and string values cannot be changed.
immutable
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 _____ Overflow occurs when a value is too large to be stored in the memory allocated by the interpreter.
Overflow Error
In general, floating-point types should be used to represent quantities that are measured, such as distances, temperatures, volumes, and weights. Integer types should be used to represent quantities that are counted, such as numbers of cars, students, cities, hours, and minutes
true
What is output by print(f'{9.1357:.3f}')
?
9.136
print(f'{9.1357:.3f}')
outputs 9.136 because the third digit after the decimal point is rounded.
print(f'{my_float:.5f}')
{} → means insert a value here.
my_float → the variable you want to print.
: → this tells Python “I’m going to specify a format now.”
.5f →
. → start formatting the decimal part.
5 → 5 digits after the decimal point.
f → format it as a floating-point number (decimal number).
| Format | Meaning | Example output |
| ------ | ---------------- | -------------- |
| .2f
| 2 decimal places | 3.14
|
| .3f
| 3 decimal places | 3.142
|
| .5f
| 5 decimal places | 0.71429
|
Specifies the number of digits after the decimal point in floating-point formatting.
When should you use float(input())
instead of int(input())?
Use float(input())
if you need to print or calculate with decimals (e.g., using .2f
, .3f
formatting).
Use int(input())
only for whole numbers without decimals.
If you see .2f
, .3f
, .5f
→ use
float()
If you're counting things (like apples, students) use
int()
expression
is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 (x + 1). A common place where expressions are used is on the right side of an assignment statement, as in y = 2 (x + 1).
literal
specific code value like 2
operator
is a symbol that performs a built-in calculation, like +, which performs addition.
In programming, multiplication typically must be indicated explicitly using which operator?
*
Commas are not allowed in an integer literal. So 1,333,555 is written as 1333555.
true
Which expression gets the tens digit of x? Ex: If x = 693, which expression yields 9?
x // 10 removes the rightmost digit, putting the tens digit in the ones place. Then % 10 gets the (new) ones digit. Ex: 693 / / 10 is 69, then 69 % 10 is 9.
%
(Modulo)
Gives the remainder after division.
"What's left over?"
Example:
7 % 3
➡1
(because 7 - 3×2 = 1 left over)
//
(Floor Division)
Divides and gives the whole number (integer) part only.
"How many times does it fit?"
Example:
7 // 3
➡2
(because 3 fits into 7 two times)
Use //
when you care about how many full items fit.
Use %
when you care about what's left over.
Floor division gives the whole number result of division, while modulo gives the remainder. They are both used in calculations to determine how many complete units fit or what remains.
module
is Python code located in another file
is a list of statements that can be executed simply by referring to the function's name. The statements for sqrt() are within the math module itself and are not relevant to the programmer.
Function
ceil(x)
round up value
fabs(x)
absolute value
floor(x)
round down value
randrange()
method generates random integers within a specified range. A single positive integer argument can be passed to the randrange() method to return an integer between 0 (inclusive) and the specified value (exclusive). Ex: random.randrange(10)
returns an integer with 10 possible values: 0, 1, 2, ..., 8, 9.
How many possible values can be produced by random.randint(-5, 5)?
-5 to 5 has 11 possible values: -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.
Ranges involving negatives are treated the same as ranges with only positives
Unicode
Python uses Unicode to represent every possible character as a unique number, known as a code point. For example, the character "G" has the code point decimal value of 71.
What is the output ofprint('10...\n9...')
10...
9...
ord()
returns an encoded integer value for a string of length one.
chr()
returns a string of one character for an encoded integer.
Adding r before the string literal causes all escape sequences in a string to be ignored.
ex: print(r'The escape sequence for new line is \n'
answer: The escape sequence for new line is \n
One approach to output "\\" in a string literal is to add an "r" before the string literal. Alternatively, an additional "\" can be inserted before each existing "\" to escape the backslashes.
What is the formula for the perimeter of a square?
Perimeter = 4 × edge length
(In Python: square_perim = 4.0 * square_edge
)
How do you compute and print the square perimeter if edge length is read from input?
How do you fix a NameError
?
A: Make sure you spelled the variable name exactly the same everywhere and defined it before using it.
Example:
x = 5 print(x) #
✅
Complete the program as follows:
Read val1 and val2 from input as floats, respectively.
Compute mean_of_two using the formula .
Output 'Mean is ' followed by the value of mean_of_two to three digits after the decimal point.
Ex: If the input is:
10.0 24.0
then the output is:
Mean is 17.000
val1 = float(input())
val2 = float(input())
mean_of_two = (val1 + val2) / 2.0
print(f'Mean is {mean_of_two:.3f}')
How do you compute the average of two numbers in Python?
avg = (num1 + num2) / 2.0