Intro to Programing Final Exam

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Trace the output def myfun(a): print (a * '#') x = "1-2-3-4-5" y = x.split("-") for z in y: myfun(int(z))

# ## ### #### #####

What will be stored in the file 'foobar.txt' myfile = open("foobar.txt", "w") for i in range(1, 5): myfile.write("#" * i + "\n") myfile.close() myfile2 = open("foobar.txt", "a") x = 0 while x < 3: myfile2.write(".") x += 1 myfile2.close()

# ## ### #### ...

Trace the output x = [1,2,3] counter = 0 while counter < len(x): print (x[counter] * '%') for y in x: print (y * '*') counter += 1

% * ** *** %% * ** *** %%% * ** ***

'a'*5

'aaaaa'

5 * 'a' + 5 * 'b'

'aaaaabbbbb'

'a'+'b'

'ab'

'cat' + 'dog'

'catdog'

Trace the output x = 10 y = 5 for i in range(x-y*2): print ("%", i)

(nothing)

Trace the output for z in range(-500,500,100): print (z)

-500 -400 -300 -200 -100 0 100 200 300 400

99 % 3

0

Trace the output colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue'] for i in range(0, len(colors), 2): print(i, colors[i])

0 Red 2 Yellow 4 Blue

Trace the output pokemon = ["squirtle", "pikachu", "charmander", "bulbasaur", "meowth"] for i in range(0, len(pokemon), 2): print (i, pokemon[i]) # rewrite counter = 0 while counter < len(pokemon): print (counter, pokemon[counter]) counter += 2

0 squirtle 2 charmander 4 meowth

Trace the output for x in [1,2,3,4,5]: print (x)

1 2 3 4 5

Trace the output for p in range(1,10): print (p)

1 2 3 4 5 6 7 8 9

Trace the output foo = { "apple":1.99, "pear":2.99, "peach":3.99 } bar = [ "apple", "starfruit", "orange", "pear", "PEACH", "banana" ] for i in range(len(bar)): print (i+1, bar[i], ": ", end="") if bar[i] in foo: print (foo[ bar[i] ]) else: print ("?")

1 apple : 1.99 2 starfruit : ? 3 orange : ? 4 pear : 2.99 5 PEACH : ? 6 banana : ?

Trace the output x = 10 y = 0 while x > y: print (x, y) x = x - 1 y = y + 1

10 0 9 1 8 2 7 3 6 4

Trace the output keepgoing = True x = 100 while keepgoing: print (x) x = x - 10 if x < 50: keepgoing = False

100 90 80 70 60 50

Trace the output class Data: def __init__(self, b, c, d): self.__a = 2 self.__b = b self.__c = c self.__d = d def foo(self): return self.__a, self.__b, self.__c, self.__d def bar(self, e): self.__d += e def main(): x = {'!':1, '#':3, '%':5, '&':7, '+':9, '~': 0} y = Data(x['~'], x['!'], x['&']) y.bar(1) for i in y.foo(): print(i, end='') main()

2018

Trace the output code = {"#":"foo", "@":"bar", "!":"hello", "&":"world"} for i in range(33, 40): if chr(i) in code: print(i, chr(i), code[chr(i)]) else: print(i, chr(i))

33 ! hello 34 " 35 # foo 36 $ 37 % 38 & world 39 '

Trace the output x = 45 while x < 50: print (x)

45 45 45 45 . . .

Trace the output def foo(a): if a.isdigit(): print(a) data1 = "5,10,15" for item in data1: foo(item) print("---") data2 = data1.split(",") for item in data2: foo(item)

5 1 0 1 5 --- 5 10 15

Trace the output c = 0 for x in range(10): for y in range(5): c += 1 print (c)

50

5 + (10 % 3)

6

Trace the output word = "xyz123!@#" for i in range(len(word)-1, -2, -1): print(i, word[i], end='') if word[i].isdigit(): for j in range(int(word[i])): print("*", end='') print()

8 # 7 @ 6 ! 5 3*** 4 2** 3 1* 2 z 1 y 0 x -1 #

(True or False) and (False and True)

False

100>5 and 100<99

False

Trace the output count = 0 while count < 10: print ('Hello') count += 1

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello

Trace the output for x in 'lamp': print (str.upper(x))

L A M P

Trace the output votes = { "NJ":[1478749,2126610,45774], "NM":[35788,415335,32634], "NY":[2490496,4485877,105163] } r = 0 d = 0 o = 0 for key in votes.keys(): r += votes[key][0] d += votes[key][1] o += votes[key][2] print ("Republican:", r) print ("Democrat:", d) print ("Other:", o)

