PYTHON PROGRAMMING – FULLY EXPANDED UNIT-
WISE NOTES
Below are deeply explained theory notes for every topic of every unit, with examples and outputs.
UNIT I – Introduction to Python
1. Python Variables (Theory + Example)
Theory
• Variables are names assigned to values.
• Python is dynamically typed → type decided at runtime.
• Variables are created automatically when a value is assigned.
Example
x = 10
name = "Anuj"
pi = 3.14
print(type(x), type(name), type(pi))
Output:
<class 'int'> <class 'str'> <class 'float'>
2. Basic Operators (Theory + Example)
Arithmetic Operators
• , - , * , / , % , // , **
Example
print(5 + 3)
print(10 // 3)
print(2 ** 3)
1
Output:
8
3
8
3. Indentation & Blocks (Theory + Example)
Theory
• Python uses indentation to group statements.
• Default = 4 spaces.
Example
if 10 > 5:
print("Valid block")
Output:
Valid block
4. Python Data Types (Theory + Example)
Built-in Types
• int, float, complex
• str
• list, tuple
• dict
• set, frozenset
Example
a = 10
b = 3.14
c = [1,2,3]
2
UNIT II – Flow Control & Conditional Blocks
1. if / else / elif (Theory + Example)
Theory
Conditions help decision making.
Example
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")
Output:
Adult
elif Example
marks = 82
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
else:
print("C")
Output:
2. for Loop (Theory + Example)
Theory
Used for iterating over sequences.
3
Example
for i in range(1,6):
print(i)
Output:
1
2
3
4
5
3. while Loop (Theory + Example)
n = 3
while n > 0:
print(n)
n -= 1
Output:
3
2
1
4. Loop Control Statements (Theory + Examples)
break Example
for i in range(5):
if i == 3:
break
print(i)
Output:
4
0
1
2
continue Example
for i in range(5):
if i == 2:
continue
print(i)
Output:
0
1
3
4
pass Example
for i in range(3):
pass
(No output)
5. Functions (Theory + Example)
Theory
• Function = reusable code block.
• Defined using def keyword.
Example
def add(a, b):
return a + b
print(add(4, 6))
5
Output:
10
UNIT III – Complex Data Types (Strings, Lists,
Tuples, Dicts)
1. Strings (Theory + Examples)
Theory
• Sequence of characters
• Immutable
Examples
s = "Python"
print(s[0])
print(s[1:4])
print([Link]())
print([Link]())
Output:
P
yth
python
PYTHON
2. Lists (Theory + Examples)
Theory
• Ordered, mutable collection
• Supports slicing, updates
Example
6
lst = [10, 20, 30]
[Link](40)
lst[1] = 25
print(lst)
Output:
[10, 25, 30, 40]
3. Tuples (Theory + Examples)
Theory
• Ordered, immutable
• Faster than lists
Example
tup = (1, 2, 3)
print(tup)
4. Dictionary (Theory + Examples)
Theory
• Key-value pairs
• Mutable
Example
student = {"name": "Anuj", "age": 20}
print(student["name"])
student["age"] = 21
print(student)
7
5. Sets (Theory + Examples)
Theory
• Unordered collection of unique elements
Example
s = {1,2,3,3}
print(s)
Output:
{1, 2, 3}
UNIT IV – File Handling
1. Reading Files (Theory + Example)
f = open("[Link]", "r")
print([Link]())
[Link]()
2. Writing Files (Theory + Example)
f = open("[Link]", "w")
[Link]("Hello Python")
[Link]()
3. readline(), readlines()
[Link]() # reads one line
[Link]() # reads all lines into a list
8
4. seek() – file pointer movement
[Link](0)
5. Exception Handling in File I/O
try:
f = open("[Link]")
except FileNotFoundError:
print("File missing!")
UNIT V – Python Packages
1. NumPy (Theory + Example)
Theory
• Used for scientific computing
• Supports arrays, matrices
Example
import numpy as np
arr = [Link]([1,2,3])
print(arr)
2. Pandas (Theory + Example)
Theory
• Used for data analysis
• Provides DataFrame & Series
Example
import pandas as pd
df = [Link]({"ID": [1,2], "Marks": [90,85]})
print(df)
9
3. Matplotlib (Theory + Example)
import [Link] as plt
[Link]([1,2,3],[4,5,6])
[Link]()
4. Tkinter GUI (Theory + Example)
from tkinter import *
root = Tk()
Label(root, text="Hello GUI").pack()
[Link]()
5. IDE Usage (Theory)
• Writing and executing Python programs in PyCharm, VS Code, IDLE.
END OF FULL NOTES
10