First Python Exercises
Your first python exercises
Printing to the Screen
Store your street address, city, state, and zip code in variables (or even better, a dictionary!), then print them in the usual format:
Name
Street
City, State, Zip
Calculations
Write a program that converts seconds to years. Test your program with
600000000
seconds, 60
seconds, and 40000.33
seconds.
Does this make sense for all the inputs? We can get a bit more exact if
we cast test1
as a float.
test1 = 600000000.to_f
Collections
Create a collection of these authors and the year they kicked the bucket. Print the collection in the following format:
Charles Dickens died in 1870.
Charles Dickens, 1870
William Thackeray, 1863
Anthony Trollope, 1882
Gerard Manley Hopkins, 1889
Branching
A time traveler has suddenly appeared in your classroom!
Create a variable representing the traveler’s year of origin (e.g., year = 2000) and greet our strange visitor with a different message if he is from the distant past (before 1900), the present era (1900-2020), or from the far future (beyond 2020).
If you want to get really fancy, try writing a line of code that would ask your user “What year is this time traveler from?”, then print the appropriate response according to their answer. (Hint: try looking here for tools! https://docs.python.org/3/tutorial/controlflow.html)
Pirate Test (easy)
Write a program that tests whether someone is a pirate or not. As we all know, no pirate can resist using the exclamation “Arrr!” constantly. If a string contains “Arrr!”, tell the pirate to go away. Otherwise, welcome your non-pirate friend with open arms.
Tests:
- Arrr! How are ye?
- Hello, friend.
Hint: string_variable["some text"]
equals “some text
” if those characters exist in
string_variable
and otherwise equals nil
.
Longest word (not too hard)
Print out the longest word in “The quick brown fox jumped over the lazy dogs” and its length.
##Hints
len(my_string)
equals the length of a string.list_of_words = my_long_string.split()
will break the string up by spaces into a list.
What about “The quick brown fox jumps over the lazy dogs”? How might we find all the longest words?
Calculating Grades (ok, let me think about this one)
Write a program that will average 3 numeric exam grades and return an average test score, a corresponding letter grade, and a message stating whether the student is passing.
Average | Grade |
---|---|
90+ | A |
80-89 | B |
70-79 | C |
60-69 | D |
0-59 | F |
Exams: 89, 90, 90
Average: 90
Grade: A
Student is passing.
Exams: 50, 51, 0
Average: 33
Grade: F
Student is failing.
Population Growth (Might need to ask somebody)
The population of Fibonaccia has been shown to grow at an exponential rate with a really odd phenomenon: each year the total population is the sum of the population for the previous two years. With a starting population of 10, the population for the first five years would be:
10, 20, 30, 50, 80
Write a program that will calculate the population of Fibonaccia after 10 years, 20 years, 100 years.
Hint: population = [0,10]
Population Growth (Are you serious?)
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
Find the sum of all the even-valued terms in the sequence which do not exceed 4 million.