Note: Exercises in gray are those that appear in the Introduction. Feel free to skip these if you have already completed them.
6+5*10
and (6+5)*10
?num=23
textnum="57"
decimal=98.3
print(variable1, variable2).
You can also print raw values that have not been assigned variables. For example, print("This is variable 1: ", 67)
. num1=...
num2=...
num3=...
num4=...
num5=...
num6=...
num7=...
num8=...
num9=...
num10=...
print
statement to print all of the even numbers out of your newly defined variables with the following statement, "These are my even numbers:". +
operator in Python works with strings variables as well as numeric types - but it simply concatenates strings together one after the other. Using +
and the example below, write your output as a single complete sentence. (Hint: you'll probably need to use at least one type conversion function).print("work"+"shop")
print("3"+"7")
Create a list of:
your two favorite holidays (list)
For example:
["red", ["Halloween", "New Years"]]
Try to use indexing to get the tenth digit of `my_pi` as defined below. Does it work as defined? Do we need to change the variable somehow?
my_pi = 3.141592653589793
Below is a list of lists containing the NATO phonetic codes for each letter of the alphabet. Each list within `nato` contains a letter of the alphabet and its corresponding code.
nato = [["A", "Alfa"],
["B", "Bravo"],
["C", "Charlie"],
["D", "Delta"],
["E", "Echo"],
["F", "Foxtrot"],
["G", "Golf"],
["H", "Hotel"],
["I", "India"],
["J", "Juliett"],
["K", "Kilo"],
["L", "Lima"],
["M", "Mike"],
["N", "November"],
["O", "Oscar"],
["P", "Papa"],
["Q", "Quebec"],
["R", "Romeo"],
["S", "Sierra"],
["T", "Tango"],
["U", "Uniform"],
["V", "Victor"],
["W", "Whiskey"],
["X", "X-ray"],
["Y", "Yankee"],
["Z", "Zulu"]]
Let's perform some more minutes-in-year calculations. Using the lists you created above, calculate and print the number of minutes in a normal year. Now calculate and print the number of minutes in a leap year.
Consider the following list of sentences:
Sentences = [
"I went to The Ohio State University",
"Add just a bit more.",
"Cars drive quickly.",
"He heard yells outside.",
"What is your name?",
"Bob was very exasperated.",
"Give me the keys.",
"Boy oh boy am I glad to see you!",
"Students in the hallway whispered loudly.",
"You're learning Python!",
"What is the capital of Florida?",
"We went to the zoo."
]
Can you create the following sentence using only elements from this list? sentence = "The boy yells loudly."
In other words, create and print this sentence without typing any new bits of text.
.split()
function can be used to split a string into a list of words. For example: str = "I want pizza."
Using the following code, you can split the sentence str
into a list of words separated by spaces: list = str.split(" ")
. This command uses whitespaces (" ")
to separate words into a list, leading to the following output: ["I", "want", "pizza."]
. You can now use list indexing to easily access words within a sentence. For example, list[2]
would output the word "pizza."
.join()
function to join elements of a list into a string. Let's use our earlier example, where list=["I", "want", "pizza."]
. Here, the command " ".join(list)
glues all of the elements in list
together with whitespaces (" "
) between them. As a result, using the command str = " ".join(list)
will bring us back to where we started! str = "I want pizza."
Can you create a "MyName" variable (str) that stores your name using only elements from the sentences
list above? In other words, create and print your name without typing any new bits of text.
#Template:
age = <your choice of age>
if <condition using age>:
print(“you are old enough to get a driver’s license.”)
#Template:
testword=<your choice of word>
if <condition using testword>:
print(testword)
Now that we've learned conditionals, we can more easily account for a wide range of circumstances. Let's go back to our minutes-in-a-year example. Define the following variables:
DaysInYear=365
HoursInDay=24
MinutesInHour=60
#Template:
if <condition to determine if a day is a leap year>:
print(<Minutes In Year calculation>)
print("It's a leap year!")
else:
print(<Minutes In Year calculation>)
print("It's not a leap year!")
Pick any word and declare it as a variable. Write a conditional statement (if
, elif
, else
) that checks your word for the conditions below. Print different messagess that describe the word if it satisfies any of the individual conditions.
testword = "google"
if testword <condition concerning testword>
print(<message describing why that word satisfied the condition>)
elif testword <condition concerning testword>
print(<message describing why that word satisfied the condition>)
#etc...
#Nesting loops - indentation is key!
listOfWords = ["blue", "yellow", "red", "green"]
newList = [] #initialize an empty list
for color in listOfWords:
numLetters = 0 #resets to zero each time the loop runs
for letter in color:
numLetters += 1
temporaryList = [color, numLetters]
newList.append(temporaryList)
print(newList)
(Un-numbered Exercise). How could we write the code above with fewer lines? Is there a simpler way to find the length of each word?
scores=[95,90,66,83,71,78,93,81,87,81]
grades=[]
for score in scores:
if score>=90:
grade="A"
elif score>=80:
grade="B"
elif score>=70 and score<80:
grade="C"
elif score>=60 and score<70:
grade="D"
else:
grade="F"
grades.append([score,grade])
print(grades)
years=[1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,
2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,
2012,2013,2014,2015,2016,2017,2018,2019,2020]
squaresdict={k:k**2 for k in range(1,16)}
print(squaresdict)
1. Considering the code above, write a list comprehension to create a list of just the values (i.e. the squares) from `squaresdict`.
random_words=["statement", "toy", "cars", "shoes", "ear", "busy",
"magnificent", "brainy", "healthy", "narrow", "join",
"decay", "dashing", "river", "gather", "stop", "satisfying",
"holistic", "reply", "steady", "event", "house", "amused",
"soak", "increase"]
vowels=["a","e","i","o","u","y"]
output=[]
for word in random_words:
count=0
for char in word:
if char in vowels:
count=count+1
if count>=3:
output.append([word,count])
#Example:
#1. Get or define the list my_numbers
my_numbers=list(range(100))
#2. Create an empty list for the new all-odd numbers, called my_numbers2.
#3. Use a loop to iterate through the list of numbers
#3a. For a given number check to see if it is even.
#3b. If the number is even, add 1.
#3c. Append the resulting number to the my_numbers2 list.
Define a function, `median` to find the median of a list. The median is the middle number of an odd-numbered list or the average of the middle two numbers in an even numbered list. (Hint: Use `sorted(
Test your function with the lists below:
data1 = list(range(1,100))
#Normally Distributed Data:
from numpy.random import normal
data2 = normal(loc=0,scale=2,size=100) #scale=2 defines the standard deviation as 2