Basic Operators & Strings
Formatting
Dr. Saima Nisar
PhD (IT), MS (IT), BBA (Hons.)
[email protected]
A2-448
Learning Objectives
• In this chapter, you will learn:
o Basic Arithmetic, modulo, exponentiation, string and list
operators
o String formatting, multiple specifiers and formatting
objects
Arithmetic Operators
• Python supports standard arithmetic operators:
Addition (+), Subtraction (-), Multiplication (*), Division (/)
Modulus (%), Exponentiation (**)
• Example:
number = 1 + 2 * 3 / 4.0
print(number) # Output: 2.5
Modulo Operator
• Modulo operator (%) returns the remainder of a division:
dividend % divisor
• Example:
remainder = 11 % 3
print(remainder) # Output: 2
Exponentiation Operator
Python used Exponentiation (**) to raise numbers to a power.
• Example:
x = 7 ** 2
y = 2 ** 3
print(x) # Output: 49
print(y) # Output: 8
String Operators
Python supports concatenating strings using the addition operator.
• Example:
helloworld = "hello" + " " + "world“
print(helloworld) # Output: hello world
String Operators
Python also supports multiplying strings to form a string with a
repeating sequence.
• Example:
text = "hello " * 3
print(text) # Output: hello hello hello
Join List with Addition Operator
List can be join or concatenate using the addition operators.
• Example:
even_numbers = [2, 4, 6, 8]
odd_numbers = [1, 3, 5, 7]
all_numbers = odd_numbers + even_numbers
print(all_numbers) # Output: [1, 3, 5, 7, 2, 4, 6, 8]
Sorting Lists
• The sort() method sorts the list ascending by default.
• Example:
even_numbers = [2, 4, 6, 8]
all_numbers.sort() #
odd_numbers = [1, 3, 5, 7]
Ascending order
print(all_numbers) # Output: all_numbers = odd_numbers + even_numbers
[1, 2, 3, 4, 5, 6, 7, 8] all_numbers.sort(reverse=True)
print(all_numbers)
Repeating Lists
• The * operator to create a new list with repeated elements.
• Example:
print([1, 2, 3] * 3)
# Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
String Formatting: Overview
Python uses C-style string formatting to create new, formatted
strings. The "%" operator is used to format a set of variables
enclosed in a "tuple" (a fixed size list), together with a format string,
which contains normal text together with "argument specifiers",
special symbols like "%s" and "%d".
String Formatting: Overview
• Python uses % for formatting strings
• Variables are formatted inside a tuple with format specifiers
• Example:
name = “John”
print("Hello, %s!" % name)
# Output "Hello, John!"
String Formatting: Multiple Specifiers
• Use parentheses to format multiple values
• 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
• Any object can be formatted using %s.
• Example:
mylist = [1, 2, 3] # Define a list with three integers
# Format the string using % operator to include the list
print("A list: %s" % mylist) # Output A list: [1, 2, 3]
Basic Argument Specifiers
• %s String (or any object with a string representation, like
numbers)
• %d Integer
• %f Floating point numbers
• %.<number of digits>f Floating point numbers with a fixed
amount of digits to the right of the dot.
• %x / %X Integers in hex representation (lowercase/
uppercase)
Thank you