Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WEBINAR: For Loops
- # We use loops to repeat actions
- # a WHILE loop... btw.. is basically an IF statement
- # that repeats as long as its condition is true
- # FOR LOOPS are used for repeating actions for every element
- # in a container like a list, string, tuple, etc...
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- # for _loop_variable_ in _someContainer_:
- for item in myList:
- print(item)
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- for char in myString:
- # if char.isupper():
- # print(char)
- print(char, end=" ")
- print()
- print(myString)
- tvLovesDCT = {
- "Joannie": "Chaci",
- "Buffy": "Angel",
- "Mulder": "Scully",
- "The Mandalorian": "Grogu"
- }
- # myDictionary[someKey] = someValue
- for key in tvLovesDCT:
- # print(key)
- print("{} loves {}".format(key, tvLovesDCT[key]))
- # if "Mulder" in tvLovesDCT:
- # print("found!")
- # if "Scully" in tvLovesDCT:
- # print("found?")
- for k, v in tvLovesDCT.items():
- print("{} --> {}".format(v, k))
- # for n in 5: # can't do that!
- for n in range(0, 5):
- print(n)
- # WHILE LOOP differences
- x = 0 # we must instantiate the variable
- while x < 5: # we must think of a useful condition
- print("{} is still less than 5".format(x))
- x += 1 # we have to do this: increment/decrement/change the variable
- # FOR LOOP with INDEX position
- # combine range() and len()
- for i in range(0, len(myList)):
- print(i, end=": ")
- print(myList[i])
- # Lab 6.14 example
- x = int(input())
- while x > 0:
- print(x % 2, end="")
- x = int(x / 2)
- print()
- # Question, how can I reverse that?
- # To reverse it, you have to store it so you have something to work with.
- # Remember print() doensn't store anything for later.
- x = int(input())
- while x > 0:
- print(x % 2, end="")
- x = int(x / 2)
- print()
- strX = ""
- while x > 0:
- strX += str(x%2)
- x = int(x/2)
- print(strX)
- # reverse it... and print() the reverse...
- print(strX[::-1])
- # But if you're asking because of the similar Lab 7.21...
- # the CONTEXT of the questions in Lab 6.14 and Lab 7.21 differs!!!
- # Lab 7.21 involves two FUNCTIONS that each have a single job
- # one to get the binary
- # the second to reverse the result of the first
- # note that this involves return values, not printing directly
- # Lab 23.5 example
- # Declare variable to hold running count.
- count = 0
- low = int(input())
- high = int(input())
- x = int(input())
- for i in range(low, high + 1):
- if i % x == 0:
- count = count + 1
- print(count)
Add Comment
Please, Sign In to add comment