Advanced Python Exam Notes (Colorful & Detailed)
MODULE 1: Python Basics
1. Python Interpreter: Python interpreter executes code line-by-line, converting it into bytecode
and running using PVM. It helps in debugging easily.
Example: Interpreter immediately shows error if syntax is wrong.
print('Hello')
2. Data Types: Python has multiple data types like int, float, string, list, tuple, set, dictionary.
Example Program:
a = 5
b = 3.5
name = 'Ali'
nums = [1,2,3]
print(type(a), type(name))
3. Control Statements: Used to control flow of execution.
Example 1: Even/Odd
n = 4
if n % 2 == 0:
print('Even')
else:
print('Odd')
Example 2: Loop
for i in range(3):
print(i)
4. Prime Number:
num = 13
for i in range(2, num):
if num % i == 0:
print('Not Prime')
break
else:
print('Prime')
Extra: Check multiple numbers using loop
5. Fibonacci Series:
a, b = 0, 1
for i in range(6):
print(a)
a, b = b, a + b
Extra: Store series in list
MODULE 2: Python Programs
1. Collections: List (mutable), Tuple (immutable), Set (unique), Dictionary (key-value).
Example:
data = {'a':1, 'b':2}
print(data['a'])
2. Functions: Functions are reusable blocks of code.
def add(a,b):
return a+b
print(add(2,3))
Recursion Example:
def fact(n):
if n==1:
return 1
return n*fact(n-1)
print(fact(5))
3. Character Frequency:
s='hello'
freq={}
for ch in s:
freq[ch]=[Link](ch,0)+1
print(freq)
Extra: Ignore spaces or case sensitivity
4. Sorting Without Built-in:
arr=[4,2,1]
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if arr[i]>arr[j]:
arr[i],arr[j]=arr[j],arr[i]
print(arr)
Extra: Descending order
MODULE 4: OOP
Class & Object:
class Student:
def __init__(self,name):
[Link]=name
s=Student('Ali')
print([Link])
Inheritance:
class A:
def show(self):
print('A')
class B(A):
pass
B().show()
Exception Handling:
try:
x=int('a')
except:
print('Error')
MODULE 5: Data Processing
NumPy:
import numpy as np
a=[Link]([1,2,3])
print(a+1)
Pandas:
import pandas as pd
df=[Link]({'A':[1,2]})
print(df)
Matplotlib:
import [Link] as plt
[Link]([1,2,3],[3,2,1])
[Link]()