Python 01 InputOutput 23
Python 01 InputOutput 23
Python
Input & Output
OBJECTIVES:
● Data types
2
Our first Python programs
# My first Python
print("PPP
program Y Y TTTTT H H OO N N")
print("P P Y Y T H H O O NN N")
print("PPP Y T HHHH O O N N N")
print("P Y T H O O N NN")
H
print("P Y T H OO N N")
# print blank lines H
print()
print()
#
print
greetin
gs
print(
"WelcoWhat do you think this program will
me todo?
Write
Python! this python code and run it.
")
See what the code produces. 3
Our first Python programs
memory
first_name age
last_name
John 20
Smith
ALWAYS use variables with meaningful names and correct data types
first_name = "John"
last_name = "Smith"
age = 20
5
Variables & Data types
Variables store values in certain data types. Common data
types:
●
str: a string represents a sequence of characters.
We use double quotes or single quotes to create a string.
first_name = "John"
state = 'New South Wales'
●
int integer, a whole number
: an
age = 20 6
●
float: a decimal number
interest_rate = 5.2
●
bool: a boolean value is either True or
False.
scan_completed =
True virus_found =
False
Variables & Data types
Each variable has a data type.
Checking data type: type(...variable_name...)
first_name = "John"
last_name = 'Smith'
7
Variables & Data types
Integer: whole numbers
age = 20
temperature = -5
credit_point = 6
print(type(age))
print(type(temperature))
<class 'int'>
print(type(credit_point))
8
Variables & Data types
Float: decimal numbers
price = 30.5
interest_rate =
3.18
print(type(price))
print(type(interest_rate)) <class 'float'>
import math
pi = math.pi
e = math.e
tau = math.tau
3.141592653589793
print(pi) 2.718281828459045
print(e) 6.283185307179586
print(tau) 9
Variables & Data types
Boolean: True or False
virus_scan_completed =
True
virus_found = False
print(type(virus_scan_completed))
print(type(virus_found)) <class 'bool'>
Example:
temperature = -5
10
Important programming rules
Bad example:
subject = "MATH111: Abstract Algebra"
The colon (:) is not part of the information and should not be stored in
variable. What if we want to display like this:
MATH111 - Abstract Algebra
or this:
Abstract Algebra (MATH111)
Good example:
subject_code = "MATH111"
subject_title = "Abstract
Algebra"
11
Important programming rules
Bad example:
unit_price = "$10.50"
Good example:
unit_price = 10.50
quantity = 12
cost = unit_price * quantity
12
Important programming rules
Bad example:
mobile_number = 1231231234
student_number = 1234567
Good example:
mobile_number = "0980980987"
student_number = "0043210"
13
String addition (concatenation)
# name details
first_name =
"John" last_name =
"Smith"
14
String addition (concatenation)
# name details
first_name =
"John" last_name =
"Smith"
memory
first_name full_name
last_name
John John Smith
Smith
15
String multiplication (with number)!
silly1 = "frog" * 7
print(silly1)
print(silly2)
frogfrogfrogfrogfrogfrogfrog
16
Get input from the user
17
Get input from the user
18
Get input from the user
# favorite number
fav_number = 7
# display
favorite number
print("My
favorite number
is " +
fav_number)
# favorite number
fav_number = 7
# display
favorite number
print("My
favorite number
is " +
fav_number)
this isPython
a cannot add a string to a number
string
(some other programming languages can)
this is a
number
24
Convert number into string
fav_number 7
str(fav_number) "7"
26
Convert string into number
30
Get input from the user
31
Convert between data types
Convert to a string:
str(...variable_name...)
Convert to an integer:
int(...variable_name...)
user_input = an integer:
input("Enter ")
number = int(user_input)
user_input "50"
int(user_input) 50
32
Convert between data types
input1 "2.3"
float(input1) 2.3
33
Convert between data types
34
Naming convention
first_name = "John"
last_name =
"Smith"
full_name =
first_name + " " +
last_name
subject1 = "ISIT111"
subject2 = ="MATH101"
fav_number 7
subject3 = "ACCY113"
SECOND_PER_MINUTE = 60
minute = 5
second = minute * SECOND_PER_MINUTE
- to help people understand our program better, especially, if our program has
a special logic that needs explanation
36
Important programming rules
37
Keywords
and elif if print
38
Thank you!