Professional Documents
Culture Documents
Module:
Import random
print (“newlines”)
string.find() – will return the len where the insert is in the string
Lists:
Grocery_list = [‘Juice’, ‘Tomates’, ‘Potatoes’, ‘Bananas’]
print(grocery_list[1:3])
>>>[‘Tomatoes’, ‘Potatoes’]
Tuples:
Similar to list, but will not be able to change tuple about making it
Print vs return:
print(“Hello World”)
def f_r(x):
return x + 3 **interactive
def f_p(x):
print(x+3) **
Boolean expressions:
Integer
Float
Type conversion:
Int(x) --- makes number into integer
List(“”)
Function design:
Def addNumber(fNum, lNum):
return Num
Docstrings:
‘’’ ‘’’
If statements:
If elif else ==, !=, >, <, >=, <= --- will execute code when the statements apply
Age = 21
Else:
Once a condition is made, the function will stop there, and will not continue on
For loops:
I want to perform something 10 times:
For x in range(0,10):
For x in range(0,3):
For y in range(0,3):
While loops:
Use when you don’t know how many times you will need to use I t
__ random.randrange(0,100)
print(random_num)
random_num = random.randrange(0,100)
I = 100
While (1 <=20):
If (i%2==0):
Print(i)
Elif( I ==9):
Break – if we want to go out of the loop completely, not check anymore, break doesit
Else:
Test_file.close()
including slicing, looping over elements of the list, looping over indices
in the list
Alias: something is a alias when they have the same memory address. U make the list == the
other list
Aliasing is one of th reasons why the notion of mutability is important. Ex) id u make changes to
list x, the same changes should be seen in list y --- this can create lots of hard to find errors ,
however this cant happen to immutable objects like strings , because a string can have be
changes after it is created.
Uses {} instead of []
print
range
append
len
'in' operator
The following functions appear in at least one function. More sure you
know what these functions do.
input
sorted - returns a copy of the list where the items are in order from smallest
to largest – doesn’t mutate L
math.floor - math.floor(x) – returns the floor of x as an Integral – this is the
largest integer <= x
math.log – returns the log of x to the given base log(x,base)
.isupper (from str module) -- returns true if all characters in the string are
uppercase
***************
Finally here some type of questions that appear frequently in the
midterm:
etc