Week 6 Zoom Session Summary Of March 15 ATA Session 2 Room 2

ATA
Python Class Summary – Algorithmic Thinking

Algorithmic Thinking and Its Applications

Session Summary: Python Fundamentals

Instructor: Jainan Nareshkumar Tandel (IIT Jodhpur)
Date: 15th March 2025
Time: 11:00 AM – 1:00 PM
Platform: Zoom

Session Overview

This session focuses on Python operators and collections, providing a comprehensive guide to these fundamental programming concepts. We’ll explore various types of operators including arithmetic, assignment, comparison, logical, and bitwise operators, as well as Python’s collection data types. The session is designed to strengthen your understanding of these core elements and their practical applications in Python programming.

Key Topics Covered

1

Arithmetic Operators

These operators perform basic mathematical calculations:

Operator Description Example Result
+ Addition 5 + 3 8
Subtraction 5 – 3 2
* Multiplication 5 * 3 15
/ Division 5 / 3 1.6667
% Modulus (Remainder) 5 % 3 2
** Exponentiation 5 ** 3 125
// Floor Division 5 // 3 1

These operators are commonly used in mathematical operations, financial calculations, and scientific computations.

2

Assignment Operators

Used to assign values to variables and simplify code in loops and calculations:

Operator Equivalent To Example Result
= Assign x = 5 x = 5
+= Add & assign x += 3 x = 8
-= Subtract & assign x -= 3 x = 5
*= Multiply & assign x *= 3 x = 15
/= Divide & assign x /= 3 x = 5.0
# Assignment operator examples x = 10 x += 5 # x = x + 5, now x is 15 x *= 2 # x = x * 2, now x is 30 print(x) # Output: 30
3

Comparison (Relational) Operators

Used to compare values and return True or False, useful in decision-making and loops:

Operator Description Example Result
== Equal to 5 == 3 False
!= Not equal to 5 != 3 True
< Less than 5 < 3 False
> Greater than 5 > 3 True
<= Less than or equal to 5 <= 5 True
>= Greater than or equal to 5 >= 3 True
# Comparison operators in conditions age = 18 if age >= 18: print(“Adult”) else: print(“Minor”) # Output: Adult
4

Logical Operators

Used for combining Boolean expressions, widely used in conditions and control flow:

Operator Description Example Result
and True if both conditions are True (5 > 3) and (10 > 5) True
or True if at least one condition is True (5 > 3) or (10 < 5) True
not Negates the result not (5 > 3) False
# Logical operators in practice age = 25 income = 50000 if age > 18 and income > 30000: print(“Eligible for loan”) else: print(“Not eligible”) # Output: Eligible for loan
5

Bitwise Operators

Bitwise operators perform operations on the binary representation of numbers:

Operator Description Example Result
& Bitwise AND 5 & 15 5
| Bitwise OR 5 | 15 15
^ Bitwise XOR 5 ^ 15 10
~ Bitwise NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 20 >> 1 10
# Binary representation print(bin(5)) # Output: 0b101 print(bin(15)) # Output: 0b1111 # Bitwise AND (& operator) result = 5 & 15 # 101 & 1111 = 101 (binary) = 5 (decimal) print(result) # Output: 5

Bitwise operators are often used in low-level programming, data compression, and encryption.

6

Membership & Identity Operators

Membership operators check if a value exists in a sequence:

Operator Description Example Result
in Returns True if value is found in sequence ‘a’ in ‘abc’ True
not in Returns True if value is not found in sequence ‘d’ not in ‘abc’ True

Identity operators check if objects share the same memory location:

Operator Description Example Result
is Returns True if variables point to same object x is y True/False
is not Returns True if variables don’t point to same object x is not y True/False
# Membership operator example fruits = [‘apple’, ‘banana’, ‘cherry’] print(‘apple’ in fruits) # Output: True print(‘grape’ not in fruits) # Output: True # Identity operator example x = [1, 2, 3] y = x # y references the same object z = [1, 2, 3] # z is a new list with the same values print(x is y) # Output: True (same object) print(x is z) # Output: False (different objects) print(x == z) # Output: True (equal values)
7

Operator Precedence

When an expression contains multiple operators, Python evaluates them in a specific order:

Precedence Operator(s) Description
1 (Highest) () Parentheses
2 ** Exponentiation
3 +x, -x, ~x Unary Operators (Positive, Negative, Bitwise NOT)
4 *, /, //, % Multiplication, Division, Floor Division, Modulus
5 +, – Addition, Subtraction
6 <<, >> Bitwise Shift (Left & Right)
7 & Bitwise AND
8 ^, | Bitwise XOR, Bitwise OR
9 ==, !=, >, <, >=, <= Comparison Operators
10 is, is not, in, not in Identity and Membership Operators
11 not Logical NOT
12 and Logical AND
13 (Lowest) or Logical OR
# Example of operator precedence result = 5 + 2 * 3 ** 2 / 3 – 1 # Step 1: Exponentiation → 3 ** 2 = 9 # Step 2: Multiplication → 2 * 9 = 18 # Step 3: Division → 18 / 3 = 6 # Step 4: Addition → 5 + 6 = 11 # Step 5: Subtraction → 11 – 1 = 10 print(result) # Output: 10

Using parentheses can make your code more readable and prevent precedence mistakes.

8

Python Collections

Python collections store multiple items in a single variable:

Collection Properties Use Cases
Lists Mutable, Ordered When you need an ordered, changeable collection that allows duplicates
Tuples Immutable, Ordered When you need an ordered, unchangeable collection that allows duplicates
Sets Mutable, Unordered, No Duplicates When you need a collection with no duplicate values
Dictionaries Mutable, Unordered, Key-Value Pairs When you need a collection of key-value pairs for efficient lookup
# List example numbers = [1, 2, 3, 4] numbers.append(5) # [1, 2, 3, 4, 5] numbers.remove(2) # [1, 3, 4, 5] numbers[0] = 10 # [10, 3, 4, 5] # Tuple example tuple1 = (1, 2, 3, 4) print(tuple1[0]) # 1 (Values cannot be changed) # Set example set1 = {1, 2, 3, 3, 4} print(set1) # {1, 2, 3, 4} (Duplicate removed) set1.add(5) # {1, 2, 3, 4, 5} # Dictionary example student = {“name”: “John”, “age”: 20, “grade”: “A”} print(student[“name”]) # John student[“age”] = 21 # Update value

Key Takeaways

  • Understanding operator precedence is crucial for writing correct mathematical expressions in Python.
  • Arithmetic operators form the foundation for mathematical operations in programming.
  • Comparison and logical operators enable decision-making through conditional statements.
  • Bitwise operators are powerful for low-level operations like data compression and encryption.
  • Membership operators provide a clean way to check for element existence in collections.
  • Python collections (lists, tuples, sets, dictionaries) offer versatile data storage options for different scenarios.
  • Using parentheses can make complex expressions more readable and help avoid precedence errors.

© 2025 Algorithmic Thinking and Its Applications | IIT Jodhpur

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these