ITI Midterm 1 – Study Guide
However make sure you know range function. It is on of the keys to
the loops in python.
Module:
Import random
Arithmetic expressions in python (including +,-,*, /,
exponentiation **, integer division //, mod %, ..) :
+, -, *, /, %, **, // (2.5 – 2)
order of operations matters – BEDMAS – use brackets
Strings (including + operator being concatenation on strings):
string (“djfsdkf”)
quote = “live, love, laugh”
quote2 = quote + quote
print(“I don’t like”, end= “”)
print (“newlines”)
>>> I don’t like newlines
new lines = print(“\n” * 7)
string.capitalize() -- will capitalize string
string.find() – will return the len where the insert is in the string
string.isaplha() – string is all letter
.replace—will replace letter
.strip – take away white space
Lists:
Grocery_list = [‘Juice’, ‘Tomates’, ‘Potatoes’, ‘Bananas’]
Print(‘First Item’, grocery_list[0])
>>> First Item Juice
print(grocery_list[1:3])
>>>[‘Tomatoes’, ‘Potatoes’]
grocery_list.append(‘Onions’) --adds onions to the list
grocery_list.insert(1, “pickle”) - will add pickle in 1 position
grocery_list.remove(“pickle”) - will remove pickle
gorcery_list.sort() -- sorts list
grocery_list.reverse() --- will reverse the sort
print(len(list)) - gives the length of the items in the list
print(max(grocery_list)) --- last in terms of alphabetically order bc strings
print(min(grocery_list)) --- first in alphabetically order
Tuples:
Similar to list, but will not be able to change tuple about making it
Pi_tuple = (3,1,4,1,5,9) - round bracket instead of [] for lists
New_tuple = list(pi_tuple) - converts tuple into a list
New_list = tuple(new_tuple) - coverts list into tuple
Len(tuple) – find length the tuple
Min and max will used in same as lists
Print vs return:
print(“Hello World”)
print() doesn’t return anything
return doesn’t print anything
def f_r(x):
return x + 3 **interactive
def f_p(x):
print(x+3) **
Boolean expressions:
Data types in python:
String
Integer
Float
Type conversion:
Int(x) --- makes number into integer
Float(36) -- 36.0 - makes it a float
Str(x) --- makes it a string
Tuple(“”)
List(“”)
Function design:
Def addNumber(fNum, lNum):
sumNum = fNum = lNum
return Num
Docstrings:
‘’’ ‘’’
If statements:
If elif else ==, !=, >, <, >=, <= --- will execute code when the statements apply
Age = 21
If age > 16:
Print(“You are old enough to drive”)
Else:
Print(“You are not old enough to drive”)
If ((age =1) and (age<=18)): ** they both have to be true
If …. Or …. ** only one or the conditions has not apply
If … not …. : ***does the opposite of what is before it
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):
Print(x, ‘ ‘. End = “”)
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)
while random_sum != 15:
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:
I += 1 ( same thing as I = i+1 )
Continue -- while go back to while loop
File:
Test_file = open(“text.txt, “wb”)
Test_file.close()
including slicing, looping over elements of the list, looping over indices
in the list
Mutable vs immutable objects (i.e. variables that refer to
immutable objects like strings, numbers and tuples vs variables
that refer to mutable objects like lists:
Mutable : Lists - can be changes after created
Immutable: strings. Numbers, tuples - cannot be changes after created
Copy vs aliasing (what is happening in the memory):
Copying: when you copy a list, you create a clone of the list. The lists have the same items, but
the lists themselves are different objects at different memory addresses.
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.
- LOGIC/ALGORITHMS/SOLVING COMPUTATIONAL PROBLEMS
(understanding what a program/function does, writing programs etc)
including solving problems on all of the above data types: lists, strings,
... then solving problems with accumulator variables etc ...
- counting how many times something is printed
Dictionaries:
you cant join them together like you can with lists
Uses {} instead of []
The following functions (operators) appear in many of questions. Make
sure you know everything we have seen about these functions.
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:
What does the following code fragment print
or
Which of the options solves the problem correctly?
or
Which of the following algorithm(s) can be used to implement/solve
this problem?
or
What does the following function do?
or
Which of the following functions correctly computes ...
etc