Chapter 3 Flashcards
Show all of the comparison operators
==
!=
>
<
>=
<=
Create a logical comparison
if this happens then do that, if none of the above do this instead
a =1
b =2
c =3
if a == b:
print(“equal”)
elif a == c:
print(“other equal”)
else:
print(“not equal”)
Remember to tab out on your print() functions!
Create a for loop for a string and a list, print a message at the end and explain the difference between the list and string output
people = [
“greg”,
“andy”,
“steve”
]
for men in people:
print(men)
else:
print(“fart”)
THIS WILL PRINT ALL LIST ITEMS ONE AT A TIME
MEN IS WHAT WE NAMED THE TEMPORARY VARIABLE THAT WILL BE THE LIST ITEMS, NAME CAN BE WHATEVER
IF WE DID THIS WITH A STRING THE TEMPORARY VARIABLES WOULD BE EACH LETTER.
ELSE WILL ONLY BE PRINTED IF THE LOOP WAS A SUCCESS, IF A BREAK HAPPENS IT WON’T PRINT
Let’s say we want a for loop with numbers. What would this be called and how would you make one
It’s called a range
for i in range (10)
print(i + 1)
0
1
2
3
4
5
6
7
8
9
THIS DOES NOT PRINT 10 BECAUSE PYTHON STARTS AT 0
THE 10 IS KNOWN AS THE ARGUMENT
THE RANGE IS A FUNCTION
Say we want a loop that follows that numerically and incrementally follows the list items, how would you do that?
Number the items from 1!
names = [
“Frank”,
“Eric”,
“Devin”
]
for num, name in enumerate(names):
print(str(num + 1) + “ “ + name)
What is a while loop? Create one
A while loop says “While this is true, do this other thing.
bottles = 99
while bottles > 0:
print(str(bottles) + “ bottles of beer on the wall”)
print(str(bottles) + “ bottles of beer”)
bottles -= 1
print(“Take one down, pass it around”)
print(bottles + “ bottles of beer on the wall”)
Show how you would increment a number via a while loop
variable += 1
variable -= 1
This is shorthand for:
variable = variable + 1 (you can use this as well if you really want to)
Create an infinite loop
while True:
print(“hello”)
What is a command you can use to stop an infinite loop
while True:
print(“Hello”)
break
Using a modulo/mod, tell the program to print numbers 0 - 9
but, using an if statement and continue, have it only print even numbers
Make this a for loop
for i in range(10):
if 1% 2: continue
print(i)
To unpack here:
% is your modulo, a modulo spits out a number based on a fixed scale of numbers.
Example:
A 12 hour clock is a modulo, and its modulus is 12. so 17 % 12 would wrap back around to 5 since after the 12 it starts back at 1.
CONTINUE means to skip the current iteration(step) and continues the loop.
IF here is asking for a True/False statement in regards to going to continue, 0 being false and non-0 being true.
Therefore odd number produce remainders, making if statement true and it goes to continue and loops again, skipping printing.
Nest an if comparison to make it slightly more intricate
a = 1
b = 2
c = 3
if a > b:
print(“a is greater than b”)
if b != c:
print(“b is not equal to c”)
else:
print(“b is equal to c”)
else:
print(“a is less than b”)
Create a nested loop using ranges
you’ll need two number ranges to make
00 - 99
Remember, if you don’t turn them into strings + will add them together instead of placing them next to each other
for i in range(10):
for j in range(10):
print(str(i) + str(j))
How do you add in a randomized number
from random import seed
from random import randint
number = randint(1, 10)
Create a random number guessing game where it lets you know if you’re guessing to high or low, game should loop.
from random import seed
from random import randint
number = randint(1,10)
guess = input(“Guess a number between 1 and 10 “)
while int(guess) != number:
if int(guess) > number:
guess = input(“A little lower, try again: “)
else:
guess = input(“A little higher, try again: “)
print(“That’s correct!”)
IF THIS ISN’T WORKING IT’S BECAUSE YOU NEED TO MAKE YOUR GUESS INTO AN INTEGER, DUMBY!