Python Coding

studied byStudied by 0 people
0.0(0)
get a hint
hint

What Python statement would you like me to run?

1 / 99

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

Studying Progress

New cards
100
Still learning
0
Almost done
0
Mastered
0
100 Terms
0
New cards

What Python statement would you like me to run?

(Chapter 1) When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?

New cards
1
New cards

20

(Chapter 1) What will the following program print out:

>>> x = 15

>>> x = x + 5

>>> print x

New cards
2
New cards

.py

(Chapter 1) Python scripts (files) have names that end with:

New cards
3
New cards

for

(Chapter 1) Which of these words is a reserved word in Python ?

New cards
4
New cards

quit()

(Chapter 1) What is the proper way to say "good-bye" to Python?

New cards
5
New cards

Central Processing Unit

(Chapter 1) Which of the parts of a computer actually execute the program instructions?

New cards
6
New cards

A sequence of instructions in a programming language

(Chapter 1) What is "code" in the context of this course?

New cards
7
New cards

Secondary Memory

(Chapter 1) A USB memory stick is an example of which of the following components of computer architecture?

New cards
8
New cards

The computer did not understand the statement that you entered

(Chapter 1) What is the best way to think about a "Syntax Error" while programming?

New cards
9
New cards

Random steps

(Chapter 1) Which of the following is not one of the programming patterns covered in Chapter 1?

New cards
10
New cards

# This is a test

(Chapter 2) Which of the following is a comment in Python?

New cards
11
New cards

123abc

(Chapter 2) What does the following code print out?

New cards
12
New cards

hours

(Chapter 2) Which of the following variables is the "most mnemonic"?

New cards
13
New cards

stop

(Chapter 2) Which of the following variables is the "most mnemonic"?

New cards
14
New cards

Retrieve the current value for x, add two to it and put the sum back into x

(Chapter 2) What does the following statement do? x = x + 2

New cards
15
New cards

Parenthesis ()

(Chapter 2) Which of the following elements of a mathematical expression in Python is evaluated first?

New cards
16
New cards

2

(Chapter 2) What is the value of the following expression? 42 % 10

New cards
17
New cards

5

(Chapter 2) What is the value in x after the following statement executes: x = 1 + 2 * 3 - 8 / 4

New cards
18
New cards

98

(Chapter 2) What value be in the variable x when the following statement is executed?

x = int(98.6)

New cards
19
New cards

Pause the program and read data from the user

(Chapter 2) What does the Python raw_input() function do?

New cards
20
New cards

Indent the line below the if statement

(Chapter 3)

What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?

New cards
21
New cards

=

(Chapter 3) Which of these operators is not a comparison / logical operator?

New cards
22
New cards

Depending on the value of x, either all three of the print statements will execute or none of the statements will execute

(Chapter 3)What is true about the following code segment:

if x == 5 :

print 'Is 5'

print 'Is Still 5'

print 'Third 5'

New cards
23
New cards

You de-indent the next line past the if block to the same level of indent as the original if statement

(Chapter 3) When you have multiple lines in an if block, how do you indicate the end of the if block?

New cards
24
New cards

You have most likely mixed tabs and spaces in the file

(Chapter 3)

You look at the following text:

if x == 6 :

print 'Is 6'

print 'Is Still 6'

print 'Third 6'

It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?

New cards
25
New cards

except

(Chapter 3) What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?

New cards
26
New cards

Small

All done

(Chapter 3) What will the following code print out?

x = 0

if x < 2 :

print 'Small'

elif x < 10 :

print 'Medium'

else :

print 'LARGE'

print 'All done'

New cards
27
New cards

This code will never print 'Something else' regardless of the value for 'x'

(Chapter 3)

For the following code,

if x < 2 :

print 'Below 2'

elif x >= 2 :

print 'Two or more'

else :

print 'Something else'

What value of 'x' will cause 'Something else' to print out?

New cards
28
New cards

1

(Chapter 3) 'In the following code (numbers added) - which will be the last line to execute successfully?

(1) astr = 'Hello Bob'

(2) istr = int(astr)

(3) print 'First', istr

(4) astr = '123'

(5) istr = int(astr)

(6) print 'Second', istr

New cards
29
New cards

-1

