Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2023 Jan 7
- # 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...
- # Basic syntax of a for loop
- # for _something_ in _someContainer__:
- # list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item) # print(item, end="\n")
- # tuples
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- # strings
- myString = "It was the best of times."
- for char in myString:
- # print(char) # a lot of returns!
- print(char, end="")
- print() # print(end="\n")
- # print("A clean new line!") # and comment out or delete after!
- # dictionaries
- bestOfXF = {
- "1x00": "Pilot",
- "2x10": "Red Museum",
- "2x14": "Die Hand Die Verletzt",
- "3x04": "Clyde Bruckman's Final Repose",
- "3x12": "War of the Coprophages",
- "3x20": "Jose Chung's From Outer Space",
- "4x05": "The Field Where I Died",
- "5x05": "The Post Modern Prometheus",
- "5x17": "All Souls"
- }
- # myDict[key] # retrieve the value for that key... similar to dict method .get()
- # myDict[key] = value # assign a new value to that key
- for key in bestOfXF: # for key in nameOfDict:
- # print("Check out Episode {} or {}".format(key, bestOfXF[key]))
- # print("Check out Episode {} or {}".format(key, bestOfXF.get(key)))
- value = bestOfXF[key]
- print("Check out Episode {} or {}".format(key, value))
- # print(bestOfXF.items())
- # x, y = [1, 3.14]
- # print(y)
- for k, v in bestOfXF.items():
- print("Check out Episode {} or {}".format(k, v))
- # myDict[key] is like myList[num]
- # print(myList[0]) # retrieving a value for an index position in a list is similar to retrieving a dictionary value associated with a key
- # range()
- for num in range(0, 5): # [0, 1, 2, 3, 4]
- print(num)
- # range() of len()
- for i in range(0, len(myList)):
- print("{} - {}".format(i, myList[i]))
- # Student questions...
- # Lab 25.3
- # This is more of a while loop situation, and a great example of the kind of thing I'll go over next week in my Input Patterns webinar
- '''
- Write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs the smallest and largest integers in the list.
- Ex: If the input is:
- 10
- 5
- 3
- 21
- 2
- -6
- the output is:
- 2 and 21
- You can assume that the list of integers will have at least 2 values.
- '''
- myInput = int(input())
- numList = []
- while myInput > 0: # another common sentinel value condition is while myInput != "-1":
- numList.append(myInput)
- myInput = int(input())
- # print("numList is: {}".format(numList)) # see? Looks like what we need, right?
- theMax = max(numList)
- theMin = min(numList)
- print("{} and {}".format(theMin, theMax))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement