PCEP Module 3 Flashcards
==
Returns True if operands’ values are equal, and False otherwise
!=
returns True if operands’ values are NOT equal, and False otherwise
>
True if the left operand’s value is greater than the right operand’s value, and False otherwise
<
True if the left operand’s value is less than the right operand’s value, and False otherwise
> =
True if the left operand’s value is greater than or equal to the right operand’s value, and False otherwise
<=
True if the left operand’s value is less than or equal to the right operand’s value, and False otherwis
if-else
x = 10
if x < 10: # Condition
print(“x is less than 10”) # Executed if the condition is True.
else:
print(“x is greater than or equal to 10”) # Executed if the condition is False.
What is the output of…
x = 5
y = 10
z = 8
print(x > y)
print(y > z)
False
True
What is the output of…
x = 10
if x == 10:
print(x == 10)
if x > 5:
print(x > 5)
if x < 10:
print(x < 10)
else:
print(“else”)
True
True
else
What is the output of…
x = “1”
if x == 1:
print(“one”)
elif x == “1”:
if int(x) > 1:
print(“two”)
elif int(x) < 1:
print(“three”)
else:
print(“four”)
if int(x) == 1:
print(“five”)
else:
print(“six”)
four
five
What is the output of…
x = 1
y = 1.0
z = “1”
if x == y:
print(“one”)
if y == int(z):
print(“two”)
elif x == y:
print(“three”)
else:
print(“four”)
one
two
While loop infinite loop
while True:
print(“Stuck in an infinite loop.”)
While loop “counter”
counter = 5
while counter > 2:
print(counter)
counter -= 1
for loop, in elements
word = “Python”
for letter in word:
print(letter, end=”*”)
for loop, in range
for i in range(1, 10):
if i % 2 == 0:
print(i)
break
stops the loop immediately
continue
stops the current iteration of the loop
can loops have an “else” clause?
yes
range(start, stop, step)
“start” is an optional parameter specifying the starting number of the sequence (0 by default)
“stop” is an optional parameter specifying the end of the sequence generated (it is not included),
and
“step” is an optional parameter specifying the difference between the numbers in the sequence (1 by default.)
Output?
for i in range(3):
print(i, end=” “)
0 1 2
Output?
for i in range(6, 1, -2):
print(i, end=” “)
6, 4, 2
Create a for loop that counts from 0 to 10, and prints odd numbers to the screen. Use the skeleton below:
for i in range(0, 11):
if i % 2 != 0:
print(i)
Create a while loop that counts from 0 to 10, and prints odd numbers to the screen:
x = 1
while x < 11:
if x % 2 != 0:
print(x)
x += 1
Create a program with a for loop and a break statement. The program should iterate over characters in an email address, exit the loop when it reaches the @ symbol, and print the part before @ on one line.
for ch in “john.smith@pythoninstitute.org”:
if ch == “@”:
break
print(ch, end=””)