1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Control Flow?
the order in which statements in a program (written in an imperative programming language, like Python) are executed
Control flow statement examples?
if statements, loop, (while, for)
What does EOL stand for?
End of line
print ("Please enter your first name")
print ("Hello", input())
Will this code execute as intended?
Yes
print ("Please enter the \"opposite\" value for the tangent")
opposite = input()
print ("Please enter the \"adjacent\" value for the tangent")
adjacent = input()
tangent = opposite / adjacent
print ("The tangent is ", tangent)
If the user types - in response to the prompts - the number 1, followed on the next prompt, by the number 2, the program will print: The tangent is 0.5, why or why note?
Inputs are strings. The program will fail with an error: TypeError: unsupported operand type(s) for /: 'str' and 'str'
To use them as numbers, you must first convert them.
print ("Please enter the \"opposite\" value for the tangent")
opposite = input()
print ("Please enter the \"adjacent\" value for the tangent")
adjacent = input()
tangent = int(opposite) / int(adjacent)
print ("The tangent is ", tangent)
What is the output of the following code?
for i in range(10, 15, 1):
print( i, end=', ')
10,11,12,13,14,
for num in range(6):
for i in range(num):
print (num, end=" ")
print("\n")
1
2
2
3
3
3
4
4
4
4
5
5
5
5
5
What is wrong with this code?
There is a TypeError
Python will interpret 1 as a callable object/function instead of an element. () are used for functions [] are used for elements in a list.
Explain why the last two answers are wrong
Python uses negative indexing, floats[-1]
refers to the last element (3.14
).floats[-2]
refers to the second-to-last element (-4.5
).
Why does the second line not produce a syntax error
print ("Please enter the \"opposite\" value for the tangent")
The \ escapes the quotes
Array indices start at
0
What is an iterable
In Python, an iterable is any object that can be looped over (e.g., in a for
loop) or passed to functions like iter()
or next()
. C
Iterable data example
string, list
For the code, nums = [3,4,5], what does nums[len(nums)] produce
3
What does this code fragment display?
for num in range(6):
print (num, end=" ")
print("\n")
0
1
2
3
4
5
Why does this code produce repeating twos?
There is no i +=1 so as a result i stays as one.
Is “““I love Ecor-1041””” valid?
Yes triple quotes are valid.
For a list of 50 items, the index of the last item is
49
Why does this produce an index error
because [len(nums)] will be out of range of num.
example:
nums = [10, 20, 30]
print(len(nums)) # Output: 3
Why does this code not work?
A value for i is not initialized, will produce the error ‘i’ is not defined.
Will "Hello" + "World" create a new string, what is it?
Yes it will create HelloWorld
T/F
Every time we modify a string, Python always creates a new string and assigns the modified string value to that new string.
True
Which expressions evaluate to false
( len( txt ) > 0 ) or ( limit - words < a )
words * a > limit
( a > limit ) and ( len( txt ) < words )
a == limit – words
not ( words < limit )
True, True, False, False, False
what will the code range (1, 5, 2) produce?
1, 3
What will the code range (1,2,5) produce
1
What will the code range (0, 5) produce?
0,1,2,3,4
T/F range () requires integer arguments only?
True
What does this code display?
number = 5
while number >= 0:
number -= 1
print (number)
4
3
2
1
0
print (num, end=" ")
What will this line print, what does the end function do
Will print num with a space at the end
The end function is used to customize what is printed at the end of an output.
What does this code fragment display?
for num in range(6):
for i in range(num):
print (num, end=" ")
print("")
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Which of the following Boolean expression(s) implement the following statement?
"x and y are not equal to zero, and they are either both positive or both negative"
1,2,3 apply
What does this print?
for num in range(6):
for i in range(num):
print (i, end=" ")
print("")
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
Type bool is
True or False (case sensitive)
What is a relational operator?
Relational operators in Python are used to compare two values or expressions. These operators evaluate the relationship between the operands and return a Boolean value: either True
or False
.
Do print functions show up in python visualizer?
no
1 |
|
2 |
|
3 |
|
4 |
|
Which will cause an error?
#4
Write a code fragment that sets a variable test
to True when the float variable x
is equal to 0.0 +/- 0.001
test=x<=0.001 and x>=-0.001
Sammy = "Sammy"
sammy = "sammy"
also_Sammy = "Sammy"
y= Sammy == sammy
x = Sammy == also_Sammy
Values of x and y?
True, False
Equal to symbol
==
Values of x y and z?
3, True, False
Why is nothing printed?
x = 4
y = 3
if x == 3:
print(1)
print(2)
if y == 3:
print(3)
print(4)
second if is indented
If there is an if/elif statement does it need to be followed by and else statement?
No
What is printed by the following code snippet?
x = 3
y = 4
if y == 3:
print(1)
print(2)
elif x == 3:
print(3)
print(4)
3
4
Why is this invalid?
Else can not have an argument
x = (1/3) == 0.3333
What is x?
False, floating point are not exact
What type of value will 70//5.0 return?
float
what type of value will 16 % 3.0 return?
float
Can a string be multiplied?
ex)
a = “a” * 2
Yes
What type of input does input() take?
Str