Python MAGES Flashcards

(56 cards)

1
Q

How do you convert this to a string?

x = 50

A

x = str(x)

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

How do you convert this to an integer?

x = “20”

A

x = int(x)

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

How do you convert this to a float?

x = “20”

A

x = float(x)

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

How to check if it x an int?

x = “hello”

A

if type(x) == int:
print(“I am an integer!”)

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

How do you add 5 to x?

x = 3

A

x = x + 3

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

How to check if it x a string?

x = “hello”

A

if type(x) == str:
print(“I am a string!”)

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

How to ask the user for an input and assign it to x?

A

x = input()

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

How to ask the user for an input with a question and assign it to x?

A

x = input(“Type something here: “)

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

What is the value of x here?

x = 2**3

A

** is the exponent operator. It is the “to the power of” operator.

23 is 222 which equals to 8

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

What is the value of x here?

x = 10 // 3

A

// is the floor operator. It will return you only the part before the decimal point.

10 divided by 3 will give you 3.3333…, so 10//3 will give you 3.

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

How to check if x is equal to 20?

x = 5

A

if x == 20:
print(“x is equals to 20!”)

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

How to check if x is NOT equal to 20?

x = 5

A

if x != 20:
print(“x is NOT equals to 20!”)

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

How to check if x is less than 20?

x = 5

A

if x < 20:
print(“x is less than 20!”)

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

How to check if x is more than 20?

x = 5

A

if x > 20:
print(“x is more than 20!”)

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

How to check if x is more than or equals to 20?

x = 5

A

if x >= 20:
print(“x is more than or equals to 20!”)

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

How to check if both x and y are more than 20?

x = 5
y = 25

A

if x > 20 and y > 20:
print(“x and y are more than 20!”)

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

How to check if either x or y are more than 20?

x = 5
y = 25

A

if x > 20 or y > 20:
print(“either x or y are more than 20!”)

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

How to create an empty list x?

A

x = []

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

How to create a list that already contains 3 items?

A

x = [‘a’, ‘b’, ‘c’]

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

How to add to a list?

x = [‘a’, ‘b’, ‘c’]

A

x.append(‘d’)

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

How to get only the first 3 from the list?

x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

A

y = x[:3]

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

How to get the 3rd item from the list?

x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

A

y = x[2]

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

How to get the index number 4 from the list?

x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

24
Q

How to get a copy of a list?

x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

25
How to loop through a list? x = ['a', 'b', 'c', 'd', 'e']
for i in x: print(i)
26
How to sort a list alphabetically? x = ['a', 'd', 'c', 'e', 'b']
x.sort()
27
How to reverse a list order? x = ['a', 'd', 'c', 'e', 'b']
x.reverse() OR x.sort(reverse=True)
28
How to get the reverse of a list without changing the original list? x = ['a', 'd', 'c', 'e', 'b']
y = x.copy() y.sort()
29
How to create a list of lists?
Example: tic_tac_toe = [ ['X', 'X', 'O'], ['X', 'O', 'X'], ['O', 'O', 'X'] ]
30
How to get the middle of this list of lists? tic_tac_toe = [ ['X', 'X', 'O'], ['X', 'O', 'X'], ['O', 'O', 'X'] ]
tic_tac_toe[1][1] Which will give you 'O'
31
How to create an empty dictionary?
x = {}
32
How to create a dictionary with 2 keys and 2 values?
x = { "name": "David", "age": 20 }
33
How to access the key "age" of this dictionary? x = { "name": "David", "age": 20 }
y = x["age"] print(y) # if you want to display it
34
How to add a new key-value pair to this dictionary? x = { "name": "David", "age": 20 }
x["hobby"] = "golfing"
35
How to delete the "age" key-value pair in this dictionary? x = { "name": "David", "age": 20 }
del x["age"]
36
How to create a function called func_1 that accepts no arguments?
def func_1(): print("hello")
37
How to create a function called func_1 that accepts 2 arguments?
def func_1(a, b): print(a) print(b)
38
How to create a function called func_1 that accepts 2 arguments, one of them with a default value?
def func_1(a, b = "default value"): print(a) print(b)
39
How to call this function? def func_1(a, b): print(a) print(b)
func_1("hello", "byebye")
40
How to call this function? def func_1(): print("hello")
func_1()
41
How to check if something exists in a list? comments = [ 'a']
if 'You
42
How to check if something exists in a list? comments = [ 'a']
if 'You
43
How to check if something exists in a list? x = ['a', 'd', 'c', 'e', 'b']
if 'd' in x: print('d exists!')
44
How to check if something is a list? ini_list1 = [1, 2, 3, 4, 5] ini_list2 = '12345'
if type(ini_list1) is list: print("your object is a list") else: print("your object is not a list") if type(ini_list2) is list: print("your object is a list") else: print("your object is not a list")
45
How to create an empty tuple?
x = ()
46
How to create a tuple with 3 values inside?
x = ('a', 'b', 'c')
47
How to edit this tuple? x = ('a', 'b', 'c')
Trick question! A tuple cannot be edited/mutated.
48
How to check if x is between 10 and 20? x = 15
if 10 < x < 20: print('Between 10 and 20!')
49
How to create a new list with only the 'b', 'c' and 'd' in this list? x = ['a', 'b', 'c', 'd', 'e']
y = x[1:4]
50
How to get the last item in this list? x = ['a', 'b', 'c', 'd', 'e']
y = x[-1]
51
How to write a single line comment in Python?
this is my comment
52
How to write a single line comment in Python on the same line as a code?
x = 10 # this is a comment
53
How to write a multi line comment in Python?
'''This is a multi Line comment. It’s not really a comment but it behaves like one. '''
54
How to print out all the keys in this dictionary? x = { "name": "David", "age": 20 }
y = x.keys() print(y)
55
How to print out all the values in this dictionary? x = { "name": "David", "age": 20 }
y = x.values() print(y)
56
How to check if x is an even number? x = 6
if x % 2 == 0: print("x is even!")