(Chapter 3) For the following code:

astr = 'Hello Bob'

istr = 0

try:

istr = int(astr)

except: istr = -1

What will the value for istr after this code executes?

New cards
30
New cards

A built-in function

(Chapter 4) In Python what is the raw_input() feature best described as?

New cards
31
New cards

def

(Chapter 4) Which Python keyword indicates the start of a function definition?

New cards
32
New cards

10

20

(Chapter 4) What will the following Python code print out?

def func(x) :

print x

func(10)

func(20)

New cards
33
New cards

Bonjour Michael

(Chapter 4) What will the following Python

program print out?

def greet(lang):

if lang == 'es':

return 'Hola'

elif lang == 'fr':

return 'Bonjour'

else:

return 'Hello'

print greet('fr'),'Michael'

New cards
34
New cards

Avoiding writing the same non-trivial code more than once in your program

(Chapter 4) What is the most important benefit of writing your own functions?

New cards
35
New cards

x

(Chapter 4) In the following Python code, which of the following is an "argument" to a function?

x = 'banana'

y = max(x)

print y

print x

New cards
36
New cards

There

(Chapter 4)

What does the following code print out?

def thing():

print 'Hello'

print 'There'

New cards
37
New cards

You de-indent a line of code to the same indent level as the def keyword

(Chapter 4) In Python, how do you indicate the end of the block of code that makes up the function?

New cards
38
New cards

print 'World'

(Chapter 4)

Which line of the following Python program is useless?

def stuff():

print 'Hello'

return

print 'World'

stuff()

New cards
39
New cards

2

(Chapter 4)What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully).

def addtwo(a, b):

added = a + b

return a

x = addtwo(2, 7)

print x

New cards
40
New cards

Hellothere

(Chapter 6)

What does the following Python Program print out?

str1 = "Hello"

str2 = 'there'

bob = str1 + str2

print bob

New cards
41
New cards

42

(Chapter 6) What does the following Python program print out?

x = '40'

y = int(x) + 2

print y

New cards
42
New cards

printx[8]

(Chapter 6)

How would you use the index operator [] to print out the letter q from the following string?

x = 'From marquard@uct.ac.za'

New cards
43
New cards

print x[14:17]

(Chapter 6) How would you use string slicing [:] to print out 'uct' from the following string?

x = 'From marquard@uct.ac.za'

New cards
44
New cards

letter

(Chapter 6)

What is the iteration variable in the following Python code?

for letter in 'banana' :

print letter

New cards
45
New cards

42

(Chapter 6)

What does the following Python code print out?

print len('banana')*7

New cards
46
New cards

print greet.upper()

(Chapter 6) How would you print out the following variable in all upper case in Python?

greet = 'Hello Bob'

New cards
47
New cards

twist()

(Chapter 6) Which of the following is not a valid string method in Python?

New cards
48
New cards

.ma

(Chapter 6)

What will the following Python code print out?

data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'

pos = data.find('.')

print data[pos:pos+3]

New cards
49
New cards

strip()

(Chapter 6) Which of the following string methods removes whitespace from both the beginning and end of a string?

New cards
50
New cards

Collection variables can store multiple values in a single variable

(Chapter 8) How are "collection" variables different from normal variables?

New cards
51
New cards

print len(data)

(Chapter 8) Which of the following Python statements would print out the length of a list stored in the variable data?

New cards
52
New cards

A list of integers

(Chapter 8) What type of data is produced when you call the range() function?

x = range(5)

New cards
53
New cards

append()

(Chapter 8) What list method adds a new item to the end of an existing list?

New cards
54
New cards

for / in

(Chapter 8) What are the Python keywords used to construct a loop to iterate through a list?

New cards
55
New cards

print friends[2]

(Chapter 8) For the following list, how would you print out 'Sally'?

friends = [ 'Joseph', 'Glenn', 'Sally' ]

New cards
56
New cards

6

(Chapter 8) What does the following Python code print out?

a = [1, 2, 3]

b = [4, 5, 6]

c = a + b

print len(c)

New cards
57
New cards

Glenn

(Chapter 8)

What will the following Python code print out?

friends = [ 'Joseph', 'Glenn', 'Sally' ]

friends.sort()

print friends[0]

New cards
58
New cards

