Python Revision Notes
📌 Variables and Data Types
▶️Concept: Variables store data. Python is dynamically typed.
🧠 Syntax:
x = 10
name = "Aditya"
price = 99.5
📊 int, float, str, bool, None
Using undeclared variables or wrong case (`Name` ≠ `name`)
📌 Control Flow – If/Else
▶️Concept: Conditional execution based on logical checks.
🧠 Syntax:
if score > 90:
print("Excellent")
elif score > 60:
print("Good")
else:
print("Needs Improvement")
Indentation is crucial in Python. Use 4 spaces.
📌 Loops – for / while
▶️Concept: Repeating blocks of code
🧠 Syntax:
for i in range(5):
print(i)
while count < 5:
count += 1
Infinite loops if `while` condition never becomes false.
📌 Functions
▶️Concept: Reusable blocks of code.
🧠 Syntax:
def greet(name):
return f"Hello, {name}"
📊 Parameters in function definition; arguments in function call.
Functions must be defined before they’re called.
📌 Strings and String Methods
▶️Concept: Strings are immutable sequences.
🧠 Syntax:
s = "hello"
s.upper() # 'HELLO'
s.replace("e", "a") # 'hallo'
"world" in s # False
📊 String indexing: s[0], s[-1]
📌 Lists
▶️Concept: Ordered, mutable collection.
🧠 Syntax:
fruits = ["apple", "banana"]
fruits.append("mango")
📊 Other Methods: .remove(), .pop(), .insert(), .sort()
Confusing .append() (modifies list) vs + (returns new list)
📌 Dictionaries
▶️Concept: Key-value pairs.
🧠 Syntax:
student = {"name": "Aditya", "score": 90}
student["score"] = 95
Accessing a non-existent key gives KeyError
📌 File Handling
▶️Concept: Reading/Writing text files
🧠 Syntax:
with open("data.txt", "r") as f:
content = f.read()
with open("output.txt", "w") as f:
f.write("Hello!")
Always use `with` to auto-close files
📌 Pandas DataFrame Basics
▶️Concept: 2D labeled structure, like an Excel table
🧠 Syntax:
import pandas as pd
df = pd.read_csv("file.csv")
📊 df.head() to see the first few rows
📌 Accessing Columns and Rows
▶️Concept:
🧠 Syntax:
df["maths"] > 80
df.loc[0:3, ["maths", "science"]]
📊 Columns: df["column"], Rows: df.loc[3] or df.iloc[3]
📌 Filtering Rows
▶️Concept: Use conditions to filter data.
🧠 Syntax:
df[df["maths"] > 80]
Conditions must be inside []
📌 Describe & Summary Stats
▶️Concept: Quickly summarize data.
🧠 Syntax:
df.describe()
df["score"].mean()
df["score"].value_counts()