Python Flashcards
Sign to use for comments.
#
Function that can help you identify a variable’s data type.
print(type()) which can give back <class ‘float’/‘str’/‘bool’>
ex.:x=42
print(type(x))
<class ‘int‘>
How to write “2^3”?
print(2**3)
What does % do?
It calculates the remainder after left/right (exemple: print(5%2) will make 1 while print(2%4) will make 2 because 4 is bigger than 2.
How to use calculator mode?
print()
Storing a value in a variable.
Declaration
Whole number
Integer
Numbers with decimals
Floats
Non-numerical data
strings
“True” and “False” variation
boolean
money_in=100
print(money_in)
100
money_in=100
money_out=50
profit=money_in-money_out
print(profit)
50
How can you write: “I completed 3 courses!“
print(‘I completed‘+str(3)+‘courses!‘)
How can you convert a string into an integer?
You use int()
ex.:print(int(‘2‘)+2)
4
How can you convert a string into a float?
You use float()
ex.:print(float(‘2‘)+2.0)
4.0
How can you convert objects to a boolean value?
You use bool()
ex.:print(bool(2), bool(0), bool(‘hello‘))
True False True
How can you print “alright alright alright“?
print(‘alright‘*3)
Adding strings together using the ‘+‘ operator.
Concatenation
ex.:‘Data‘+‘Camp‘
DataCamp
What is the first element of a zero-indexing?
0
How can you count backwards when selecting elements near the end of a list?
You use negative indexes
ex.: x=[‘h‘, ‘b‘, ‘i‘, ‘k‘]
print(x[-1])
print(x[-3])
k
b
How can you make a list?
You use ‘[]‘.
l=[‘a‘, ‘b‘, ‘c‘]
print(l[0])
print(l[1])
a
b
How can you select multiple elements from a list?
Slicing: l[start:end]