t[2:4]

(Chapter 8) Which of the following slicing operations will produce the list [12, 3]?

t = [9, 41, 12, 3, 74, 15]

New cards
59
New cards

Nothing would print - the program fails with a traceback

(Chapter 8) What would the following Python code print out?

fruit = 'Banana'

fruit[0] = 'b'

print fruit

New cards
60
New cards

Zap

ABC

Zap

(Midterm) What will the following Python program print out:

# define functions

def fred():

print "Zap"

def jane():

print "ABC"

# Main program

Zap = 42

ABC = 22

fred()

jane()

fred()

New cards
61
New cards

To make your program run faster

(Midterm) Which of the following is not a reason to use a function in your program

New cards
62
New cards

Jane returns!

Jane returns!

(Midterm) What will the following Python program print out:

def fred():

return "Fred returns!"

def jane():

return "Jane returns!"

print jane()

fred()

print jane()

New cards
63
New cards

h

e

l

l

o

(Midterm)

What will the following Python code print out?

i = 10

list1 = ['h','e','l','l','o']

for i in list1:

print i

New cards
64
New cards

The Main memory

(Midterm)

Where in the computer is a variable such as "x" stored?

x = 123

New cards
65
New cards

The program would show an error and a traceback on the second line

(Midterm) What would happen if the following Python code were executed?

st = "abc"

ix = int(st)

New cards
66
New cards

Parenthesis

(Midterm) If all of the following were used in a Python expression, which would have the highest precedence (i.e. which would be evaluated first)?

New cards
67
New cards

11 Hello

(Midterm) What will the following Python program print out? (This is a bit tricky so look carefully).

def hello():

print "Hello"

print "There"

x = 10

x = x + 1

print x, "Hello"

New cards
68
New cards

(5/9)

(Midterm) You develop the following program in Python:

f = int(raw_input("Enter:"))

c = ( f - 32 ) * ( 5 / 9 )

print "Celsius",c

And when you run it three times you get the following output:

Enter:212

Celsius 0

Enter:72

Celsius 0

Enter:15

Celsius 0

What part of the program is causing the output to always be zero?

New cards
69
New cards

12

(Midterm) For the following Python program, what will it print out?

x = 0

for value in [3, 41, 12, 9, 74, 10] :

if value < 10 :

x = x + value

print x

New cards
70
New cards

count = 1

(Midterm)

This Python program is supposed to compute the average of a list of numbers but the output is always wrong. Which line is in error?

total = 0

count = 0

for abc in [3, 41, 12, 9, 74, 15] :

total = total + abc

count = 1

ave = total / count

print ave

New cards
71
New cards

-1

(Midterm) For the following Python program, what will it print out?

x = -1

for value in [3, 41, 12, 9, 74, 15] :

if value < x :

x = value

print x

New cards
72
New cards

Bonjour

(Midterm)

What is the output of the following program, if the user input is: fr?

# define function 'greet'

def greet(lang):

if lang == 'es': # if lang is Spanish

print 'Hola'

return 'adios'

elif lang == 'fr': # French

print 'Bonjour'

return 'au revoir'

else:

print 'Hello'

return 'bye'

xx = raw_input('enter language: ')

#----main program begins here---

yy= greet(xx)

New cards
73
New cards

hello

yo

Done!

(Midterm) What is the output of the following Python code, given the following inputs?

hello

# hi

yo

done

# Python code begins here

while True:

line = raw_input('>')

if line[0] == '#' :

continue

if line == 'done':

break

print line

print 'Done!'

New cards
74
New cards

The program will run indefinitely

(Midterm) What will happen if this code is run?

n = 5

while True:

print n

n = n - 1

print 'Done!'

New cards
75
New cards

5

4

3

Done!

(Midterm) What is the output of this code?

n = 5

while True:

if n < 3: break

print n

n = n - 1

print 'Done!'

New cards
76
New cards

5 3 1 Done

(Midterm)

What will this code output? (note the comma after a print statement)

n = 5

while n > 0:

print n,

n = n - 2

print 'Done!'

New cards
77
New cards

6

5

4

3

Done!

(Midterm) What is the output of this code?

n = 6

while True:

print n

if n == 3: break

n = n - 1

print 'Done!'

