vertopal.com_workingwithlistpart1
vertopal.com_workingwithlistpart1
#Let’s use a for loop to print out each name in a list of magicians:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
alice
david
carolina
###Let’s add a second line to our message, telling each magician that
we’re looking forward to their next trick:
###Indentational Errors####
##Forgetting to Indent
#Error 1
#magicians = ['alice', 'david', 'carolina']
#for magician in magicians:
#print(magician)
# Error 2: Forgetting to Indent Additional Lines
#This is a logical error. The syntax is valid Python code, but the
code does not
# produce the desired result because a problem occurs in its logic.
# Sometimes your loop will run without any errors but won’t
#produce the expected result. This can happen when you’re
#trying to do several tasks in a loop and you forget to indent
# some of its lines.
#magicians = ['alice', 'david', 'carolina']
#for magician in magicians:
#print(f"{magician.title()}, that was a great trick!")
#print(f"I can't wait to see your next trick,{magician.title()}.\n")
#Error 3: Indenting Unnecessarily
#If you accidentally indent a line that doesn’t need to be indented,
# Python informs you about the unexpected indent
#message = "Hello Faculty members!"
#print(message)
#Error 4: Indenting Unnecessarily After the Loop
#The colon at the end of a for statement tells Python to interpret the
next line as the start of a loop.
####EXERCISE PIZZA####
# List of favorite pizzas
####EXERCISE Animals
1
2
3
4
1
2
3
4
5
[1, 2, 3, 4, 5]
#We can also use the function to tell Python to skip range() numbers
in a given range.
# If you pass a third argument to range(), Python uses that value as a
step size
# when generating numbers.
##For example, here’s how to list the even numbers between 1 and 15
#For example, consider how you might make a list of the first 10
square numbers
# (that is, the square of each integer from 1 through 10).
# In Python, two asterisks (**) represent exponents.
# Here’s how you might put the first 10 square numbers into a list
#To write this code more concisely, omit the temporary variable square
and append
# each new value directly to the list.
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares) # The result will be same, but there is no temporary
variable over here such as "square"
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
min(digits)
max(digits)
sum(digits)
##EXERCISE 1
#Counting to Twenty: Use a for loop to print the numbers from 1 to 20,
inclusive.
# For loop to print numbers from 1 to 20
for number in range(1, 21):
print(number)
# EXERCISE 2
# One Million: Make a list of the numbers from one to one million,
and then use a for
# loop to print the numbers. (If the output is taking too long, stop
it by pressing
# CTRL-C or by closing the output window.)
# List of numbers from 1 to 1 million
#numbers = list(range(1, 1000001))
# For loop to print each number
#for number in numbers:
#print(number)
#EXERCISE 3 Make a list of the numbers from one to one million, and
# then use min() and max() to make sure your list actually starts at
one and
# ends at one million. Also, use the sum() function to see how quickly
1
3
5
7
9
11
13
15
17
19
3
6
9
12
15
18
21
24
27
30
# EXERCISE 6:
# Cubes: A number raised to the third power is called a cube. For
example, the cube of 2
# is written as 2**3 in Python. Make a list of the first 10 cubes
# (that is, the cube of each integer from 1 through 10), and use a for
loop to print out
# the value of each cube.
# List of the first 10 cubes
1
8
27
64
125
216
343
512
729
1000