Code for PCEP

Ace your homework & exams now with Quizwiz!

def hi(): return print("Hi!") hi()

Nothing gets outputted b/c theres nothing attached to the return statement

dict.vals() does not exist

True

var = 1 # First iteration, 1 < 10, "#" is printed # THEN var is incremented # var << 1 = var * 2^1 = 2 # Second iteration, 2<10, "#" is printed # 2*2^1 = 4 # Third iteration, 4 < 10, "#" is printed # 4 * 2^1 = 8 # Fourth iteration, 8 < 10, "#" is printed # 8 * 2^1 = 16 # 16 > 10, loop stops, 4 "#"s printed while var < 10: ...print("#") var = var << 1

# # # #

for i in range(1): print("#") else: print("#")

# # else is always executed at least once for first time not in range

foo = (1,2,3) foo.index(3)

# .index outputs the index of a value # The value 3, has an index of 2 in the list # if you enter a value that's not in list/tuple you get ValueError

i = 0 while i <= 3: i += 2 # On first iteration, i becomes 2 print("*") # 2 is less than 3 so the loop prints another "*" # Because 2+2 > 3, the loop stops and only 2 "*"s are printed

* *

i = 0 while i <= 5: i = i + 1 if i % 2 == 0: break print("*")

* loop breaks in second iteration because i gets incremented twice

def fun(x,y): ...if x == y: ......return x ...else: ......return fun(x,y-1) print(fun(0,3))

0

x = int(input()) # Enter 11 y = int(input()) # Enter 4 x = x%y # 3 x = x%y # 3, if x < y in x % y, then result is just x y = y%x # 4 % 3 = 1

1

1 % 2

1, if left is < right, answer is always number on the left

x = 1 y = 2 z = x # z = x = 1 x = y # x = 2 y = z # z is still equal to 1 print(x,y)

2 1

dct = {} dct['1'] = (1,2) dct['2'] = (2,1) for x in dct.keys(): print(dct[x][1], end="")

21

def any(): print(var + 1, end='') var = 1 any() print(var)

21, v is defined as 1 outside the function

def fun(x): global y y = x * x return y fun(2) print(y)

4

# Example 1 def wishes(): print("My Wishes") return "Happy Birthday" wishes() # outputs: My Wishes # Example 2 def wishes(): print("My Wishes") return "Happy Birthday" print(wishes()) # outputs: My Wishes # Happy Birthday

My Wishes My Wishes Happy Birthday

def fun(x): if x % 2 == 0: return 1 else: return print(fun(fun(2)) + 1)

Returns TypeError because fun(fun(2)) returns a None variable and you can't add None to 1

nums = [1,2,3] vals=nums del vals[:] # deletes all elements in nums AND vals bc they are the same list print(vals)

[ ]

list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2 print(list_3)

['B', 'C']

my_list = [1, 2, 3] for v in range(len(my_list)): my_list.insert(1, my_list[v]) print(my_list) # First iteration: my_list[v] is my_list[0], which is 1 # my_list becomes [1, 1, 2, 3] # This means that my_list[1] = 1 # # my_list becomes [1, 1, 1, 2, 3]

[1, 1, 1, 1, 2, 3]

my_list = [1,2] for v in range(2): my_list.insert(-1, my_list[v]) print(my_list)

[1, 1, 1, 2] # insert puts in the element right before the specified position.

vals = [0, 1, 2] vals[0], vals[1] = vals[1], vals[2] # index 0 becomes index 1 (0 --> 1) # index 1 becomes index 2 (1 --> 2) print(vals)

[1, 2, 2]

nums = [1, 2, 3] vals = nums del vals[1:2] print(nums)

[1, 3]

my_list = [3,1,-2] print(my_list[my_list[-1]])

my_list[-1] = -2, this serves as the index my_list[-2] = 1

t = [[3-i for i in range(3)] for j in range(3)] s = 0 for i in range(3): s = s + t[i][i] print(s)

t = [ [ 3, 2, 1 ], [ 3, 2, 1 ], [ 3, 2, 1 ] ] t[0][0] = 3 t[1][1] = 2 t[2][2] = 1 s = 0 + 3 + 2 + 1 = 6

dictionary = {"one":"two", "three":"one", "two":"three"} v = dictionary["one"] # = two for k in range(len(dictionary)): ...v = dictionary[v] print(v) # v is defined outside of the function

two


Related study sets

Lecture 21: Sexual selection, Genetic Drift, Gene Flow, Quiz Q's

View Set

PSYC151 Unit 5 (Chpt. 11, 14, and 15)

View Set

U.S. Senator And House Representative

View Set

Recapitulación - Lección 1, 2, 3, 4, 5 (in order), Spanish 2: Chapter 3 Review, *****Aventuras Lesson 3 Family vocab*****, Leccion 4 exam study guide, Estar (to be), interrogative words, School items -- Spanish terms

View Set

Final Exam - Pennsylvania Life Only

View Set