New cards
78
New cards

6

5

2

1

Done!

(Midterm) What is the output of this code? (note the 'continue')

n = 6

while n > 0:

if n in [3,4]: # this is a list, where n is checked for either 3 or 4

n = n-1

continue

print n

n = n - 1

print 'Done!'

New cards
79
New cards

2

(Midterm) What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully).

def addtwo(a, b):

added = a + b

return a

x = addtwo(2, 7)

print x

New cards
80
New cards

This loop will run forever

(Chapter 5) What is wrong with this Python loop:

n = 5

while n > 0 :

print n

print 'All done'

New cards
81
New cards

Exits the currently executing loop

(Chapter 5) What does the break statement do?

New cards
82
New cards

Jumps to the "top" of the loop and starts the next iteration

(Chapter 5) What does the continue statement do?

New cards
83
New cards

5

(Chapter 5)

What does the following Python program print out?

tot = 0

for i in [5, 4, 3, 2, 1] :

tot = tot + 1

print tot

New cards
84
New cards

friend

(Chapter 5) What is the iteration variable in the following Python code:

friends = ['Joseph', 'Glenn', 'Sally']

for friend in friends :

print 'Happy New Year:', friend

print 'Done!'

New cards
85
New cards

Sum all the elements of a list

(Chapter 5)

What is a good description of the following bit of Python code?

zork = 0

for thing in [9, 41, 12, 3, 74, 15] :

zork = zork + thing

print 'After', zork

New cards
86
New cards

-1

(Chapter 5) What will the following code print out?

smallest_so_far = -1

for the_num in [9, 41, 12, 3, 74, 15] :

if the_num < smallest_so_far :

smallest_so_far = the_num

print smallest_so_far

Hint: This is a trick question and most would say this code has a bug - so read carefully

New cards
87
New cards

matches both type and value

(Chapter 5) What is a good statement to describe the is operator as used in the following if statement:

if smallest is None :

smallest = value

New cards
88
New cards

while

(Chapter 5) Which reserved word indicates the start of an "indefinite" loop in Python?

New cards
89
New cards

0

(Chapter 5) How many times will the body of the following loop be executed?

n = 0

while n > 0 :

print 'Lather'

print 'Rinse'

print 'Dry off!'

New cards
90
New cards

Python lists maintain order and dictionaries do not maintain order

(Chapter 9) How are Python dictionaries different from Python lists?

New cards
91
New cards

Traceback error

(Chapter 9) What would the following Python code print out?

stuff = dict()

print stuff['candy']

New cards
92
New cards

Associative arrays

(Chapter 9) What is a term commonly used to describe the Python dictionary feature in other programming languages?

New cards
93
New cards

What is a common use of Python dictionaries in a program?

(Chapter 9) Building a histogram counting the occurrences of various strings in a file

New cards
94
New cards

counts[key] = counts.get(key,0) + 1

(Chapter 9) Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?

if key in counts:

counts[key] = counts[key] + 1

else:

counts[key] = 1

New cards
95
New cards

It loops through the keys in the dictionary

(Chapter 9)

In the following Python, what does the for loop iterate through?

x = dict()

...

for y in x :

...

New cards
96
New cards

values()

(Chapter 9) Which method in a dictionary object gives you a list of the values in the dictionary?

New cards
97
New cards

To provide a default value if the key is not found

(Chapter 9) What is the purpose of the second parameter of the get() method for Python dictionaries?

New cards
98
New cards

-1

(Chapter 9) What would the following Python code print out?

stuff = dict()

print stuff.get('candy',-1)

New cards
99
New cards

False

(Chapter 9) TRUE or FALSE: When you add items to a dictionary they remain in the order in which you added them.

New cards

Explore top notes

note Note
studied byStudied by 36 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 25 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 15 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 32 people
Updated ... ago
5.0 Stars(2)
note Note
studied byStudied by 53 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 41 people
Updated ... ago
5.0 Stars(2)

Explore top flashcards

flashcards Flashcard30 terms
studied byStudied by 4 people
Updated ... ago
5.0 Stars(4)
flashcards Flashcard94 terms
studied byStudied by 160 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard40 terms
studied byStudied by 132 people
Updated ... ago
4.3 Stars(7)