0% found this document useful (0 votes)
2 views15 pages

PythonProgramming Week3 4

The document provides an overview of operators and expressions in Python, including arithmetic, relational, logical, and bitwise operators, as well as assignment and ternary operators. It also covers conditional statements such as if, if-else, and if-elif-else, explaining their syntax and usage with examples. Additionally, it discusses operator precedence and mathematical functions available in the math module.

Uploaded by

hammadnaeem437
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

PythonProgramming Week3 4

The document provides an overview of operators and expressions in Python, including arithmetic, relational, logical, and bitwise operators, as well as assignment and ternary operators. It also covers conditional statements such as if, if-else, and if-elif-else, explaining their syntax and usage with examples. Additionally, it discusses operator precedence and mathematical functions available in the math module.

Uploaded by

hammadnaeem437
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Operators and Expressions

1.​ Arithmetic Operators


2.​ Relational (Comparison) Operators
3.​ Equality Operators
4.​ Logical Operators
5.​ Bitwise Operators
6.​ Shift Operators
7.​ Assignment Operators
8.​ Ternary (Conditional) Operator
9.​ Special Operators
a.​ Identity Operators
b.​ Membership Operators
10.​Operator Precedence
11.​Mathematical Functions (using the math module)

(A) Arithmetic Operators

Used for calculations

Operato Meaning Exampl Outpu


r e t

+ Addition 5+2 7

- Subtraction 5-2 3

* Multiplication 5*2 10

/ Division 5/2 2.5

// Floor 5 // 2 2
Division

% Modulus 5%2 1

** Power 2 ** 3 8

Example
print(10 % 3) # remainder
print(10 // 3) # integer division Divides and returns integer part only
x/0 or x//0 or x%0 always raises "ZeroDivisionError
(B) Comparison Operators

Used to compare values → result is True/False

Operato Meaning
r

== Equal

!= Not equal

> Greater

< Less

>= Greater or
equal

<= Less or equal

Example
print(5 > 3) # True
print(5 == 2) # False

Equality Operators

Used to compare values.

Operator Meaning

== Equal

!= Not equal

Example
a = 10
b=5

print(a == b) # False
print(a != b) # True
(C) Logical Operators

Used to combine conditions

Operator Meaning

and Both conditions must be True

or At least one True

not Reverse condition

Example
x = 10

print(x > 5 and x < 15) # True


print(x > 5 or x > 20) # True
print(not(x > 5)) # False

Bitwise Operators

Work on binary numbers.

Operator Meaning

& AND

` `

^ XOR

~ NOT

Work on binary numbers.

Operator Meaning

& AND

` `

^ XOR

~ NOT
Example:

a = 5 # 101
b = 3 # 011

print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6

Shift Operators

Operator Meaning

<< Left shift

>> Right shift

Example:

x = 5 # 101

print(x << 1) # 10
print(x >> 1) # 2

Assignment Operators

Operator Meaning

= Assign

+= Add and assign

-= Subtract and assign

*= Multiply and assign

x=5
x += 3 # x = 8
x *= 2 # x = 16

print(x)
Ternary (Conditional) Operator

Short form of if-else.

Syntax:

result = value_if_true if condition else value_if_false

Example:

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)

Identity Operators

Check memory location (object identity).

Operato Meaning
r

is Same object

is not Different
object

Example:

a = [1,2]
b=a
c = [1,2]

print(a is b) # True
print(a is c) # False

9. Membership Operators

Check presence in sequence.

Operator Meaning

in Exists
not in Does not
exist

Example:

lst = [1,2,3]

print(2 in lst) # True


print(5 not in lst) # True

10. Operator Precedence

Defines execution order.

Example:

print(10 + 5 * 2) # 20

Multiplication happens before addition.

Important Order (Simplified):

1.​ ()
2.​ **
3.​ * / %
4.​ + -
5.​ Comparison
6.​ Logical

11. Mathematical Functions (math module)

Import module:

import math
Function Descriptio
n

[Link] Square
(x) root
[Link] Round up
(x)

[Link] Round
r(x) down

[Link]( Power
x,y)

[Link] Value of π

Example:

import math

print([Link](16)) # 4.0
print([Link](4.2)) # 5
print([Link](4.8)) # 4

