Zybooks L3 Flashcards
print floating number two digits after decimal
print(f’{myFloat:.2f}’)
print(f’{math.pi:.2f}’)
print(f’{(7.0/3.0):.2f}’) outputs 2.33.
int(input())
when working with whole numbers
float(input())
when working with decimals
expression
combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 * (x + 1)
literal
a specific value in code like 2
operator
a symbol that performs a built-in calculation, like +, which performs addition. Common programming operators are shown below
exponent operator: **
x ** y (x to the power of y)
compound operators
shorthand way to update a variable
age += 1 age = age + 1
age -= 1
age *= 1
age /= 1
The division operator / performs division and returns a floating-point number.
20 / 10 is 2.0
floor division operator // can be used to round down the result of a floating-point division to the closest smaller whole number value.
20 // 10 is 2
The modulo operator (%) evaluates the remainder of the division of two integer operands
9 % 5 is 4. Reason: Since 9 = 5 * 1 + 4, the floor division 9 // 5 results in 1, and the remainder is 4
module
a file containing Python code that can be used by other modules or scripts
import statement
referencing and making another module available for use
python’s randrange()
method generates random integers within a specified range
random.randrange(10) returns an integer with 10 possible values: 0, 1, 2, …, 8, 9
python defined range code:
randint(min,max)
print(random.randint(12,20))
returns a randon integer between 12 and 20 inclusive
python defined range code:
randrange(min,max)
returns a random integer between min and max -1 inclusive
print(random.randrange(12,20)
- returns a random integer between 12 and 19 inclusive
seed // seed() method
program can specify the seed by calling seed() method. i.e. random.seed(5) // with a specific seed, each program run yields the same sequence of psuedo-random numbers
- for the first call to any random method (no previous random method exists), computer uses built-in integer based on current time to help generate a random number
unicode
python uses Unicode to represent every possible character as a unique number, known as a code point
escape sequence
the interpreter recognizes \ as the start of a special character’s two-item sequence
- print(‘\home\users\’) -> \home\users\
- print(‘Name: John O'Donald’) -> Name: John O’Donald
- print(“He said, "Hello friend!"”) -> He said, “Hello friend!”
- print(‘My name…\nIs John…’) -> My name…
Is John…
- print(‘1. Bake cookies\n\t1.1. Preheat oven’) ->
1. Bake cookies
1.1. Preheat oven
raw string
escape sequences can be ignored
- my_raw_string = r’This is a \n 'raw' string’
This is a \n 'raw' string