Playing around with arithmetic
Trying to answer the following questions using a single line of Python.
-
What is ten times four plus two?
-
What is eight times the quantity of five minus three?
-
What is fifteen divided by three?
-
What is fifteen divided by two?
-
How many times does two go into fifteen?
-
What is the remainder when fifteen is divided by two?
It slices. It dices.
Lists let you work with multiple pieces of data in a single variable. Take a look at the following:
pi = [3, 1, 4, 1, 5, 9, 2, 6, 5] e = [2, 7, 1, 8] print e # Access a single element of the list. print e[0] print e[1] # Select multiple elements from the list. print pi[3:5] print e[1:] print pi[:5] # Change the step size. print pi[::2] print pi[5:3:-1] # Create a new list. newlist = [e[2]] + [e[0]] + pi[0:5:2] print newlist
Try using the above definitions of pi and e to solve the following problems in an "efficient" manner.
-
Store into the variable shortpi the list [3, 1, 4]
-
Store into the variable back the list [8, 1, 7, 2]
-
Store into the variable zip the list [8, 3, 8, 6, 4]
-
Store into the variable count the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
Note that strings are treated a lot like a list of individual letters.
school = "Sandpoint Charter School" city = school[:9]
A few more problems…
-
Store into the variable face the string "ear and nose"
-
Store into the variable long the longest English word you can build from school