Python Essentials Flashcards
What are the six primitive values and types?
int, float, complex, bool, str, and NoneType
What is the int type? Give an example.
Integers (whole numbers). Example: x = 1
What is the float type? Give an example.
Floating-point numbers (decimals). Example: x = 1.0
What is the complex type? Give an example.
Numbers with real and imaginary parts. Example: x = 1 + 2j
What is the bool type? Give an example.
Boolean (True/False values). Example: x = True
What is the str type? Give an example.
Strings (characters/text). Example: x = ‘hello’
What is the NoneType type? Give an example.
Special object indicating nulls. Example: x = None
What do variables do? Give an example.
They name values. Example: greeting = ‘hey there’
What is floor division? Example: a // b
An operation for integers and floats that gives the quotient (removes fractional parts) of a and b in the example: a // b
Can you type an example of formatted I/O and formatted string literals?
print(‘{X} has type {T}’.format(X=x, T=type(x)))
print(f’{x} has type {type(x)}’)
What is a tuple?
A read-only (‘immutable’) fixed-length sequence of values
What is the immutable version of a set?
A frozen set! NOT a tuple.
Is it quicker to access a mutable or an immutable object?
Immutable
Does it ‘cost’ more compute to change a mutable object or immutable object?
Immutable, because it requires creating a copy
Can the values that print from a tuple change?
YES, because the binding is unchangeable but not that objects that they are bound to. For example, the tuple could contain the variable ‘x’ which can be modified.