Republican: 4005033 Democrat: 7027822 Other: 183571

(99 x 2) < 200 or (100 > 50 x 2)

True

1 > 2 or 2 > 1

True

100 > 5

True

True or False or False or True

True

not (1 > 2) and not (3 < 3)

True

['a', 'b'] + ['c', 'd']

['a', 'b', 'c', 'd']

[1,2,3] + 3 * [4]

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

[1] + [2]

[1, 2]

[2] * 4 + [4] * 2

[2, 2, 2, 2, 4, 4]

runtime error

an error that occurs while a program is running

Trace the output x = 'apple,pear,peach,grapefruit' y = x.split(',') for z in y: if z < 'm': print (str.lower(z)) else: print (str.upper(z))

apple PEAR PEACH grapefruit

Trace the output x = "apple,pear,peach" y = x.split(",") for z in y: print (z)

apple pear peach

Trace the output code = { 6:["t", "g"], 14:["p","q"], 15:["c", "d"], 9:["o", "a"] } data = "5,0:3,1:2,0" splitdata = data.split(":") for item in splitdata: splititem = item.split(",") print(code[int(splititem[0]) * 3][int(splititem[1])], end='')

cat

Rewrite this program using a while loop colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue'] for i in range(0, len(colors), 2): print(i, colors[i])

colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue'] i = 0 while i < len(colors): print(i, colors[i]) i += 2

how do you delete all the items in your dictionary?

d.clear( )

how do you duplicate your dictionary?

d.copy( )

how do you print out a particular value in your dictionary?

d.get(key)

how do you print out the items in a dictionary?

d.items( )

how do you print out the keys in a dictionary?

d.keys( )

how do you print out and delete a particular value in your dictionary?

d.pop(key)

how do you print out and delete the last item in your dictionary?

d.popitem( )

how do you print out the values in a dictionary?

d.values( )

ord('a')

gets the Unicode number for the character 'a'

chr( )

gets the character for any Unicode number

what type of data structure is [1, 2.0, 'three']

list

how do you add items to your list?

my_list.append( )

how do you count the number of times a particular item is in your list?

my_list.count(x)

how do you find where the position a particular item is in your list?

my_list.index(x)

how do you add another item to your list in a specific place?

my_list.insert(i,x)

how do you collect and remove a specific item in your list?

my_list.pop(i)

how do you take a specific item out of your list?

my_list.remove(x)

how do you order the items in your list?

my_list.sort( )

if x='python' what is x[-1]?

n

Trace the output x = 'one' y = 'two' counter = 0 while counter < len(x): print (x[counter], y[counter]) counter+=1

o t n w e o

how do you open a file in read mode?

open(file_name, 'r')

how do you open a file in write mode?

open(file_name, 'w')

Trace the output pokemon = { "pikachu":["electricity",100], "charmander":["fire",90], "squirtle":["water",110] } for key in pokemon.keys(): print (key) for i in range(len(pokemon[key])): print ("*", pokemon[key][i])

pikachu * electricity * 100 charmander * fire * 90 squirtle * water * 110

Trace the output code = { "a":"o", "b":"e", "c":"p", "d":"t", "e":"r" } data = "2tzczy6a8i_d!@55qd*88bopse" for d in data: if d in code: print(code[d], end="")

potter

slicing strings

s[begin:end]

if datafile.txt is snow snowflake sled and sledding what will be stored? file_object = open("datafile.txt", "r") data = file_object.read() file_object.close() x = data.split("\n") file_object_2 = open("datafile.txt", "w") for i in x: if len(i) % 2 == 0: file_object_2.write(i + "!\n") else: file_object_2.write(i + "?\n") file_object_2.close()

snow! snowflake? sled! sledding!

if x='python' what is x[-4]?

t

what is the general format for an exception handler?

try: statement1 statement2 except: statement1 statement2 else: statement1 statement2

how do you read from the web?

urllib.request

if x='python' what is x[1]?

y


संबंधित स्टडी सेट्स

4.1 Learning Through Classical Conditioning

View Set

BIOLOGY CH. 17 Gene Expression: From Gene to Protein

View Set

Exam 3 Lymphatic system & immunity

View Set

Anatomy and Physiology - Blood : White Blood Cells

View Set

Nurse Leadership and Management Final

View Set

Executive ap gov test multiple choice

View Set

Healthcare Statistics Chapters 1-7 Midterm

View Set