📘 Python Notes (Detailed – Exam Ready)
Unit 1: Basic Introduction
Origin of Python
Created by Guido van Rossum in 1989, released publicly in 1991.
Named after “Monty Python’s Flying Circus” (a British comedy show), not the
snake.
Python was designed with a focus on:
o Readability (code looks like English)
o Productivity (fewer lines compared to C, C++ or Java)
Need of Python Programming
Easy to learn and use (ideal for beginners).
Widely used in AI, Data Science, Web Development, Automation, Cybersecurity.
Large standard library (built-in modules).
Platform independent (works on Windows, Linux, macOS).
Features of Python
1. Simple & Easy – English-like syntax.
2. Interpreted – No need for compilation, executed line by line.
3. Object-Oriented – Supports classes and objects.
4. Portable – Write once, run anywhere.
5. Extensible & Embeddable – Can integrate with other languages like C, Java.
6. Large Library Support – Includes modules for math, file handling, web, etc.
7. Dynamic Typing – No need to declare variable type explicitly.
Program Structure
# Example Python program
# Single line comment
"""
Multi-line
comment
"""
def main():
x = 10
y = 20
print("Sum:", x + y)
main()
Comments: # for single line, triple quotes (''' or """) for multi-line.
Indentation is compulsory (no {} like C/Java).
Identifiers (HARD)
Identifiers are names used for variables, functions, classes, etc.
Rules for Identifiers:
1. Can contain letters (A–Z, a–z), digits (0–9), and underscore _.
2. Must start with a letter or underscore (not digit).
3. Case-sensitive: value, Value, and VALUE are different.
4. Cannot use reserved words as identifiers.
Valid: age, _salary, name1
Invalid: 2name, student-name, class
⚠️ Common mistake in exams: Using class or if as variable names.
Reserved Words (Keywords) (HARD)
Predefined words in Python used for specific purposes.
Cannot be used as variable names.
Examples:
and, or, not, if, else, elif, for, while, break,
continue, True, False, None, return, import, def, class
Total: ~35 (varies slightly by version).
IDLE – Python Interpreter
IDLE (Integrated Development and Learning Environment) is Python’s built-in
editor.
Allows you to:
o Write and run Python code.
o Test programs interactively in the shell ( >>>).
o Debug code easily.
Unit 2: Python Programming Introduction
Variables and Assignment Statements
Variable → Named memory location.
Assignment → Using = operator.
x = 10
name = "Alice"
pi = 3.14
Python is dynamically typed → type decided at runtime.
Multiple assignments:
a, b, c = 1, 2, 3
Data Types (HARD)
1. Numeric Types
o int → whole numbers (5, -20)
o float → decimal numbers (3.14, -0.5)
o complex → imaginary numbers (2+3j)
2. Sequence Types
o str (string): Ordered, immutable.
o s = "Python"
o print(s[0], s[-1]) # P n
o list: Ordered, mutable.
o l = [1, 2, 3]
o [Link](4) # [1, 2, 3, 4]
o tuple: Ordered, immutable.
o t = (1, 2, 3)
3. Mapping Type
o dict (dictionary): key-value pairs.
o d = {"name": "Bob", "age": 20}
o print(d["name"]) # Bob
4. Set Types
o set: Unordered, no duplicates.
o s = {1, 2, 2, 3}
o print(s) # {1, 2, 3}
5. Boolean
o Values: True, False.
Input and Print Functions
name = input("Enter your name: ")
print("Hello", name)
input() → always returns string, convert using int(), float().
String Formatting (HARD)
1. f-string (Best, Python 3.6+)
2. name = "Alice"
3. age = 20
4. print(f"My name is {name} and I am {age} years old.")
5. format() method
6. print("Student: {} | Marks: {}".format("Bob", 90))
7. % operator (old)
8. pi = 3.14159
9. print("Pi = %.2f" % pi) # Pi = 3.14
Unit 3: Operators and Expressions
Arithmetic Operators
+ , - , * , / , // , % , **
print(10 // 3) # 3 (floor division)
print(2 ** 3) # 8
Logical Operators
and, or, not
x, y = True, False
print(x and y) # False
print(not x) # False
Relational Operators
< , > , <= , >= , == , !=
Operator Precedence (HARD)
Order:
1. Parentheses ()
2. Exponent **
3. Unary +x, -x, not
4. Multiplication, Division, Modulus, Floor Division * / % //
5. Addition, Subtraction + -
6. Relational <, >, ==, !=
7. Logical and
8. Logical or
9. Assignment = += -=
Example:
result = 2 + 3 * 4 ** 2
# 4**2 = 16 → 3*16 = 48 → 2+48 = 50
print(result) # 50
Errors in Python (HARD)
1. Syntax Error → Code written incorrectly.
2. if x > 5
3. print("Hi") # ❌ Missing colon
4. Runtime Error (Exception) → Error during execution.
5. print(10 / 0) # ZeroDivisionError
6. Logical Error → No crash, but wrong result.
7. avg = total * count # Wrong, should be total / count
Unit 4: Conditional Statements
Boolean Processing
Conditions return True or False.
x = 5
print(x > 3) # True
if Statement
if x > 0:
print("Positive")
if/else
if x % 2 == 0:
print("Even")
else:
print("Odd")
if-elif-else (Multi-way decision)
marks = 82
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
elif marks >= 60:
print("C")
else:
print("Fail")
Nested if
x = 20
if x > 10:
if x < 30:
print("Between 10 and 30")
Errors in Conditionals (HARD)
1. Missing colon → if x > 5
2. Wrong indentation → code not properly spaced.
3. Using = instead of ==.
if x = 5: # ❌ Error