Stuff Flashcards

(38 cards)

1
Q

How would you get the type of a value in Python?

A

Using the type() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between tuples and lists in Python?

A

You can not modify items in a tuple. In a list, you can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why would you use a tuple?

A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is this “ {2, 3, 4, 5} “ in Python?

A

This is a Set. (Data Structure)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is this “ (3, 0, ‘UM’) “ in Python?

A

This a Tuple. (Data Structure)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is this “ [ ‘app’, ‘tiger’, ‘pen’] “ in Python?

A

This is a List. (Data Structure)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is this “ { ‘name’: ‘Joe’, id: 4994} “ in Python?

A

This is a Dictionary, (Data Structure)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does this “ ** “ do in Python?

A

This is the exponent arithmetic operator.
EX: 2 ** 3 -> 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does this “ // “ do in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does this produce?
‘Hello’ * 3

A

‘HelloHelloHello’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do these Logical operators do?
- and
- or
- not

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do these Membership Operators do?
in
not in

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you write a for loop in Python for this list?
my_list = [ 1, 2, 3, 3 ]

A

for num in my_list:
print(num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you write a while loop Python for this list?
my_list = [ 1, 2, 3, 3 ]

A

i = 0
while i < my_list.len():
print(my_list[i])
i += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the __init__() function do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Assuming a Dog class exists, what is the type that this statement returns?

type(Dog(‘Rover’))

A

Dog. It will return the name of the class.

17
Q

What does this do?
int(8.99999)

A

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.

18
Q

What do the following do?
round(10 / 3)
round(10 / 3, 2)

A

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.

19
Q

What does 1.2 - 1.0 return and how would you adjust the return?

A

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

20
Q

How would you grab just the first name below?
name = ‘Jim Jones’

A

name[0:3] -> Jim
or
name[:3]

21
Q

How would you grab just the last name below?
name = ‘Jim Jones’

22
Q

What are the 2 ways to combine the the strings below?

name = ‘Deek’
id = 22

A
  1. name + ‘-‘ + id
  2. f’{name}-{id}’ –This is the f string. It is like a template literal in JS.
23
Q

How would you create a multi-line string?

A

By using triple quotes.
EX.

'''
Dear Allen,
Hi.
-Ray
'''
24
Q

What is this?
b’\x00\x00\x00\x00’

A

A bytes object

25
What will my_list look like after the code below? my_list = [0, 1, 2, 3, 4, 5] my_list = my_list[::2]
[0, 2, 4] the third number in the list slicing is saying how much do you want to slice by. In this situation it skips over by 2.
26
What does range() do and how can you use it?
It creates a range of integers. ex: ``` for i in range(10): print(i) my_list = list(range(10)) --> [0, 1, 2, ...] ```
27
What does the LIST.append() do?
This adds the given value to the end of a list
28
What does the LIST.insert() do?
This inserts a value at a given index. EX ``` my_list = ['cat', 'dog', 'cow] my_list.insert(1, 'pig') -> ['cat', 'pig', 'dog', 'cow'] ```
29
What does the LIST.remove() do?
It removes the given value from the list and throws an error if the item does not exist.
30
How would you delete an item in a set?
my_set.discard('item')
31
How would you get the keys and values from a Dictionary in Python?
my_dict.keys() my_dict.values()
32
What are 2 of the ways you can access a value in a Dictionary?
my_dict.get('key') my_dict['key']
33
What does the this block of code return? And what is it called? ``` myList = [1,2,3,4,5] [2*item for item in myList] ```
[2, 3, 6, 8, 10] This is called List Comprehension. It is looping over the list and making a copy of it with new values that are 2 * the originals.
34
What does this block of code return? ``` myList = list(range(10)) filteredList = [item for item in myList if item % 2 == 0] filteredList ```
[0, 2, 4, 6, 8, 10]
35
What does this block of code do? ``` amounts = ['22.99', '1.99', '3.22'] def addDollarSign(price): return '$' + price [addDollarSign(price) for price in amounts] ```
This is iterating over the amounts list and anding a dollar sign to the front of each. This is similar to .map() in JS.
36
What does the pass statement do in Python?
It is a null statement. The interpreter doesn't ignore it, it just reads it as a no operation. Good for a function definition that you want to define later and just put a place holder in temporarily.
37
What do *ags and **kwargs do in a Python function? Ex: ``` def my_func(*args, **kwargs): do stuff ```
*args allows you to have an unlimited amount of arguments. Then you can access them inside of your function as a tuple. **kwargs allows you to have an unlimited amount of keyword args. You can access them inside of your function as a dictionary. EX: ``` def my_func(*args, **kwargs): print(args) print(kwargs) my_func(1, 2, 3, name='tom', city='atlanta') output (1, 2, 3) {name: 'tom', city:'atlanta'} ```
38
What does Try/Expect do?
It allows you to handle errors cleanly. Example: ``` def causeError(): try: return 1/0 except Exception as e: return e causeError() output: ZeroDivisionError('division by zero') ```