Exam 1 Flashcards
What is wrong with the code snippet below?
print(Hello world)
Missing “ “ around Hello world
True or False: Python automatically inserts a newline character when you use the print command
True
Single line comment
#
Multiple line comment
’’’
What does the print statement do?
Sends your output to the screen
Which statement will print Hi! without a newline character?
print(“Hi!” , end= ‘ ‘)
How many arguments are in each of the following calls to the print function?
print(end= ‘ ‘)
print()
print(“Hi”,”there”)
print(“Hi there”)
print(“The quick brown fox” , “jumps” , “over the lazy” , “dog”, end= ‘\n’)
10215
True or False: the two names below refer to the same variable
My_Variable
my_variable
False
Declare the variable my_variable and assign it the value of “red” and assign another variable to overwrite this with “blue”
my_variable = “red”
my_variable = “blue”
What is the rule regarding boolean values in Python?
Boolean values in Python are case sensitive. They must start with a capital letter.
Strings
Collections of letters, numbers, and symbols
Boolean
True or False
Ints
Integers (no decimals)
Floats
Numbers with a decimal
Select the ways that a string can be declared
my_string = “I am a string”
my_string = “I am a string’
my_string = ‘“I am a string”’
my_string = ‘I am a string’
my_string = “I am a string”
my_string = ‘“I am a string”’
my_string = ‘I am a string’
What are the rules for variable names in Python?
You cannot use a keyword
The variable name must start with a letter or an underscore
The rest of the variable name can only be letters, numbers, or an underscore
Declare a variable my_boolean and assign it the boolean value of true. Print the variable
my_boolean = True
print(my_boolean)
Use two print statements to write the following string:
Okay, it is time to learn about operators
print(“Okay, “, end= ‘ ‘)
print(“it is time to learn about operators.”)
What happens when you add an int and a float?
You get a float
Select the line of code below that increments the variable by 1
a = a +1
In the following line of code, what value is stored in variable b when the program ends?
a = 5
b = 3
c = a + b
a = 2
b = c
8
True or False: The / operator sometimes returns an integer
False
True or False: The // operator always returns a whole number value
True
What does modulo do?
Returns the remainder after division is performed
Which operation should be performed first in the following expression?
7 / 2 + (5 % 2 * 5) ** 2 - 3
5 % 2
Which operators can be used with strings?
- and +
Which data type is used by a conditional to know which part of the code should be executed?
Boolean
Why might you want to use a compound conditional?
Compound conditionals allow you to test two or more things to be true, and they can make your code more concise and easy to read
Why might you NOT want to use a compound conditional?
When you have conditions that depend on the outcome of other conditions and with several variables, constructing a single boolean expression that is correct is difficult and hard to read and understand.
Create a compound conditional that prints Hello World. Assume the variable color has already been declared and has the value red
A compound conditional requires two boolean expressions joined by either and or or.
if color == ‘red’ or color == ‘pink’:
print(“Hello World”)
What is the purpose of the random.seed() function
To set up a predictable sequence of pseudo-random numbers
What is the main difference between a loop and a conditional?
A loop repeats the block of code as long as its condition is true
In the following code, identify the loop control variable, the looping condition, and the update to the loop control.
n = int(input(“Enter a number: “))
sum = 0
while n > 0:
sum = sum + n
n = n - 1
print(“The sum of the first n numbers is”, sum)
n
n > 0
n = n - 1
What will cause an infinite loop?
Not updating the loop control variable in the body of the loop
How many times is the following while loop going to check it’s condition?
i = 0
while i <= 5:
print (i+1)
i = i + 1
7
What is the difference between a definite loop and an indefinite loop?
A definite loop will execute a predictable number of times while an indefinite loop will execute an unpredictable number of times which depends on circumstances.
What purpose does a break statement have on a loop?
A break statement is used to exit a loop