U of W Bits Flashcards
log something
print(‘something’)
make a variable
variable = 123
get the type of something
type(‘thing’) # string
make a comment
number sign
> #this is a comment
What are the types in python?
what do we call arrays and objects in python?
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
object = dictionary
array = list
convert types
str(123)
float(123) #123.0
int(123.7) #123 (just removes the decimal)
get input
input()
get the length of a string
len(‘123’) #3
what happens with this with this if you input a number like 32?
if input() > 10:
print(‘yes’)
error. It does not implicitly convert
have to do:
if int(input()) > 0:
print(‘yes’)
slice a bit of a string
‘01234’[0:2] # ‘01’
concatenate a string
‘01’ + ‘23’ # ‘0123’
Difference between:
result = ‘one’ + 2
print(result)
and
result = 1 + ‘two’
print(result)
both will fail. have to explicitly convert
repeat a string
‘two’ * 3 # ‘twotwotwo’
initiate a loop (while loop)
counter = 0
while counter < 10:
print(‘hello’)
counter = counter + 1
initiate a loop with the conditions applied (for loop)
1 for counter in range(10, 15):
2 print(“counter is”, counter)
counter is 10
counter is 11
counter is 12
counter is 13
counter is 14