Expressions
In python, We can see the value of something by using print
like this:
print(3)
The expression passed to the print
can be any Python expression. Here are some examples of expression:
1 + 1 # addition
2 - 1 # subtraction
2 * 6 # multiplication
We can compose expressions to make more complicated expressions like,
52 * 3 + 12 * 9
We can also use parentheses to group expressions:
(52 * 3) + (12 * 9)
is different from
52 * (3 + 12) * 9
For example, this code prints out the the number of hours in a day:
print(365 * 24 * 60 * 60)
Quiz 1.1
Write a Python program that prints out the number of minutes in seven weeks.
Answer
print(7 * 7 * 24 * 60)
Quiz 1.2
Which of the following are valid Python expressions that can be produced starting from Expression? There may be more than one.
- A)
3
- B)
((3)
- C)
(1 * (2 * (3 * 4)))
- D)
+ 3 3
- E)
(((7)))
Answer
- A)
3
- C)
(1 * (2 * (3 * 4)))
- E)
(((7)))
Quiz 1.3
Write Python code to print out how far light travels in centimeters after one nanosecond using the multiplication operator.
The speed of light is 299792458 meters per second.
One meter is 100 centimeters.
One nanosecond is one billionth (1/1000000000
) of a second.
Answer
print(299792458 * 100 * (1 / 1000000000))
Answer: 11.1034243704