Stuff Flashcards
(38 cards)
How would you get the type of a value in Python?
Using the type() function.
What is the difference between tuples and lists in Python?
You can not modify items in a tuple. In a list, you can.
Why would you use a tuple?
- More efficient in memory because it only uses the space it needs vs a list will allocate more that it needs because the size is unbound.
What is this “ {2, 3, 4, 5} “ in Python?
This is a Set. (Data Structure)
What is this “ (3, 0, ‘UM’) “ in Python?
This a Tuple. (Data Structure)
What is this “ [ ‘app’, ‘tiger’, ‘pen’] “ in Python?
This is a List. (Data Structure)
What is this “ { ‘name’: ‘Joe’, id: 4994} “ in Python?
This is a Dictionary, (Data Structure)
What does this “ ** “ do in Python?
This is the exponent arithmetic operator.
EX: 2 ** 3 -> 8
What does this “ // “ do in Python?
This is a division arithmetic operator that will give you an integer as a result rather than a float.
EX:
4 // 2 -> 2
4 / 2 -> 2.0
What does this produce?
‘Hello’ * 3
‘HelloHelloHello’
What do these Logical operators do?
- and
- or
- not
- and is used when you need both items to be true.
and is like && in JS - or is used when you need either item to be true.
or is like || in JS - not is used on 1 operator and is used to flip the truth/false value
not is like the bang op. “!” in JS.
not True -> False
What do these Membership Operators do?
in
not in
These check to see if an item is in or not in a string and or data structure.
Ex.
‘a’ in ‘apple’ -> True
‘toy’ not in [‘car’, ‘bike] -> True
How would you write a for loop in Python for this list?
my_list = [ 1, 2, 3, 3 ]
for num in my_list:
print(num)
How would you write a while loop Python for this list?
my_list = [ 1, 2, 3, 3 ]
i = 0
while i < my_list.len():
print(my_list[i])
i += 1
What does the __init__() function do?
This is called when a Class is initialized. It sets up the class with values based on what we define.
EX
class Dog: def \_\_init\_\_(self): self.name = 'Danny' self.legs = 4
Assuming a Dog class exists, what is the type that this statement returns?
type(Dog(‘Rover’))
Dog. It will return the name of the class.
What does this do?
int(8.99999)
Returns 8.
This is called casting from a float to an int. Python will not round this for you, it just takes away all decimal places.
What do the following do?
round(10 / 3)
round(10 / 3, 2)
round(10 / 3) -> 3
round(10 / 3, 2) -> 3.33
This is the round class that Python has built in. It rounds your floats for you. The 2nd arg is optional and allows you to define how many decimal places you want.
What does 1.2 - 1.0 return and how would you adjust the return?
It returns 0.19999999996 because python uses approximation for floats and could give unexpected results.
To fix this you could do the following:
round(1.2 - 1.0, 2) -> 0.2
How would you grab just the first name below?
name = ‘Jim Jones’
name[0:3] -> Jim
or
name[:3]
How would you grab just the last name below?
name = ‘Jim Jones’
name[4:]
What are the 2 ways to combine the the strings below?
name = ‘Deek’
id = 22
- name + ‘-‘ + id
- f’{name}-{id}’ –This is the f string. It is like a template literal in JS.
How would you create a multi-line string?
By using triple quotes.
EX.
''' Dear Allen, Hi. -Ray '''
What is this?
b’\x00\x00\x00\x00’
A bytes object