Python MAGES Flashcards

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’]

A

y = x[4]

24
Q

How to get a copy of a list?

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

A

y = x[:]

25
Q

How to loop through a list?

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

A

for i in x:
print(i)

26
Q

How to sort a list alphabetically?

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

A

x.sort()

27
Q

How to reverse a list order?

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

A

x.reverse()

OR

x.sort(reverse=True)

28
Q

How to get the reverse of a list without changing the original list?

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

A

y = x.copy()
y.sort()

29
Q

How to create a list of lists?

A

Example:

tic_tac_toe = [
[‘X’, ‘X’, ‘O’],
[‘X’, ‘O’, ‘X’],
[‘O’, ‘O’, ‘X’]
]

30
Q

How to get the middle of this list of lists?

tic_tac_toe = [
[‘X’, ‘X’, ‘O’],
[‘X’, ‘O’, ‘X’],
[‘O’, ‘O’, ‘X’]
]

A

tic_tac_toe[1][1]

Which will give you ‘O’

31
Q

How to create an empty dictionary?

A

x = {}

32
Q

How to create a dictionary with 2 keys and 2 values?

A

x = {
“name”: “David”,
“age”: 20
}

33
Q

How to access the key “age” of this dictionary?

x = {
“name”: “David”,
“age”: 20
}

A

y = x[“age”]

print(y) # if you want to display it

34
Q

How to add a new key-value pair to this dictionary?

x = {
“name”: “David”,
“age”: 20
}

A

x[“hobby”] = “golfing”

35
Q

How to delete the “age” key-value pair in this dictionary?

x = {
“name”: “David”,
“age”: 20
}

A

del x[“age”]

36
Q

How to create a function called func_1 that accepts no arguments?

A

def func_1():
print(“hello”)

37
Q

How to create a function called func_1 that accepts 2 arguments?

A

def func_1(a, b):
print(a)
print(b)

38
Q

How to create a function called func_1 that accepts 2 arguments, one of them with a default value?

A

def func_1(a, b = “default value”):
print(a)
print(b)

39
Q

How to call this function?

def func_1(a, b):
print(a)
print(b)

A

func_1(“hello”, “byebye”)

40
Q

How to call this function?

def func_1():
print(“hello”)

A

func_1()

41
Q

How to check if something exists in a list?

comments = [
‘a’]

A

if ‘You

42
Q

How to check if something exists in a list?

comments = [
‘a’]

A

if ‘You

43
Q

How to check if something exists in a list?

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

A

if ‘d’ in x:
print(‘d exists!’)

44
Q

How to check if something is a list?

ini_list1 = [1, 2, 3, 4, 5]
ini_list2 = ‘12345’

A

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
Q

How to create an empty tuple?

A

x = ()

46
Q

How to create a tuple with 3 values inside?

A

x = (‘a’, ‘b’, ‘c’)

47
Q

How to edit this tuple?

x = (‘a’, ‘b’, ‘c’)

A

Trick question! A tuple cannot be edited/mutated.

48
Q

How to check if x is between 10 and 20?

x = 15

A

if 10 < x < 20:
print(‘Between 10 and 20!’)

49
Q

How to create a new list with only the ‘b’, ‘c’ and ‘d’ in this list?

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

A

y = x[1:4]

50
Q

How to get the last item in this list?

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

A

y = x[-1]

51
Q

How to write a single line comment in Python?

A

this is my comment

52
Q

How to write a single line comment in Python on the same line as a code?

A

x = 10 # this is a comment

53
Q

How to write a multi line comment in Python?

A

’'’This is a multi
Line comment.
It’s not really a comment
but it behaves like one.
‘’’

54
Q

How to print out all the keys in this dictionary?

x = {
“name”: “David”,
“age”: 20
}

A

y = x.keys()
print(y)

55
Q

How to print out all the values in this dictionary?

x = {
“name”: “David”,
“age”: 20
}

A

y = x.values()
print(y)

56
Q

How to check if x is an even number?

x = 6

A

if x % 2 == 0:
print(“x is even!”)