The exercises below are organized to match A Whirlwind Tour of Python (VanderPlas). Specific pages are referenced in the various sections. These are designed to run using Python 3.6.
Follow the guidelines below. Where necessary replace the ellipses (...
) with the appropriate code in the code blocks, the run the code by pressing ctrl-enter. Be sure to save your document before closing.
if
, elif
, and else
(p 38)¶if
statement to control what code gets runelif
can be used to add more conditional code blockselse
can be used to run code when all the if
's and elif
's are not met.too slow
¶avg_speed = 10
if avg_speed is less than 15, then print "too slow"
if
/elif
/else
statements that evaluates the value of elev_feet
such that:¶elev_feet
is less than 400, it prints "too low"elev_feet
is greater than 500, it prints "too high"elev_feet
equal 450, it print "got it!"elev_feet = 475
if elev_feet…
print('too low')
print('too high')
print('got it!')
print('close enough')
3.2.1 Create a for loop that iterates through the items in the list of states and prints them.
states = ['Maine','Ohio','Kansas','Utah','Alaska','Nevada']
for...
print(state)
3.2.2 Combine a for
loop and the range
function to print powers of 2 up to the 8th power of 2 (256)
for x...
print(x, 2**x)
3.2.3 The first line in the code below reads in the contents of the NC_Providers.csv file and stores each line as item in the list lineList
. Iterate through the lines in the lineList
list and print the length of each line.
end
in the print statement so no 'new line' character is printed:print(lineText,end='')
lineList = open('NC_SiteData.csv','r').readlines()
for...
print(...)
3.2.4 Extact the 10th line from the lineList to a variable called line10
. Then use the string split()
method to break this line up into a new list called lineData
. (Note that this is a csv
file, so values are separated by a comma.) Finally print each of these items, one at a time.
line10 = lineList[...]
lineData = line10.split(...)
for...
print(...)
3.3.1 Print the phrase "Python is fun!" 5 times using the range()
function and a for
loop.
for...
print("Python is fun!")
3.3.2 Using the range()
function in a for
loop, print the every number between 480 and 490.
for ...
print (i, end=', ')
3.3.3 Using the range()
function in a for
loop, print every other number between 480 and 490.
for ...
print (i, end=', ')
3.4.1 Write a while
loop that prints each year, starting at 2017, stops after printing 2020
year = 2016
while...
print(year)
...
3.4.2 The Python input
function prompts the user to enter a value. Use a while loop to continue asking the user to "pick a number between 1 and 4" until the user guesses correctly.
guess = input("Guess my number, between 1 and 4: ")
actual_number = "1"
while...
if...
guess = input("Nope, guess again: ")
print ("Got it!, It was " + actual_number)
break
and continue
¶#Run this to generate a dictionary of states (keys) and their capitol (values)
state_capitals={"Washington":"Olympia","Oregon":"Salem",
"California":"Sacramento","Ohio":"Columbus",
"Nebraska":"Lincoln","Colorado":"Denver",
"Michigan":"Lansing","Massachusetts":"Boston",
"Florida":"Tallahassee","Texas":"Austin",
"Oklahoma":"Oklahoma City","Hawaii":"Honolulu",
"Alaska":"Juneau","Utah":"Salt Lake City",
"New Mexico":"Santa Fe","North Dakota":"Bismarck",
"South Dakota":"Pierre","West Virginia":"Charleston",
"Virginia":"Richmond","New Jersey":"Trenton",
"Minnesota":"Saint Paul","Illinois":"Springfield",
"Indiana":"Indianapolis","Kentucky":"Frankfort",
"Tennessee":"Nashville","Georgia":"Atlanta",
"Alabama":"Montgomery","Mississippi":"Jackson",
"North Carolina":"Raleigh","South Carolina":"Columbia",
"Maine":"Augusta","Vermont":"Montpelier",
"New Hampshire":"Concord","Connecticut":"Hartford",
"Rhode Island":"Providence","Wyoming":"Cheyenne",
"Montana":"Helena","Kansas":"Topeka",
"Iowa":"Des Moines","Pennsylvania":"Harrisburg",
"Maryland":"Annapolis","Missouri":"Jefferson City",
"Arizona":"Phoenix","Nevada":"Carson City",
"New York":"Albany","Wisconsin":"Madison",
"Delaware":"Dover","Idaho":"Boise",
"Arkansas":"Little Rock","Louisiana":"Baton Rouge"}
3.5.1 Use a for
loop and a break
statement to iterate through states (i.e., the keys) in the state_capitals
dictionary, printing the name of the state's capitol, stopping when 'Texas' is reached (and not printing Texas' capitol).
#Loop through the keys in the dict (i.e. states)
for … in state_capitals.keys():
#Use an if statement to determine if the current state is 'Texas'
if …:
#If so, stop iterating immediately
…
# Print the dictionary value corresponding with the state
print(…)
3.5.2 Modify the if
statment and swap the break
statement with continue
so that instead of stopping at Texas, the loop skips printing the capital of any state ending with the letter a
.
#Loop through the keys in the dict (i.e. states)
for … in state_capitals.keys():
#Get the capital associated from the current state key
capital = ...
#If the capital ends with 'a'...
if …:
#...skip the remainder of the 'for loop' for this iteration
# and move forward to the next item in the list
…
# Print the dictionary value corresponding with the state
print(…)
3.6.1 Create a new variable called "all_sports_lower" that has the contents of the all_sports
string, but using all lowercase letters.
#Create the initial string
all_sports = "Soccer, BASKETBALL, Hockey, Tennis"
all_sports_lower = …
print(all_sports_lower)
3.6.2 Use the split()
function to parse the string into a list variable named "sports".
sports = all_sports_lower.…
print(sports)
3.6.3 Tweak the code below to print Euler's number ($e$) to 6 decimal places.
e = 2.71828182845904
print(…)
3.6.4 Use the variables below to print out the address so it appears as:
222 Main St.
Durham, NC 27701
house_number = 222
street_name = "Main st."
city = "Durham"
state = "NC"
zip = 27701
print(…)