Chapter 3 Flashcards

1
Q

Show all of the comparison operators

A

==
!=
>
<
>=
<=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a logical comparison
if this happens then do that, if none of the above do this instead

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Let’s say we want a for loop with numbers. What would this be called and how would you make one

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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!

A

names = [
“Frank”,
“Eric”,
“Devin”
]
for num, name in enumerate(names):
print(str(num + 1) + “ “ + name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a while loop? Create one

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show how you would increment a number via a while loop

A

variable += 1
variable -= 1

This is shorthand for:
variable = variable + 1 (you can use this as well if you really want to)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create an infinite loop

A

while True:
print(“hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a command you can use to stop an infinite loop

A

while True:
print(“Hello”)
break

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Nest an if comparison to make it slightly more intricate

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

for i in range(10):
for j in range(10):
print(str(i) + str(j))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you add in a randomized number

A

from random import seed
from random import randint
number = randint(1, 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a random number guessing game where it lets you know if you’re guessing to high or low, game should loop.

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly