Python Lesson 4 Basic Operators
Python Lesson 4 Basic Operators
Formatting
Dr. Saima Nisar
PhD (IT), MS (IT), BBA (Hons.)
saima.nisar@xmu.edu.my
A2-448
Learning Objectives
operators
objects
Arithmetic Operators
• Example:
number = 1 + 2 * 3 / 4.0
dividend % divisor
• Example:
remainder = 11 % 3
print(remainder) # Output: 2
Exponentiation Operator
• Example:
x = 7 ** 2
y = 2 ** 3
print(x) # Output: 49
print(y) # Output: 8
String Operators
• Example:
repeating sequence.
• Example:
• Example:
even_numbers = [2, 4, 6, 8]
odd_numbers = [1, 3, 5, 7]
• Example:
even_numbers = [2, 4, 6, 8]
all_numbers.sort() #
odd_numbers = [1, 3, 5, 7]
Ascending order
[1, 2, 3, 4, 5, 6, 7, 8] all_numbers.sort(reverse=True)
print(all_numbers)
Repeating Lists
• Example:
print([1, 2, 3] * 3)
# Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
String Formatting: Overview
• Example:
name = “John”
• Example:
name = "John” # String variable for the name
age = 23 # Integer variable for the age
# Formatting the string using % operator
print("%s is %d years old." % (name, age))
# Output John is 23 years old
String Formatting: Formatting Objects
• Example: