FIT9136 Week 2-Python Basic Elements
FIT9136 Week 2-Python Basic Elements
INFORMATION
TECHNOLOGY
Week 2:
Python Basic Elements
Help for This Unit
▪ Lecture:
– Ask me during the break of the class
▪ Lab
– Ask your tutor any questions
▪ Consultation
– 2 online & 2 offline
▪ PASS program
– Unregistered students can also join
▪ Forum
– Strongly recommended
▪ Instant answers from tutors or other experienced students
▪ Others can benefit from your questions.
2
Basic Elements of Python:
Core Data Types
Core Data Types in Python
▪ Atomic types:
– Indivisible which represents only a single data value
– E.g. Integer (type int), floating-point (type float), Boolean type
(type bool)
▪ Collective types:
– Comprised of more than one data values
– E.g. String (type str)
4
Numbers in Python
5
Booleans in Python
It is ==,
▪ Standard logical operations supported: not =
– and, or , not
6
Strings in Python
7
String Manipulation
▪ Common usage:
– Presentation of computational results as some form of output
– Using the built-in function print() a = 1
b = 2
print(“result is”, a + b)
9
(More on) String Data Type
10
(More on) String Data Type
11
String Data Type
▪ Concatenation of strings:
– Attach individual strings to one another to form larger strings
– With the use of overloaded ‘+’ operator
str.join()
NOTE: To change a string, you must make a NEW string. Strings are immutable,
meaning that they cannot be `mutated’. Try last_name[0] = ‘S’, see what happens.
12
(More on) String Data Type
13
Basic Elements of Python:
Operators and Expressions
The Composition of a Python Program
15
Operators and Expressions
▪ Expressions:
– Composition of operators and data objects (i.e. data values)
– Evaluated to a value of a specific data type
– Evaluated results can be assigned to variables for further
manipulation
▪ Operator:
– Arithmetic operator
– Relational operator
– Logical or boolean operators
16
Arithmetic Operators
18
Arithmetic Expressions
▪ Arithmetic expressions:
– Composed by arithmetic operators and numbers (of type int or
float)
– Evaluated to a value of either int or float
– If both of the operands are int , the result is usually an int
– If one of the two operands is a float, the result is a float
19
Question
A. 2, 14, 2
B. 14, 14, 2
C. 14, 2, 14
D. 2, 2, 14
20
Relational Expressions
▪ Relational expressions:
– Logical expressions evaluated to either True or False
– Formed by relational operators and any data objects
▪ Evaluation:
– If the object types are int or float, the values are compared
based on the relative numerical order
– If the object types are str, the values are compared based on the
lexicographical order >>> 23 < 123
>>> True
>>> ‘xyz’ < ‘xy’
>>> False
>>> 123 == “123”
>>> False
>>> 789 < “789”
>>> TypeError
22
(Compound) Relational Expressions
>>> 2 + 3 <= 7 - 2
>>> True
>>> 5 / 2 == 5 // 2
>>> False
>>> 6 / 2 == 6 // 2
>>> True
24
Logical or Boolean Operators
▪ OR operator: a or b
– Evaluated to True if either a or b is True or both a and b are True
▪ NOT operator:
– To invert the Boolean value of the operand
– If a is True: not a will turn a into False
25
Logical Expressions
26
(Compound) Logical Expressions
>>> x = 6
>>> y = 9
>>> x % 3 == 0 and x < 0
>>> False
>>> x < 10 and x < y
>>> True
>>> x + y > 10 or x + y < 10
>>> True
Logical expressions are often used as the conditions that control the
flow of execution of a program, which are specified by the control
structures defined within the program.
28
Standard Input and Output in Python
Input and Output
▪ Input:
– Data needed for solving a specific computational problem
▪ Output:
– Presentation of the computational results
a = 1
b = 2
result = a + b
print(“The addition of a and b is”, result)
30
Standard Input
31
Standard Output
32
Question
33
Standard Output
type conversion
34
Opening Files
35
File Input
36
File Output
37
Basic Elements of Python:
Statements and Assignments
Python Statements
▪ Statements:
– Instructions (commands) of a Python program that are
interpretable and executable by the Python interpreter
▪ Assignment statements:
– Binding a data object (representing specific type of value) to a
variable
– Assigning the result of an expression to a variable
39
Single-Line Statements
▪ Single-line statements:
– Each Python statement spans a single line of code
https://www.python.org/dev/peps/pep-0008/#maximum-line-length
40
Statement Blocks
▪ Statement blocks:
– Certain programming constructs that span across multiple lines of
code
– Control structures that determine the flow of program execution
41
An Example of Statement Blocks
flag = True
if flag == True:
print(“YES”)
print(“It is true!”)
else:
print(“NO”)
print(“It is false!”)
42