Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
209 views

Python Loops 1

The document provides examples of using for loops to iterate through lists in Python. It demonstrates basic for loops, nested for loops, conditional logic within loops, breaking out of loops, and list comprehensions. A variety of lists are defined containing data like dog breeds, student names, sales numbers, temperatures and numbers. These lists are looped through to print values, add items to new lists, check conditions, and perform calculations like conversions.

Uploaded by

vishal dhiman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views

Python Loops 1

The document provides examples of using for loops to iterate through lists in Python. It demonstrates basic for loops, nested for loops, conditional logic within loops, breaking out of loops, and list comprehensions. A variety of lists are defined containing data like dog breeds, student names, sales numbers, temperatures and numbers. These lists are looped through to print values, add items to new lists, check conditions, and perform calculations like conversions.

Uploaded by

vishal dhiman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Paste the following code into script.

py:

for breed in dog_breeds:


print(breed)
This will print each breed in dog_breeds.
dog_breeds = ['french_bulldog', 'dalmatian', 'shihtzu', 'poodle', 'collie']

for breed in dog_breeds:

print(breed)

1.
Run the code. You should get an IndentationError because the print(game) line is
not indented.
2.
Indent line 6 so that you don’t get an IndentationError when you run the code.
3.
Write a loop that prints each sport in sport_games.
board_games = ['Settlers of Catan', 'Carcassone', 'Power Grid', 'Agricola', 'Scrabble']

sport_games = ['football', 'football - American', 'hockey', 'baseball', 'cricket']

for game in board_games:

print(game)

for game in sport_games:

print(game)

1.
Use the range function in a for loop to print out promise 5 times.
promise = "I will not chew gum in class"

for i in range(5):

print(promise)

1.
Suppose we have two lists of students, students_period_A and students_period_B.
We want to combine all students into students_period_B.
Write a for loop that goes through each student in students_period_A and adds it
to the end of students_period_B.
Stuck? Get a hint
2.
Inside the for loop, after appending student to students_period_B, print student.
3.
Let’s suppose you made a typo in the body of your for loop. Oh no!

Inside the for loop, change the object of the append statement
from students_period_B to students_period_A.

Uh oh! Now you’re adding each student in students_period_A to students_period_A!


This will be infinite!

Refresh the page (or type something into your editor) to escape this infinite
loop! Then, get rid of the mistake that caused the infinite loop.
Stuck? Get a hint
4.
Having the fear of infinite loops instilled in you, change the
inner students_period_A back to students_period_B, if you haven’t already. Revel in
the safety of your code, and how it doesn’t crash your browser!
students_period_A = ["Alex", "Briana", "Cheri", "Daniele"]

students_period_B = ["Dora", "Minerva", "Alexa", "Obie"]

for student in students_period_A:

#students_period_A.append(student)

students_period_B.append(student)

print(student)

1.
You have a list of dog breeds you can adopt, dog_breeds_available_for_adoption.
Using a for loop, iterate through the dog_breeds_available_for_adoption list and
print out each dog breed.
2.
Inside your for loop, after you print each dog breed, check if it is equal
to dog_breed_I_want. If so, print "They have the dog I want!"
3.
Add a break statement when your loop has found dog_breed_I_want, so that the
rest of the list does not need to be checked.
dog_breeds_available_for_adoption = ['french_bulldog', 'dalmatian', 'shihtzu', 'poodle', 'collie']

dog_breed_I_want = 'dalmatian'

for dogs in dog_breeds_available_for_adoption:

print(dogs)

if dogs == dog_breed_I_want:

print("They have the dog I want!")

break

1.
Your computer is the doorman at a bar in a country where the drinking age is
21.

Loop through the ages list. If an entry is less than 21, skip it and move to the
next entry. Otherwise, print the age.
ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]

for i in ages:

if i < 21:

continue

print(i)

1.
You are adding students to a Poetry class, the size of which is capped at 6.
While the length of the students_in_poetry list is less than 6, use .pop() to take a
student off the all_students list and add it to the students_in_poetry list.
Stuck? Get a hint
2.
Print the students_in_poetry list .
all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]

students_in_poetry = []

while len(students_in_poetry) < 6:


name = all_students.pop()

students_in_poetry.append(name)

print(students_in_poetry)

1.
We have provided the list sales_data that shows the numbers of different
flavors of ice cream sold at three different locations of the fictional shop,
Gilbert and Ilbert’s Scoop Shop. We want to sum up the total number of
scoops sold. Start by defining a variable scoops_sold and set it to zero.
2.
Go through the sales_data list. Call each inner list location, and print out
each location list.
3.
Within the sales_data loop, go through each location list and add the element
to your scoops_sold variable.

By the end, you should have the sum of every number in the sales_data nested
list.
Stuck? Get a hint
4.
Print out the value of scoops_sold.
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]

scoops_sold = 0

for location in sales_data:

print(location)

for element in location:

scoops_sold += element

print(scoops_sold)

1.
We have defined a list heights of visitors to a theme park. In order to ride the
Topsy Turvy Tumbletron roller coaster, you need to be above 161 centimeters.
Using a list comprehension, create a new list called can_ride_coaster that has
every element from heights that is greater than 161.
Stuck? Get a hint
2.
Print can_ride_coaster.
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]

can_ride_coaster = [height for height in heights if height > 161]

print(can_ride_coaster)

1.
We have provided a list of temperatures in celsius. Using a list comprehension,
create a new list called fahrenheit that converts each element in the celsius list
to fahrenheit.

*Note: * To convert, use the formula:

temperature_in_fahrenheit = temperature_in_celsius * 9/5 + 32


2.
Print fahrenheit.
celsius = [0, 10, 15, 32, -5, 27, 3]

fahrenheit= [(temp *9/5)+32 for temp in celsius]

print(fahrenheit)

1.
Create a list called single_digits that consists of the numbers 0-9 (inclusive).
Stuck? Get a hint
2.
Create a for loop that goes through single_digits and prints out each one.
Stuck? Get a hint
3.
Create a list called squares. Assign it to be an empty list to begin with.
4.
Inside the loop that iterates through single_digits, append the squared value
of each element of single_digits to the list squares. You can do this before or
after printing the element.
Stuck? Get a hint
5.
After the for loop, print out squares.
6.
Create the list cubes using a list comprehension on the single_digits list. Each
element of cubes should be an element of single_digits taken to the third
power.
Stuck? Get a hint
7.
Print cubes.

Good job!
single_digits=list(range(10))

for digit in single_digits:

print(digit)

squares=[digit**2 for digit in single_digits]

print(squares)

cubes=[digit**3 for digit in single_digits]

print(cubes)

You might also like