Lecture - 6
COMPUTER SYSTEMS AND
PROGRAMMING
(ME-2609)
Data Types
Data Types
Type Conversion
• Python allows programmer
to convert the data types.
• Built in functions
• int()
• float()
Operators Arithmetic
Comparison
Assignment
Python Operators Logical
Bitwise
Membership
Identity
Arithmetic Operators
Operator Description Example
+ Addition A+B
- Subtraction A–B
* Multiplication A*B
/ Division A/B
% Modulus A%B
** Exponent A ** B
// Floor Division A // B
Arithmetic Operators
Comparison Operators
Operator Description Example
== Equality A == B
!= Not equal to A != B
< Less than A<B
> Greater than A>B
<= Less than equal to A <= B
>= Greater than equal to A >= B
Comparison Operators
Assignment Operators
Operator Description Example
= Assignment A = 10
+= Add and assign A += B A = A + B
-= Subtract and assign A -= B A = A - B
*= Multiply and assign A *= B A = A * B
/= Divide and assign A /= B A = A / B
%= Remainder and assign A %= B A = A% B
**= Exponent and assign A **= B A = A ** B
//= Floor division and assign A //= B A = A // B
Logical Operators
Operator Description Example
and Returns True if both A <5 and B > 10
statements are true
or Returns True if one of the A <5 or B > 10
statements is true
not Reverse the result, Not(A <5 and B > 10)
returns False if the result
is true
Bitwise Operators
Suppose
Operator Description Example
A = 10 -> 00001010
B = 20-> 00010100
& Binary AND A&B
Result = 00000000 | Binary OR A|B
~ Binary NOT ~A
Suppose ^ Binary XOR A^B
A = 10 -> 00001010 << Shift left A << B
B = 10-> 00001010 >> Shift right A >> B
Result = 00001010
Operator Precedence
• When a expression or statement have multiple operators, then
python must know which one to execute first.
• This is called operator precedence.
x = 2 + 4 * 3 - 4 / 5 * 6
Operator Precedence
• In this case, Python follows a rule
• Parentheses are always respected
• Exponentiation (raise to a power)
• Multiplication, Division, and Remainder
• Addition and Subtraction
• Left to right
Operator Precedence Example
x = 1 + 2 ** 3 / 4 * 5
x = 1 + 8 / 4 * 5
x = 1 + 2 * 5
x = 1 + 10
x =11
Exercise
1. x = (4 + 2) * 3 - 4 * 5
-2
2. x = 1 + 2 * 4 / 4 * 10
21
Simple Exercise
• Simple Pay calculator
• Hours = 40/week
• Rate = 100/hr
• Pay ????
Program:
Hours = 40
Rate = 100
Pay = Hours * Rate
Conditional Execution
• Normally program flow is sequential.
• Change the flow of program based on the condition.
• Check the condition and change the behavior of the program
according
Conditional Execution
• The general form of a typical decision
making structure found in most of the
False
programming languages Condition
True
Statements
Conditional Statements in Python
• Python programming language provides following types of
decision-making statements
IF statement
Nested statements
IF-ELSE statement
IF-ELIF statements
If Statement
False
Syntax: Condition
if condition:
True
• statements to be executed;
Statements
Simple Analogy
• Your mobile Phone alarm
• Alarm is set for a specific time
• Phone will start Buzzing on the set time
• Digital calendar automatically changes the date of
mobile phone
Example Program
Program
False
x = 10 x>0
if x>0: True
print(‘x is a positive
number’)
Print(‘x is a positive number’)
Example Program
x=5
if x == 5 : Equals 5
print('Equals 5')
if x > 4 : Greater than 4
print('Greater than 4') Greater than or Equals 5
if x >= 5 :
print('Greater than or Equals 5') Less than 6
if x < 6 : print('Less than 6') Less than or Equals 5
if x <= 5 :
print('Less than or Equals 5') Not equal 6
if x != 6 :
print('Not equal 6')
Indentation
• Indent defines the scope of the block (Lines of code).
• In Python language, indentation refers to spaces and tabs.
• The statements with the same indentation belongs to same
group
• Increase indent shows the beginning of block
• Decrease indent shows the end of block
Examples
Example
Nested Statements
True
Syntax: Condition
if condition: False
True
• statements to be Condition
executed;
•if condition: False
Statements
• statements to be
executed;
Statements
Example
True
Program x >0
x = 5 False
if x > 0 : True
x < 10
if x < 10:
print(‘x is a positive
False
single digit number’) Print
Statement
Simplified Condition using Logical Operators
• Recall logical operators
AND
OR
• Provides a way to simplify the nested statements. NOT
• Can check multiple conditions in a single statement
Program
x =
if 0 < x and x < 10:
print(‘x is a positive single digit number’)
If-Else Statement
• There are two possibilities and condition determines which will be
executed.
• Multi-way Decision structure.
Simple Analogy
• Automatic barrier has two states
• Opened
• Closed
• Condition determines which state to follow
if sensor == True:
print(‘Barrier opened’)
else:
print(‘Barrier closed’)
Simple Analogy
• Email login
• Social Networking Login
If-Else Statement
False
Syntax: Condition
if condition:
True
• statements to be executed; Else block
else :
If block
statements to be executed;
Example Program x=6
• Program to determine either
number is positive or negative
True False
Program x> 0
x = 6
if x > 0 : Print(‘Positive’) Print(‘Negative’)
print(‘x is positive’)
else :
print(‘x is negative’)
print(‘Done’)
Print(‘Done’)
Example Program x=6
• Program the determine the number is even or odd
Program True False
x = 6 x % 2==0
if x % 2 == 0 :
print(‘x is even’)
else : Print(‘Even’) Print(‘Odd’)
print(‘x is odd’)
print(‘Done’)
Print(‘Done’)
Example Program
• Re-write the same program to the get the input from user
Program
x = int(input('Enter the number:'))
if x % 2 == 0 :
print(‘x is even’)
else :
print(‘x is odd’)
print(‘Done’)
Exercise
Exercise
• Write a program to determine the Maximum of two numbers
if-elif Statements
• Provides a way to check the multiple test conditions.
• No limit on number of conditions.
• Only one statement executes.
• Else means all are false
• Else is optional.
If-elif Statements
Syntax:
if condition-1:
statements to be executed;
elif condition-2:
statements to be executed;
elif condition-3:
statements to be executed;
else:
statements to be executed;
If-elif Statements
True
Condition Statements
False
True
Condition Statements
False
True
Condition Statements
False
Statements
Example
Program:
if x < 2 :
• print('small')
elif x < 10 :
• print('Medium')
else :
• print('LARGE')
print('All done')
•
Example
Simple Python calculator
Choice Operation
Takes two numbers from user
along with the choice of 1 Addition
operation 2 Subtraction
3 Multiplication
4 Division
Example
Simple Python calculator