Python MAGES Flashcards
How do you convert this to a string?
x = 50
x = str(x)
How do you convert this to an integer?
x = “20”
x = int(x)
How do you convert this to a float?
x = “20”
x = float(x)
How to check if it x an int?
x = “hello”
if type(x) == int:
print(“I am an integer!”)
How do you add 5 to x?
x = 3
x = x + 3
How to check if it x a string?
x = “hello”
if type(x) == str:
print(“I am a string!”)
How to ask the user for an input and assign it to x?
x = input()
How to ask the user for an input with a question and assign it to x?
x = input(“Type something here: “)
What is the value of x here?
x = 2**3
** is the exponent operator. It is the “to the power of” operator.
23 is 222 which equals to 8
What is the value of x here?
x = 10 // 3
// 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 to check if x is equal to 20?
x = 5
if x == 20:
print(“x is equals to 20!”)
How to check if x is NOT equal to 20?
x = 5
if x != 20:
print(“x is NOT equals to 20!”)
How to check if x is less than 20?
x = 5
if x < 20:
print(“x is less than 20!”)
How to check if x is more than 20?
x = 5
if x > 20:
print(“x is more than 20!”)
How to check if x is more than or equals to 20?
x = 5
if x >= 20:
print(“x is more than or equals to 20!”)
How to check if both x and y are more than 20?
x = 5
y = 25
if x > 20 and y > 20:
print(“x and y are more than 20!”)
How to check if either x or y are more than 20?
x = 5
y = 25
if x > 20 or y > 20:
print(“either x or y are more than 20!”)
How to create an empty list x?
x = []
How to create a list that already contains 3 items?
x = [‘a’, ‘b’, ‘c’]
How to add to a list?
x = [‘a’, ‘b’, ‘c’]
x.append(‘d’)
How to get only the first 3 from the list?
x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
y = x[:3]
How to get the 3rd item from the list?
x = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
y = x[2]