ENGG1003 08 PythonBasics-A
ENGG1003 08 PythonBasics-A
Python Basics
Background
• Python is a computer programming language.
• We can realize computations using Python.
• Milestones:
• Python Installation and BMI calculation
• Python IDLE Interactive Shell
• Key in instructions and get results interactively in a
Read-Eval-Print Loop (REPL)
Musician beethoven
Painter van_gogh
Philosopher karl_marx
Type, Name and Value/ Object
• Type: for classification
• Many types have been defined and can be used readily.
• We can still define new types (as known as classes.)
• Many computing operations require type matching.
>>> type(pet)
<class 'turtle.Turtle'>
>>> type(pet.forward)
<class 'method'>
>>> type(100)
<class 'int'>
100 is an integer-type value
according to their kinds
-- Genesis 1, Bible (NIV)
Type
• Python supports MANY types of data, such as:
• int – integers (whole numbers)
• Example int-type values: -7 , 0 , 999
Identifier/ Name
• Names are for identification, usually meaningful words.
• Think about nouns and verbs, they are ubiquitous!
• Names usually start with a letter and form with letters, digits and _
• Some identifiers have been defined/ created already and can be
used readily
• Still, we have room to create our own names, examples:
Pythagoras, a and b are all identifiers.
COVID_death_toll = 0 math and sqrt are also identifiers.
peter_weight = 64.5 Identifiers are everywhere.
bodyWeight = 57
bodyHeight = 1.58
count
bmi = bodyWeight / bodyHeight / bodyHeight 1
2
3
print(bmi) (int)
Arithmetic Operators + - * / % // **
and Check if both logical statements are True 7 != 2 and 5 > 2 True
print( x == y and y == z )
is equivalent to
print( x == y == z )
Printing on Screen
• We can print text (string) and values:
Hi, Peter !
name = 'Peter' Your weight is 57 kg
age = 21 Your BMI is 22.832879346258608
You will be 22 years old
bodyWeight = 57
bodyHeight = 1.58
bmi = bodyWeight / bodyHeight / bodyHeight
print("Hi,", name, "!")
print("Your weight is", bodyWeight, 'kg')
print("Your BMI is", bmi)
print('You will be', (age+1), "years old")
Input from User
• User may input some text (string) using input( )
name = input("What's your name? ")
print("Hi,", name, "!")
bool_ True
if bool_expression: expression
statement False
statement
4 spaces/ Tab here!
next_statement
next_statement
Branching Statement if:
• If condition is True, execute statement.
• If condition is False, skip statement.
• Anyway, move on to next_statement.
True
if condition: condition
statement False
statement
4 spaces/ Tab here!
next_statement
next_statement
Branching Statement if: more
• If condition is True, execute statement1 & statement2.
• If condition is False, skip statement1 & statement2.
• Anyway, move on to next_statement.
True
if condition: condition
statement1 False
statement1
statement2
4 spaces/ Tab here! statement2
next_statement
next_statement
Branching Example if:
1 score = int(input("Please enter your score: "))
2
3 if score >= 60:
4 print("Passed!")
5 print("Congratulations!")
6
7 if score < 60:
8 print("Failed!")
9
10 print("Your score is", score)
11
Please enter your score: 80 Please enter your score: 40
Passed! Failed!
Congratulations! Your score is 40
Your score is 80 20
Branching Statement if:else:
• If condition is True, execute only statement_T.
• If condition is False, execute only statement_F.
• Anyway, move on to next_statement.
22
Successive Conditions
if:elif:
1 year = int(input("Please enter year of study: "))
2
3 # handles multiple conditions successively
4
5 if year == 1:
6 print("Welcome, Freshman!")
7 elif year == 2:
8 print("O'camp Leader!")
9 elif year == 3:
10 print("O'camp Organizer!")
11 else:
12 print("Graduate!")