2. Variables, Expressions and Statements
Just a quick introduction to some foundamental elements in programming.
- Input and Output
- Assignment statements
- Variable names
- Expressions and statements
- Order of operations
- String operations
- String Formating
print('Hello, World!')
print('Hey Jude', 'don\'t make it bad')
print ('The total number of overall medals in Tokyo 2020 is', 39 + 41 + 33)
print ('39 + 41 + 33 =', 39 + 41 + 33)
name = input()
name
Question: What is a variable?
print(name)
name = input()
print('Hello, ', name)
Then rewrite it to ask user to enter his/her name first.
message = 'I did something cool today!'
n = 100
pi = 3.14
76ers = 'Philadelphia 76ers'
more@ = 10000
class = 'Problem Solving'
Because class
is one of Python's keywords.
More reserved words, or keywords of Python:
https://docs.python.org/3/reference/lexical_analysis.html#keywords
DO NOT use them as variable names or file names.
42
n
n + 100
When you type an expression at the prompt, the interpreter evaluates it, which means that it finds the value of the expression.
A statement is a unit of code that has an effect, like creating a variable or displaying a value.
Examples of statements:
n = 100
print(n)
a = 123 # a is an integer
print(a)
a = 'ABC' # a becomes a string
print(a)
Assignment =
is not equation in mathematics! For example:
x = 10
x = x + 2 # This does not make sense in mathematics,
# but it is perfectly ok in Python.
print(x)
a = 'ABC'
Two things happen:
- a string
'ABC'
is created in RAM - a variable
a
is created in RAM. It is referencing to'ABC'
a = 'ABC'
b = a
a = 'XYZ'
print(b)
Step by step:
'2' -'1'
'EU' - 'Great Britain'
However, +
and *
could be used carefully.
The +
operator performs string concatenation, which means it joins the strings by linking them end-to-end. For example:
first_name = 'John'
last_name = 'Lennon'
first_name + last_name
The *
operator also works on strings; it performs repetition. For example:
'Naah, na na nanana naah, nanana naah, hey Jude. ' * 10
name = 'world'
print(f'Hello, {name}')
actor = 'Joaquin Phoenix'
year = 2020
movie = 'Joker'
print(f'{actor} wins Best Actor for {movie} at Golden Globes {year}.')
pi = 3.1415926
print(f'Pi equals {pi:.5f}.')
print(f'Pi equals {pi:8.5f}.')
print(f'Pi equals {pi:8.2f}.')
a = 2021
# binary
print(f'{a:b}')
# hexadecimal
print(f"{a:x}")
# octal
print(f"{a:o}")
# scientific
print(f"{a:e}")
s1 = 'a'
s2 = 'ab'
s3 = 'abc'
s4 = 'abcd'
print(f'{s1:>10}')
print(f'{s2:>10}')
print(f'{s3:>10}')
print(f'{s4:>10}')
minute = 45 # current time
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
Exercise 02
Create file calc_2.py to answer the following questions. Add comments when necessary.
-
The volume of a sphere with radius r is $$(4/3)\pi r^3.$$ What is the volume of a sphere with radius 5?
-
Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs \$3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?
-
If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?
-
If my average grade rises from 82 to 89. What is the percentage of the increase? Format the result as
xx.x%
. Keep one figure after decimal point.