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
stop a loop
skip this iteration of the loop
break
continue
declare a function
def my_function():
print(“Hello from a function”)
Do an else and else if statement
if x > 0:
code
else x < 0:
code
elif:
code
do and or and not
and
or
not
just the words
What happens with this:
def square(x):
return x*x
print(square(7, 11))
error
too many arguments. same with too few
define a list
access an element
apple = [0, 1, 2]
print(apple[1])
get the length of a list
len([1, 2]) #2
concatenate a list
[1, 2] + [1, 2] # [1, 2, 1, 2]
duplicate list elements
[1, 2] * 3 # [1, 2, 1, 2, 1, 2]