Chapter 7  Operators and Expressions
 Authors
 Ms. Samavi Salman
 Dr. Rao Muhammad Adeel Nawab
 Supporting Material
Quick Recap
 Quick Recap – Variables in Python
In previous Chapter, I presented the following main concepts:
 Variable
 A Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Data (Values / Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
 Three main Components of a Variable are
 Variable Name
 Value (stored) in a Variable (i.e., Content of a Variable)
 Memory Location / Memory Address (used to store Value of a Variable)
 Main Operations on a Variable
 The main Operations performed on a Variable are
 Operation 01
 Store Value in a Variable
 Operation 02
 Modify / Update Value of a Variable
 Operation 03
 Use Value of a Variable for various Tasks
 In Python, there are three main Types of Variables
 Integer Variables
 Float Variables
 String Variables
 Integer Variable
 An Integer Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Integer Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
 Float Variable
 A Float Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store FloatingPoint Values (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
 String Variable
 A String Variable is defined as the Name Assigned to a Memory Location / Memory Address (to store Fixed Sequence of Characters (Constants)), that can change (i.e., does not remain the same) every time a Python Program is executed
 input () Function
 The input() Function takes User input (reads a Line) convert it in a String and return it
 Code Time Variable Value vs Run Time Variable Value
 Code Time Variable Value
 At Code Time, the Value to be stored in a Variable is
 Decided by the Programmer
 At Code Time, the Value to be stored in a Variable is
 Run Time Variable Value
 At Run Time, the Value to be stored in a Variable is
 Decided by the User (of the Program / Software)
 At Run Time, the Value to be stored in a Variable is
 Code Time Variable Value
 Dry Run
 The testing process which reduces the effect of Failure is called Dry Run
 Check Data Type of Variable using type() Function
 Check Memory Address of a Variable using id() Function
 The testing process which reduces the effect of Failure is called Dry Run
 Variable Naming Conventions – Best Coding Practices
 Best Coding Practice 01
 Variable Name should be in lower case
 Best Coding Practices 02
 Use expressive Variable Names
 Best Coding Practices 03
 Use underscore to separate words in long Variable Names
 Best Coding Practice 01
Operation, Operator and Operand
 Operation
 Definition
 In Computer Programming, an Operation is defined as an action that is carried out to accomplish a given Task
 Purpose
 The main purpose of an Operation is to
 accomplish a given Task
 The main purpose of an Operation is to
 Five Basic Computer Operations
 The five basic Computer Operations are
 Input
 Processing
 Output
 Store
 Control
 Performing Various Operations in a Computer
 Question
 Who performs various Operations in a Computer?
 Answer
 Processors (a.k.a. CPU (Central Processing Unit))
 Performing Complex Tasks using Computers
 Question
 How CPUs perform complex Tasks?
 Answer
 CPUs perform complex Tasks by executing thousands of individual Operations per Second
 Main Types of Operations
 Question
 What are main Types of Operations performed by a CPU?
 Answer
 CPU performs two main Types of Operations
 Arithmetic Operations
 Logical Operations
 CPU performs two main Types of Operations
 Unit of CPU Performing Arithmetic and Logical Operations
 Question
 Which Unit of CPU performs Arithmetic and Logical Operations?
 Answer
 ArithmeticLogic Unit (ALU)
 Operator
 Definition
 In Computer Programming, an Operator is defined as a special Symbol, which is used to carry out Arithmetic / Logical Operations
 Purpose
 The main purpose of Operators is to
 Carry out different Operations to accomplish a given Task
 The main purpose of Operators is to
 Importance
 To perform a given Task, we need to perform various Operations
 Without Operators, we cannot perform Operations
 Applications
 Operators are used to perform
 Arithmetic and Logical Operations to perform various useful Tasks
 Operators are used to perform
 Operand
 In Computer Programming, an Operand is defined as the Object (or Value) on which an Operator operates
Expression
 Expression
 Definition
 In Programming Language, an Expression is a combination of Operands (Variables / Constants) and Operator(s) to produce an Output Value
 Note
 An Expression is a Python Statement which returns a Value
 Purpose
 The main purpose of an Expression is to perform various Operations to
 accomplish a given Task
 The main purpose of an Expression is to perform various Operations to
 Importance
 Expressions help us to perform
 Complex Calculations and Tasks
 Expressions help us to perform
 Main Components of an Expression
 An Expression comprises of Two Main Components
 Operator
 Operands
 Note
 In an Expression, we must have at least
 One Operator and
 Two Operands
 In an Expression, Operands can be either
 Variables or
 Constants
 In an Expression, we must have at least
 Valid Expressions vs Invalid Expressions
For an Expression to be Valid, it must satisfy the following Conditions
Conditions for a Valid Expressions
 Condition 01
 A Valid Expression will have at least
 One Operator and
 Two Operands
 A Valid Expression will have at least
 Condition 02
 Operands in an Expression should be either
 Variables or
 Constants
 Operands in an Expression should be either
 Condition 03
 When a Variable is used in an Expression, it must be
 first declared and Value must be Stored in it before using it in an Expression
 Note
 If we use a Variable without Declaration and Storing a Value in it
 An Error will be generated, and Python Program will not be executed
 When a Variable is used in an Expression, it must be
 Condition 04
 Since an Expression returns an Output Value, we may use an Expression in either
 print() Statement or
 Assignment Statement
 Note
 If we don’t use the Result (Output Value) returned by an Expression in either: (1) print() Statement – to display Output Value on the Output Screen, or (2) Store it in a Variable using Assignment Statement – for further processing
 An Error will be generated, and Python Program will not be executed
 Since an Expression returns an Output Value, we may use an Expression in either
 Condition 05
 If an Expression is used in an Assignment Statement, then
 We must have a Variable on Left Hand Side (LHS) of the Assignment Statement and
 Expression should come on the RightHand Side (RHS) of the Assignment Statement
 Note
 If we do not use a Variable on the LHS of an Assignment Statement
 An Error will be generated, and Python Program will not be executed
 If an Expression is used in an Assignment Statement, then
 Valid Expressions vs Invalid Expressions Cont…
Task Description
 Task Description
 Consider the following Python Program and answer the questions given below
 Questions
 Identify Expression(s) in the given Python Program?
 Classify the Expressions identified in the first Question as either: (1) Valid Expression or (2) Invalid Expression?
 Note
 Your answers should be
 Well Justified
 Your answers should be
 Questions
 Python Program
 Consider the following Python Program and answer the questions given below
print("5 + 2 = ", 5 + 2)
number1 = 10  7
number2 = number1 + 10
print("Square of number1 is = " , number1 * number1)
number2 + number1
Expressions in Python Program
 The above Python Program contains five Expressions
 Expression 01
 5 + 2
 Expression 02
 10 – 7
 Expression 03
 number + 10
 Expression 04
 number * number
 Expression 05
 number2 + number1
 Expression 01
 Example 1 – Valid Expressions vs Invalid Expressions Cont…
Checking Validity of Expression 01
 Python Statement
 print(“5 + 2 = “, 5 + 2)
 Expression 01 (used in print() Statement)
 Expression 01 is
 5 + 2
 Expression 01 is
Analysis of Expression
 Analyze Expression 01
 Operators in Expression 01
 Operator 01
 +
 Operator 01
 Operands in Expression 01
 Operand 01
 5
 Operand 02
 2
 Operand 01
 Expression (5 + 2) is used in print() Statement
 Therefore, the Result (Output Value) produced by the Expression will be displayed on the Output Screen
 Operators in Expression 01
Summary – Analysis of Expression 01
 In Expression 01
 Operator is
 Arithmetic Operator
 Both Operands are
 Constants
 Operator is
 Result (Output Value) produced by the Expression will be displayed on the Output Screen using print() Function
Main Findings
 Finding 01
 Expression 01 satisfies
 Condition 01, Condition 02 and Condition 04
 Expression 01 satisfies
Conclusion
 Expression 01 is a
 Valid Expression
Reason – Why Expression 01 is a Valid Expressions
 Expression 01 satisfies
 Condition 01, Condition 02 and Condition 04
 Example 2 – Valid Expressions vs Invalid Expressions Cont…
Checking Validity of Expression 02
 Python Statement
 number1 = 10 – 7
 Expression 02 (used in Python Statement)
 Expression 02 is
 10 – 7
 Expression 02 is
Analysis of Expression
 Analyze Expression 02
 Operators in Expression 02
 Operator 01
 –
 Operator 01
 Operands in Expression 02
 Operand 01
 10
 Operand 01
 7
 Operand 01
 Expression 10 – 7 is used in Assignment Statement
 Therefore, the Result (Output Value) produced by the Expression will be stored in Variable (number1)
 Operators in Expression 02
Summary – Analysis of Expression 02
 In Expression 02
 Operator is
 Arithmetic Operator
 Both Operands are
 Constants
 Operator is
 Result (Output Value) produced by the Expression will be stored in Variable (number1)
Main Findings
 Finding 01
 Expression 02 satisfies
 Condition 01, Condition 02, Condition 04 and Condition 05
 Expression 02 satisfies
Conclusion
 Expression 02 is a
 Valid Expression
Reason – Why Expression 02 is a Valid Expressions
 Expression 02 satisfies
 Condition 01, Condition 02, Condition 04 and Condition 05
 Example 3 – Valid Expressions vs Invalid Expressions Cont…
Checking Validity of Expression 03 

Analysis of Expression 

Summary – Analysis of Expression 03 

Main Findings 

Conclusion 

Reason – Why Expression 03 is a Valid Expression 

 Example 4 – Valid Expressions vs Invalid Expressions Cont…
Checking Validity of Expression 04 

Analysis of Expression 

Summary – Analysis of Expression 04 

Main Findings 

Conclusion 

Reason – Why Expression 04 is a Valid Expression 

 Example 5 – Valid Expressions vs Invalid Expressions Cont…
Checking Validity of Expression 05 

Analysis of Expression 

Summary – Analysis of Expression 05 

Main Findings 

Conclusion 

Reason – Why Expression 05 is a Valid Expression 

TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
 Task
 Consider the following Python Program and answer the questions given below
 Python Statements
 print(“5 ** 2 = “, 5 ** 2)
 number1 = 36 – 46
 number2 = number1 * 50
 print(“Cube of number1 is = ” , number1 * number1 * number1)
 number2 % number1
 Note
 Your answer should be
 Well Justified
 Your answer should be
 Questions
 Identify Expressions in above Python Program?
 Classify each Expression as either Valid Expression or Invalid Expression using the following Template
 Checking Validity of Expression
 Analyze the Expression
 Operands
 Operators
 Summary – Analysis of Expression
 Main Findings
 Conclusion
 Reason – Why Expression is Valid / Invalid
Your Turn Tasks
Your Turn Task 1
 Task
 Write a Python Program (like the one given in the TODO Task) and answer the questions given below
 Note
 Your answer should be
 Well Justified
 Your answer should be
 Questions
 Identify Expressions in your Python Program?
 Classify each Expression as either Valid Expression or Invalid Expression using the following Template
 Checking Validity of Expression
 Analyze the Expression
 Operands
 Operators
 Summary – Analysis of Expression
 Main Findings
 Conclusion
 Reason – Why Expression is Valid / Invalid
Arithmetic Operators
 Three Main Components of a Variable
Types of Operators
 The main Types of Operators (in Python) are
 Assignment Operator
 Arithmetic Operators
 Logical Operators
 Comparison Operators
 Membership Operators
 Identity Operators
 Three Main Components of a Variable
Chapter Focus
 The focus of this Chapter is on
 Arithmetic Operators
Assignment Operator
 Assignment Operator
 Definition
 Assignment Operator is represented with = Symbol
 Purpose
 The main purpose of an Assignment Operator is to
 Store Value in a Variable
 The main purpose of an Assignment Operator is to
 Importance
 To perform various Operations on a Variable,
 We must first store value in that Variable using Assignment Operator
 To perform various Operations on a Variable,
 Applications
 Assignment Operator is used to
 Store Value in a Variable
 Modify / Update Value of a Variable
 Assignment Operator is used to
Arithmetic Operator
 Arithmetic Operator
 Definition
 An Arithmetic Operator is an Operator that takes two Operands and performs Calculation on them
 Purpose
 The main purpose of Arithmetic Operators is to
 Perform various Arithmetic Calculations like addition, subtraction, division, multiplication etc.
 The main purpose of Arithmetic Operators is to
 Importance
 Arithmetic Operators are used to perform various Arithmetic Calculations, which help us to accomplish complex Tasks
 Applicators
 Arithmetic Operators are useful to carry out
 Arithmetic Calculations
 Arithmetic Operators are useful to carry out
 Types of Arithmetic Operators in Python
 In Python, we have following six Arithmetic Operators
 Addition
 +
 Subtraction
 –
 Multiplication
 *
 Division
 /
 Exponent
 **
 Modulus
 %
 Addition
Addition Operator
 Assignment Operator
 Symbol
 +
 Purpose
 Add Two Values
 + Operator Usage in an Expression
 Operand 01 + Operand 02
 How + Operator Works
 Adds Operand 01 and Operand 02 and
 returns a Single Output Value
 Adds Operand 01 and Operand 02 and
 Example 1  Addition Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
17 
Reason – Why Expression Returned Output Value of 17 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Addition Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
15 
Reason – Why Expression Returned Output Value of 15 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Addition Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
5.2 
Reason – Why Expression Returned Output Value of 5.2 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1 vs Example 2 vs Example 3
Example 1 – Expression is 7 + 10 

Example 2 – Expression is number1 + 5 

Example 3 – Expression is number1 + number2 

Analysis 

Conclusion 

Subtraction Operator
 Subtraction Operator
 Symbol
 –
 Purpose
 Subtract One Value from another Value
 – Operator Usage in an Expression
 Operand 01 – Operand 02
 How – Operator Works
 Subtracts Operand 02 from Operand 01 and
 returns a Single Output Value
 Subtracts Operand 02 from Operand 01 and
 Example 1  Subtraction Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
3 
Reason – Why Expression Returned Output Value of 3 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Subtraction Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
5 
Reason – Why Expression Returned Output Value of 5 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Subtraction Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
2.4 
Reason – Why Expression Returned Output Value of 2.4 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1 vs Example 2 vs Example 3
Example 1 – Expression is 10 – 7 

Example 2 – Expression is number1 – 10 

Example 3 – Expression is number1 – number2 

Analysis 

Conclusion 

Multiplication Operator
 Multiplication Operator
 Symbol
 *
 Purpose
 Multiply Two Values
 * Operator Usage in an Expression
 Operand 01 * Operand 02
 How * Operator Works
 Multiplies Operand 01 with Operand 02 and
 returns a Single Output Value
 Multiplies Operand 01 with Operand 02 and
 Example 1  Multiplication Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
70 
Reason – Why Expression Returned Output Value of 70 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Multiplication Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
50 
Reason – Why Expression Returned Output Value of 50 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

Exponent Operator
 Exponent
 Definition
 Exponent (or Power) of a Number tells us how many times a Number will be multiplied by itself
 Example 1
 72 = 7 x 7 = 49
 In above Example
 Number = 7
 Exponent (or Power) = 2
 Example 2
 34 = 3 x 3 x 3 x 3 = 81
 In above Example
 Number = 3
 Exponent (or Power) = 4
 Exponent Operator
 Symbol
 **
 Purpose
 Take Exponent (Power) of a Number
 ** Operator Usage in an Expression
 Operand 01 ** Operand 02
 Note
 Operand 01 is a Number and
 Operand 02 is its Exponent (Power)
 How ** Operator Works
 Operand 01 is multiplied by itself N times (where N is the Value of Operand 02) and
 returns a Single Output Value
 Operand 01 is multiplied by itself N times (where N is the Value of Operand 02) and
 Example 1  Exponent Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
16 
Reason – Why Expression Returned Output Value of 16 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Exponent Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
25 
Reason – Why Expression Returned Output Value of 25 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Exponent Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
6.48 
Reason – Why Expression Returned Output Value of 6.48 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1 vs Example 2 vs Example 3
Example 1 – Expression is 2 ** 4 

Example 2 – Expression is number1 ** 2 

Example 3 – Expression is number1 ** number2 

Analysis 

Conclusion 

Division Operator
 Division Operator
 Symbol
 /
 Purpose
 Divide One Value with another Value
 / Operator Usage in an Expression
 Operand 01 / Operand 02
 How / Operator Works
 Divide Operand 01 by Operand 02 and
 returns a Single Output Value
 Divide Operand 01 by Operand 02 and
 Dividend, Divisor and Quotient
 In Division, a Value (Number) is divided by any other Value (Number) to get another Value (Number) as Output
 Dividend
 The Value (Number) which is getting divided is called Dividend
 Divisor
 The Value (Number) which divides a given Value (Number) is called Divisor
 Quotient
 The Output Value (Number) which we get as a result is called Quotient
 Example 1  Dividend, Divisor and Quotient
Task Description 

Expression, Dividend, Divisor and Quotient 

 Example 2  Dividend, Divisor and Quotient
Task Description 

Expression, Dividend, Divisor and Quotient 

 Example 3  Dividend, Divisor and Quotient
Task Description 

Expression, Dividend, Divisor and Quotient 

 Example 4 Dividend, Divisor and Quotient
Task Description 

Expression, Dividend, Divisor and Quotient 

 Comparing Example 1, Example 2, Example 3 and Example 4
Example 1 – Expression is 10 / 4 

Example 2 – Expression is 10.0 / 4.0 

Example 3 – Expression is 10.0 / 4 

Example 4 – Expression is 10 / 4.0 

Analysis 

Conclusion 

 Types of Division
 In Python, two main Types of Division are
 Integer Division
 Float Division
 Integer Division
 Definition
 In Integer Division, both Dividend (Operand 01) and Divisor (Operand 02) must be Integer Values (Constants)
 Consequently, Quotient (Output Value) will also be an Integer Value (Constant)
 In Integer Division, both Dividend (Operand 01) and Divisor (Operand 02) must be Integer Values (Constants)
 Integer Division  Dividend, Divisor, Quotient and Remainder
 In Integer Division, an Integer Value (Number) is divided by any other Integer Value (Number) to get another Integer Value (Number) as Output
 Dividend
 The Integer Value (Number) which is getting divided is called Dividend
 Divisor
 The Integer Value (Number) which divides a given Value (Number) is called Divisor
 Quotient
 The Output (Integer) Value (Number) which we get as a result is called Quotient
 Remainder
 Remainder is an Integer Value, that is left over after dividing Dividend with Divisor to produce Quotient
 Example – Integer Division (Dividend, Divisor, Quotient and Remainder)
 Task Description
 Identify the Values of Dividend, Divisor Quotient and Remainder in the following Expression
 Expression, Dividend, Divisor and Quotient
 Expression
 int(10 / 4) = 2
 Dividend
 10
 Divisor
 4
 Quotient
 2
 Remainder
 2
 Expression
 Float Division
 Definition
 In Float Division, both (Dividend and Divisor) or one of them must be a Float Value (Constant)
 Consequently, Quotient (Output Value) will also be a Float Value (Constant)
 In Float Division, both (Dividend and Divisor) or one of them must be a Float Value (Constant)
 Example – Float Division (Dividend, Divisor, Quotient and Remainder)
 Task Description
 Identify the Values of Dividend, Divisor Quotient and Remainder in the following Expression
 Expression, Dividend, Divisor and Quotient
 Expression
 10.0 / 4.0 = 2.5
 Dividend
 10.0
 Divisor
 4.0
 Quotient
 2.5
 Remainder
 0
 Expression
 Important Note
 By default, Python performs Float Division on all Numbers (either Integer or Float)
 Example 1 – Division in Python
 10 / 4 = 2.5
 Example 2 – Division in Python
 10.0 / 4.0 = 2.5
 Note that Quotient of Example 1 and Example 2 are exactly same
 Although in Example 1, both Numbers are Integer and
 In Example 2, both Numbers are Float
 Performing Integer Division in Python
 Question
 How can we perform Integer Division in Python?
 Answer
 Use int() Type Casting Function
 Examples – int() Type Casting Function
 Example 1
 int(10 / 4) = 2
 Example 2
 int(10.0 / 4) = 2
 Example 3
 int(10 / 4.0) = 2
 Example 4
 int(10.0 / 4.0) = 2
Integer Division
 Example 1 – Integer Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
2 
Reason – Why Expression Returned Output Value of 2 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2 – Integer Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
0 
Reason – Why Expression Returned Output Value of 0 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3 – Integer Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
3.0 
Reason – Why Expression Returned Output Value of 3.0 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1, Example 2, Example 3 and Example 4
Example 1 – Expression is 10 / 5 

Example 2 – Expression is number1 / 10 

Example 3 – Expression is number1 / number2 

Analysis 

Conclusion 

Float Division
 Example 1 – Float Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
1.43 
Reason – Why Expression Returned Output Value of 1.43 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Float Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
0.5 
Reason – Why Expression Returned Output Value of 0.5 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Float Division using / Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
2.71 
Reason – Why Expression Returned Output Value of 2.71 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1, Example 2, Example 3 and Example 4
Example 1 – Expression is 10.0 / 7.0 

Example 2 – Expression is number1 / 10.0 

Example 3 – Expression is number1 / number2 

Analysis 

Conclusion 

Modulus Operator
 Modulus Operator
 Symbol
 %
 Purpose
 Divide One Value (Number) by another Value (Number) and return the Remainder
 % Operator Usage in an Expression
 Operand 01 % Operand 02
 How % Operator Works
 Divide Operand 01 with Operand 02 and
 returns a Single Output Value
 Divide Operand 01 with Operand 02 and
 Important Note
 We can only use Modulus (%) Operator, when
 Both Operands (Dividend and Divisor) are
 Integer Values (Constants)
 Both Operands (Dividend and Divisor) are
 We can only use Modulus (%) Operator, when
 Example 1  Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
3 
Reason – Why Expression Returned Output Value of 3 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
5 
Reason – Why Expression Returned Output Value of 5 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Output – Returned by Expression 
0 
Reason – Why Expression Returned Output Value of 0 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Comparing Example 1, Example 2, Example 3 and Example 4
Example 1 – Expression is 10 % 7 

Example 2 – Expression is number1 % 10 

Example 3 – Expression is number1 % number2 

Analysis 

Conclusion 

 Difference between Division Operator and Modulus Operator
 Division Operator /
 When we divide an Integer Value (Dividend) by any other Integer Value (Divisor), / Operator returns
 Quotient
 When we divide an Integer Value (Dividend) by any other Integer Value (Divisor), / Operator returns
 Modulus Operator %
 When we divide an Integer Value (Dividend) by any other Integer Value (Divisor), % Operator returns
 Remainder
 When we divide an Integer Value (Dividend) by any other Integer Value (Divisor), % Operator returns
 Important Note
 By default, in Python, for Modulus Operator
 Integer Numbers are treated as Integers
 There is no need to use int() Type Casting Function
 Example 1
 10 % 7 = 3
 Example 2
 7 % 13 = 7
 Example 1  Division Operator and Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Expression 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 2  Division Operator and Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Expression 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 3  Division Operator and Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Expression 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

 Example 4  Division Operator and Modulus Operator
Completely and Correctly Understand the Task 
Task Description 

Plan and Design Solution to the Task 
InputProcessingOutput 

Expression 

Analysis, Main Findings and Conclusions 
Analysis 

Main Findings 

Conclusions 

TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
 Task
 Consider the following Python Programs and answer the questions given below
 Note
 Your answer should be
 Well Justified
 Your answer should be
 Questions
 Write Python Programs for all the Tasks given above by following the Template given in this Chapter i.e.
 Identify following
 Expression
 Operator
 Operands
 Output returned by Expression
 Reason – Why Expression Returned Output Value
 Analysis
 Findings
 Conclusion
 Identify following
 Write Python Programs for all the Tasks given above by following the Template given in this Chapter i.e.
Completely and Correctly Understand the Task 
Task 01 

Completely and Correctly Understand the Task 
Task 02 

Completely and Correctly Understand the Task 
Task 03 

Completely and Correctly Understand the Task 
Task 04 

Completely and Correctly Understand the Task 
Task 05 

Completely and Correctly Understand the Task 
Task 06 

Completely and Correctly Understand the Task 
Task 07 

Completely and Correctly Understand the Task 
Task 08 

Completely and Correctly Understand the Task 
Task 09 

Completely and Correctly Understand the Task 
Task 10 

Completely and Correctly Understand the Task 
Task 11 

Completely and Correctly Understand the Task 
Task 12 

Completely and Correctly Understand the Task 
Task 13 

Completely and Correctly Understand the Task 
Task 14 

Completely and Correctly Understand the Task 
Task 15 

Completely and Correctly Understand the Task 
Task 16 

Completely and Correctly Understand the Task 
Task 17 

Completely and Correctly Understand the Task 
Task 18 

Your Turn Tasks
Your Turn Task 1
 Task
 Write at least 10 different Python Programs which contain Expressions (very similar to the ones given in the TODO Tasks)
 For each Python Program, perform Calculations on Expression using the following Template (given in this Chapter) i.e.
 Identify following
 Expression
 Operator
 Operands
 Output returned by Expression
 Reason – Why Expression Returned Output Value
 Analysis
 Findings
 Conclusion
Precedence of Operators
 Recap
 In previous Sections, we discussed very basic Expression with only
 One Operator and
 Two Operands
 However, there can be complex Expressions with
 Multiple Operators and
 More than Two Operands
 Calculations in Complex Expressions
 Question
 In what Order Operators will operate on Operands in a complex Expression?
 Answer
 Follow the Precedence of Operators
 Precedence of Operators
 Definition
 The Order in which Operators are applied on Operands in an Expression is called Precedence of Operators
 Purpose
 The main purpose of Precedence of Operators is to
 completely and correctly understand the process of Calculations i.e., how Calculations are made in Expressions
 The main purpose of Precedence of Operators is to
 Table  Precedence of Operators
 The Table below shows the Precedence of Operators
 Note that Level of Precedence is from Top to Bottom i.e.
 () has highest Precedence
 After (), ** Operator has Precedence
 After ** Operator, *, / and % Operators have Precedence
 After *, / and % Operators, + and – Operators have Precedence
 Note that Level of Precedence is from Top to Bottom i.e.
Precedence of Operators  
()  Parentheses 
**  Exponent 
*, /, %  Multiplication, Division, Modulus 
+, –  Addition, Subtraction 
=  Assignment Operator 
Operators with Same Precedence
 Operators with Same Precedence
 In Table 1, the following Operators have Same Precedence
 *, /, %
 +, –
 Performing Calculations on Operators with Same Precedence
 Question
 How to Perform Calculations when we have Operators with Same Precedence?
 Answer
 Perform Calculations from
 Left to Right
 Perform Calculations from
 Example 1 – Operators with Same Precedence
Completely and Correctly Understand the Task 
Task Description 

Expressions 

Plan and Design Solution to the Task 
InputProcessingOutput 

Calculation, Findings and Conclusion 
Calculations 

Findings 

Conclusion 

 Example 2 – Operators with Same Precedence
Completely and Correctly Understand the Task 
Task Description 

Expressions 

Plan and Design Solution to the Task 
InputProcessingOutput 

Calculation, Findings and Conclusion 
Calculations 

Findings 

Conclusion 

 Example 3 – Operators with Same Precedence
Completely and Correctly Understand the Task 
Task Description 

Expressions 

Plan and Design Solution to the Task 
InputProcessingOutput 

Calculation, Findings and Conclusion 
Calculations 

Findings 

Conclusion 

 Example 4 – Operators with Same Precedence
Completely and Correctly Understand the Task 
Task Description 

Expressions 

Plan and Design Solution to the Task 
InputProcessingOutput 

Calculation, Findings and Conclusion 
Calculations 

Findings 

Conclusion 

 Example 5 – Operators with Same Precedence
Completely and Correctly Understand the Task 
Task Description 

Expressions 

Plan and Design Solution to the Task 
InputProcessingOutput 

Calculation, Findings and Conclusion 
Calculations 

Findings 

Conclusion 

TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
 Task
 Consider the following Python Programs and answer the questions given below
 Note
 Your answers should be
 Well Justified
 Your answers should be
 Question
 For each Python Program, perform Calculations on Expression using the following Template (given in this Chapter) i.e.
 Identify Expression
 Calculations, Findings and Conclusion
 Calculations
 Analysis
 Findings
 Conclusion
Completely and Correctly Understand a Task 
Task 01 

Your Turn Tasks
Your Turn Task 1
 Task
 Write at least 10 different Python Programs which contain Expressions (very similar to the ones given in the TODO Tasks)
 For each Python Program, perform Calculations on Expression using the following Template (given in this Chapter) i.e.
 Identify Expression
 Calculations, Findings and Conclusion
 Calculations
 Analysis
 Findings
 Conclusion
Chapter Summary
 Chapter Summary
In this Chapter, I presented the following main concepts:
 Operation
 In Computer Programming, an Operation is defined as an action that is carried out to accomplish a given Task
 Five Basic Computer Operations
 The five basic Computer Operations are
 Input
 Processing
 Output
 Store
 Control
 The five basic Computer Operations are
 Operator
 In Computer Programming, an Operator is defined as a special Symbol, which is used to carry out Arithmetic / Logical Operations
 Operand
 In Computer Programming, an Operand is defined as the Object (or Value) on which an Operator operates
 Expression
 In Programming Language, an Expression is a combination of Operands (Variables / Constants) and Operator(s) to produce an Output Value
 Main Components of an Expression
 An Expression comprises of Two Main Components
 Operator
 Operands
 An Expression comprises of Two Main Components
 Types of Operators
 The main Types of Operators (in Python) are
 Assignment Operator
 Arithmetic Operators
 Logical Operators
 Comparison Operators
 Membership Operators
 Identity Operators
 The main Types of Operators (in Python) are
 Assignment Operator
 Assignment Operator is represented with = Symbol
 Arithmetic Operator
 An Arithmetic Operator is an Operator that takes two Operands and performs Calculation on them
 Types of Arithmetic Operators in Python
 In Python, we have following six Arithmetic Operators
 Addition
 +
 Subtraction
 –
 Multiplication
 *
 Division
 /
 Exponent
 **
 Modulus
 %
 Addition
 In Python, we have following six Arithmetic Operators
 Exponent
 Exponent (or Power) of a Number tells us how many times a Number will be multiplied by itself
 Dividend, Divisor and Quotient
 In Division, a Value (Number) is divided by any other Value (Number) to get another Value (Number) as Output
 Dividend
 The Value (Number) which is getting divided is called Dividend
 Divisor
 The Value (Number) which divides a given Value (Number) is called Divisor
 Quotient
 The Output Value (Number) which we get as a result is called Quotient
 Types of Division
 In Python, two main Types of Division are
 Integer Division
 Float Division
 In Python, two main Types of Division are
 Integer Division
 In Integer Division, both Dividend (Operand 01) and Divisor (Operand 02) must be Integer Values (Constants)
 Consequently, Quotient (Output Value) will also be an Integer Value (Constant)
 Integer Division – Dividend, Divisor, Quotient and Remainder
 In Integer Division, an Integer Value (Number) is divided by any other Integer Value (Number) to get another Integer Value (Number) as Output
 Dividend
 The Integer Value (Number) which is getting divided is called Dividend
 Divisor
 The Integer Value (Number) which divides a given Value (Number) is called Divisor
 Quotient
 The Output (Integer) Value (Number) which we get as a result is called Quotient
 Remainder
 Remainder is an Integer Value, that is left over after dividing Dividend with Divisor to produce Quotient
 Float Division
 In Float Division, both (Dividend and Divisor) or one of them must be a Float Value (Constant)
 Consequently, Quotient (Output Value) will also be a Float Value (Constant)
 Precedence of Operators
 The Order in which Operators are applied on Operands in an Expression is called Precedence of Operators
In Next Chapter
 In Next Chapter
 In Sha Allah, in the next Chapter, I will present a detailed discussion on
 Algorithm, Pseudo Code and Flow Charts