Input and Output

The simplest way to input and output is to use the built-in functions print() and input().

Output

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)

Input

Python provides a built-in function called input that stops the program and waits for the user to type something. When the user presses Return or Enter, the program resumes and input returns what the user typed as a string.

name = input()
name

Question: What is a variable?

print(name)

Exercise 01

Modify hello.py to

name = input()
print('Hello, ', name)

Then rewrite it to ask user to enter his/her name first.

Assignment statements

An assignment statement creates a new variable and gives it a value:

message = 'I did something cool today!'
n = 100
pi = 3.14

Variable names

  • anything meaningful
  • any length
  • can contain letters, numbers and underscore _
  • not begin with number
  • conventionally only lower case

Ilegal names

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.

Expressions and statements

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable.

Examples of expressions:

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)

When you type a statement, the interpreter executes it, which means that it does whatever the statement says. In general, statements don’t have values.

Dynamic language

Variable could be declared again.

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)

What's happening in RAM.

Example:

a = 'ABC'

Two things happen:

  1. a string 'ABC' is created in RAM
  2. a variable a is created in RAM. It is referencing to 'ABC'
a = 'ABC'
b = a
a = 'XYZ'
print(b)

Step by step:

Order of operations

For mathematical operators, Python follows mathematical convention.

String operations

In general, you can’t perform mathematical operations on strings.

'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

String Formating

How do we output formated string? Let's see some examples:

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}.')

More f-string formatting

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}')

Comments

Question: Why do we need comments?

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.

  1. The volume of a sphere with radius r is $$(4/3)\pi r^3.$$ What is the volume of a sphere with radius 5?

  2. 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?

  3. 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?

  4. 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.