www.umbc.edu
CMSC201 Computer Science I for Majors
Lecture 08 – For Loops
- Prof. Katherine Gibson
- Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 08 For Loops Prof. - - PowerPoint PPT Presentation
CMSC201 Computer Science I for Majors Lecture 08 For Loops Prof. Katherine Gibson Prof. Jeremy Dixon www.umbc.edu Last Class We Covered Lists and what they are used for How strings are represented How to use strings:
www.umbc.edu
www.umbc.edu
2
www.umbc.edu
3
www.umbc.edu
www.umbc.edu
5
www.umbc.edu
6
www.umbc.edu
7
www.umbc.edu
8
www.umbc.edu
9
www.umbc.edu
10
www.umbc.edu
www.umbc.edu
12
www.umbc.edu
animals = ['cat', 'dog', 'eagle', 'ferret', 'horse', 'lizard'] print("The best animal is a", animals[3]) animals[5] = "mouse" print("The little animal is a", animals[5]) print("Can a", animals[4], "soar in the sky?")
13
??? ??? ??? ??? ??? ???
www.umbc.edu
animals = ['cat', 'dog', 'eagle', 'ferret', 'horse', 'lizard'] print("The best animal is a", animals[3]) animals[5] = "mouse" print("The little animal is a", animals[5]) print("Can a", animals[4], "soar in the sky?")
14
cat dog eagle ferret horse lizard
The best animal is a ferret The little animal is a mouse Can a horse soar in the sky?
mouse
www.umbc.edu
15
www.umbc.edu
16
print(names[1], "sends a message to", names[0]) names[0] = "Ann" print(names[1] + names[1] + names[0] + names[2]) # or print(names[1]*2 + names[0] + names[2])
www.umbc.edu
www.umbc.edu
def main(): print("Welcome to the Grocery Manager 1.0") # initialize the value and the size of our list grocery_list = [None]*3 grocery_list[0] = input("Please enter your first item: ") grocery_list[1] = input("Please enter your second item: ") grocery_list[2] = input("Please enter your third item: ") print(grocery_list[0]) print(grocery_list[1]) print(grocery_list[2]) main()
18
www.umbc.edu
19
www.umbc.edu
20
www.umbc.edu
21
www.umbc.edu
22
www.umbc.edu
nums = [98, 75, 89, 100, 45, 82] total = 0 # we have to initialize total to zero for n in nums: total = total + n # so that we can use it here avg = total / len(nums) print("Your average in the class is:", avg)
23
www.umbc.edu
24
www.umbc.edu
25
List is now: [1, 2, 3, 4]
www.umbc.edu
26
www.umbc.edu
music = "jazz" for c in music: print(c)
27
j a z z
www.umbc.edu
food = ["apples", "bananas", "cherries", "durians"] # for loop goes here for f in food: print(f, "are yummy!")
28
apples are yummy! bananas are yummy! cherries are yummy! durians are yummy!
www.umbc.edu
www.umbc.edu
30
www.umbc.edu
31
www.umbc.edu
32
www.umbc.edu
33
www.umbc.edu
34
www.umbc.edu
>>> twoA = list(range(5,10)) >>> print(twoA) [5, 6, 7, 8, 9] >>> twoB = list(range(-10, -5)) >>> print(twoB) [-10, -9, -8, -7, -6] >>> twoC = list(range(10, 5)) >>> print(twoC) []
35
www.umbc.edu
>>> threeA = list(range(2, 11, 2)) >>> print(threeA) [2, 4, 6, 8, 10] >>> threeB = list(range(3, 28, 5)) >>> print(threeB) [3, 8, 13, 18, 23]
36
www.umbc.edu
37
www.umbc.edu
>>> downA = list(range(10, 0, -1)) >>> print(downA) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> downB = list(range(18, 5, -2)) >>> print(downB) [18, 16, 14, 12, 10, 8, 6]
38
www.umbc.edu
39
www.umbc.edu
www.umbc.edu
41
www.umbc.edu
42
www.umbc.edu
43
www.umbc.edu
www.umbc.edu
45
www.umbc.edu
46
bash-4.1$ python lec8_practice1.py What is the height of your parallelogram? 4 What is the length of your parallelogram? 7 ******* ******* ******* *******
www.umbc.edu
47
bash-4.1$ python lec8_practice2.py Enter a positive integer: 4 The sum of the first 4 squares is 30.