What is the Difference between '==' and 'is' operators?

is Operator :'is' operator meant for reference or address comparison.

When a is b returns true?


Whenever 'a' and 'b' pointing to the same object, then only 'a is b' returns true, which
is nothing but reference comparison (or) Address comparison.

Example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a is b) # False

Different objects in memory → False

a = [1, 2, 3]
b=a

print(a == b) # True
print(a is b) # True
Same object + same values

== Operator :
'==' is meant for content comparison

x = 10
y = 10

print(x is y) # True

This happens due to memory optimization, not because is checks values


Python reuses the same memory location for some values instead of creating new
ones again and again.
Python sees 10 is a small integer
It stores it once in memory
Both x and y point to the same memory location
Use id() function
1. Conditional Statements

1. Conditional Statements (Selection Statements)

Definition

Conditional (selection) statements allow a program to make decisions.​


Based on a condition:

●​ Some statements are executed


●​ Some statements are skipped

Important Notes

●​ Python does not have a switch statement (available in C/Java)​


→ Instead, Python uses if-elif-else
●​ Python does not have a do-while loop​
→ It uses while loop
●​ Python does not support goto statement​
→ It follows structured programming
i) if Statement

Before learning syntax, understand an important concept:

Indentation (Very Important in Python)

●​ Indentation defines a block of code


●​ All statements under if must have the same indentation

Syntax
if condition:
statement1
statement2
statement3

statement4 # outside if block

Explanation

●​ Statements with same indentation → belong to if block


●​ Statement without indentation → outside the block

Important Rule

●​ If indentation is not correct → Python gives IndentationError

Example (Correct)
if 10 < 20:
print("10 is less than 20")

print("End of Program")

Output
10 is less than 20
End of Program
Example (Wrong – Indentation Error)
if 10 < 20:
print("10 is less than 20")

Error
IndentationError: expected an indented block

Example (User Input)


name = input("Enter Name: ")

if name == "Ali":
print("Hello Ali, Good Morning")

print("How are you?")

Output 1
Enter Name: Ali
Hello Karthi, Good Morning
How are you?

Output 2
Enter Name: Sara
How are you?

ii) if-else Statement

Syntax
if condition:
Action1
else:
Action2

Explanation

●​ If condition is True → Action1 executes


●​ If condition is False → Action2 executes

Example
name = input("Enter Name: ")

if name == "Ali":
print("Hello Ali! Good Morning")
else:
print("Hello Guest! Good Morning")

print("How are you?")

Output

Case 1:
Enter Name: Ali
Hello Ali! Good Morning
How are you?

Case 2:
Enter Name: Sara
Hello Guest! Good Morning
How are you?

iii) if-elif-else Statement

Syntax
if condition1:
Action1
elif condition2:
Action2
elif condition3:
Action3
...
else:
Default Action

Explanation

●​ Python checks conditions one by one


●​ First True condition executes
●​ If none are True → else runs
Example
brand = input("Enter your favourite brand: ")

if brand == "RC":
print("It is a children's brand")
elif brand == "KF":
print("It is not that strong")
elif brand == "FO":
print("Buy one get one free")
else:
print("Other brands are not recommended")

Output Examples

Input: RC
It is a children's brand

Input: FO
Buy one get one free

Input: abc
Other brands are not recommended
Possible Structures of Conditional Statements

if condition:
Action

if condition:
action1
else:
action2

if condition1:
action1
elif condition2:
action2
else:
default_action

if condition1:
action1
elif condition2:
action2

●​ Use if → when only one condition matters


●​ Use if-else → when there are two possible outcomes
●​ Use if-elif-else → when multiple conditions exist
●​ Use if-elif → when default case is not required

n1 = int(input("Enter First Number: "))


n2 = int(input("Enter Second Number: "))
n3 = int(input("Enter Third Number: "))

if n1 <= n2 and n1 <= n3:


print("Smallest Number is:", n1)
elif n2 <= n1 and n2 <= n3:
print("Smallest Number is:", n2)
else:
print("Smallest Number is:", n3)

Enter First Number: 10


Enter Second Number: 20
Enter Third Number: 30

Smallest Number is: 10

You might also like