
Python Review 2
Presentation
•
Computers
•
12th Grade
•
Practice Problem
•
Medium
Catherine L Hulcher
Used 4+ times
FREE Resource
12 Slides • 13 Questions
1
total = input('Enter a number: ')
-total is a variable name storing a variable type.
-The input() function always reads the user’s input as a string.
-The value in total is a string
-you cannot divide a string by an integer (total/4)
-strings are stored in memory as a sequence of characters
-line 2 is trying to divide a string (total) by 4 (an integer) ERROR
-you have to convert the value in total to an integer
2
Multiple Choice
On which line will this program call an error in the console?
Line 1
Line 2
Line 3
Line 4
3
-line 2 is trying to divide a string (total) by 4 (an integer) ERROR
-you have to convert the value in total to an integer
-total = int(input('Enter a number: '))
-the int function will change the value to an integer.
-line 3 will also produce an ERROR.
-python does not allow concatenation of a str and an int
-Line 3: print('One quarter of ' + str(total))
-String concatenation = Use the + character to add a variable to another variable
This code will produce 2 runtime errors.
4
Multiple Choice
How do you fix this code?
5
A. no error. Converts input to integer. Converts integer back to string.
Which will produce an error?
B. no error. Converts input to integer. Converts integer back to string.
C. no error. Converts input to integer. Prints an integer (no concatenation).
D. Error. Converts input to integer. Tries to concate an integer (1) with a string.
E. no error. Converts input to integer. Converts integer back to string
6
Multiple Choice
Which line will produce an error?
Line A
Line B
Line C
Line D
Line E
7
A. Error. Converts input to integer. Does not convert integer(dozen) back to string.
Which of the following code snippets will not produce an error, assuming the user inputs an integer?
B. No Error. Converts input to integer. Converts both dozen and 1 back to string.(Notice parentheses.)
C.Error. Converts input to integer. Adds to integers but does not convert to string for the concatenation
D. Error. Inputs a string. Tries to add a string and integer.
E. Error. Converts input to integer. Converts integer back to string but adds an one
8
Multiple Choice
Which of the following code snippets will not produce an error, assuming the user inputs an integer?
9
In Python, you can use the * operator to multiply not only numbers but also lists and strings. When you multiply a list or a string by an integer, Python repeats the content of that list or string the specified number of times.
The type() function returns the type of the specified object.
10
Multiple Choice
What will be printed to the screen when the following code snippet is run?
11
1. It must start with a letter or the underscore (_).
2. It should not start with a number.
3. It should only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). It should not have any other characters or spaces.
4. The names are case-sensitive (i.e., Pythongeeks and PythonGeeks are treated as different variables).
5. It should not be a keyword.
12
Multiple Choice
Which of the following cannot be used as a variable name in a Python program?
one_word
oneWord
1word
word1
13
Integer Division in Python
In Python, integer division, divides the numbers and returns a float.
7/3 = 2
9/3 = 3
9/6 = 1.5
8/3 =2.66666
15.0/6.0 = 2.5
14
Multiple Choice
Which of the following equations will return the lowest value?
print(9/6)
print (4 / 2)
print (15.0 / 6.0)
print (8 / 3)
15
Integer Division in Python
In Python, // integer division, also referred to as floor division, involves discarding the remainder and returning the whole number part of the quotient.
7//3 = 2
9//3 = 3
9//6 = 1
8//3 =2
15.0//6.0 = 2.0
16
Multiple Choice
Given the assigned values of x and y, which of the following expressions will produce the smallest value?
x = 25 y = 22
x % y
x / y
x // y
x * y
17
The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
Modulo Operator %
18
Multiple Choice
Which of the following code snippets will not print 3.0 to the screen?
19
Multiple Choice
What will calculate be set to when the following program is run?
calculate = 24 * 2 + 8 2 - 4
24
60
46
26
20
Operators in Python are applied in this order:
Operations in parentheses are resolved first, moving from left to right.
** is resolved second, moving from left to right
*, /, // and % are resolved third, moving from left to right.
+ and - are resolved fourth, moving from left to right.
You might recall the “PEMDAS” acronym for remembering the order of operations in math, and thankfully it still applies here.
21
Multiple Choice
What does the modulus operator return?
The rounded quotient when one number is divided by another.
The truncated quotient when one number is divided by another.
The answer when one number is raised to the power of another.
The remainder when one number is divided by another.
22
A. No Error. Converts 5 to string. Y is a string. Prints both strings
Which of the following code snippets will produce an error?
B. No Error. 5 is an integer. 4 is converted to an integer. Thus adds two integers.
C. No Error. Concatenates two strings. Puts 5 and 4 together making 54.
D. No Error. multiplies a string 5 times.
E. Error. Cannot multiply two strings.
23
Multiple Choice
Which of the following code snippets will produce an error?
24
Method #1: Commenting Using Multiple Single Line #
Method #2: Commenting Using Triple-Quoted String Literals
An alternative method for commenting out multiple lines is to use triple-quoted string literals (' ' ' ' ' ' or " " " " " ").
25
Multiple Choice
Which of the following will result in an error?
total = input('Enter a number: ')
-total is a variable name storing a variable type.
-The input() function always reads the user’s input as a string.
-The value in total is a string
-you cannot divide a string by an integer (total/4)
-strings are stored in memory as a sequence of characters
-line 2 is trying to divide a string (total) by 4 (an integer) ERROR
-you have to convert the value in total to an integer
Show answer
Auto Play
Slide 1 / 25
SLIDE
Similar Resources on Wayground
20 questions
ICT in the Home part 1
Presentation
•
12th Grade
21 questions
The Loop Structure
Presentation
•
11th Grade
19 questions
Job Application Letter
Presentation
•
12th Grade
19 questions
Logarithms and Exponential Functions
Presentation
•
12th Grade
22 questions
Mastering Email's By Mr. B
Presentation
•
12th Grade
18 questions
Perfect Verb Tenses
Presentation
•
KG
18 questions
Discussion Text
Presentation
•
12th Grade
21 questions
Redistricting and Gerrymandering
Presentation
•
12th Grade
Popular Resources on Wayground
19 questions
Naming Polygons
Quiz
•
3rd Grade
10 questions
Prime Factorization
Quiz
•
6th Grade
20 questions
Math Review
Quiz
•
3rd Grade
15 questions
Fast food
Quiz
•
7th Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
20 questions
Inferences
Quiz
•
4th Grade
19 questions
Classifying Quadrilaterals
Quiz
•
3rd Grade