Python Full Detailed Notes (2nd Year)
1. Introduction to Python
Python is a high-level, interpreted, object-oriented, dynamically typed programming language.
It was developed by Guido van Rossum in 1991.
Key Features:
1. Simple & Easy
2. Interpreted (line by line execution)
3. Cross-platform
4. Object-Oriented
5. Extensive Libraries
6. Dynamic Typing
7. Free & Open Source
Applications:
- Web Development (Django, Flask)
- Data Science and AI
- Machine Learning & Deep Learning
- Automation & Scripting
- Game Development
- Networking
- Cybersecurity
2. Python Basics
Variables store data in memory.
Example:
x = 10
name = "Kraven"
Data Types:
- Numeric: int, float, complex
- Text: str
- Sequence: list, tuple, range
- Mapping: dict
- Set: set, frozenset
- Boolean: True, False
- NoneType: None
Operators:
Arithmetic (+,-,*,/,//,%,**), Relational (==,!=,>,<,>=,<=),
Logical (and, or, not), Assignment (=, +=, -=),
Membership (in, not in), Identity (is, is not), Bitwise (&, |, ^, ~, <<, >>)
Input & Output:
name = input("Enter name: ")
print("Hello", name)
3. Control Structures
Conditional Statements:
if condition:
statement
elif condition:
statement
else:
statement
Loops:
for i in range(5):
print(i)
while count <= 5:
print(count)
count += 1
Loop Control: break, continue, pass
4. Functions
Definition: Block of reusable code.
Types of Functions:
1. Built-in: len(), sum(), max(), min(), type(), sorted()
2. User-defined:
def greet(name):
return "Hello " + name
3. Lambda Functions:
square = lambda x: x*x
4. Recursive Functions:
def fact(n): return 1 if n==0 else n*fact(n-1)
5. Higher Order Functions: map(), filter(), reduce()
5. Data Structures
List: Ordered, mutable
fruits = ["apple","banana","mango"]
Tuple: Ordered, immutable
point = (1,2,3)
Set: Unordered, unique items
s = {1,2,3,3}
Dictionary: Key-value pairs
student = {"name":"Kraven","age":20}
6. Strings
String Functions:
upper(), lower(), replace(), split(), join(), find(), count()
String slicing:
s = "Python"
print(s[0:4]) # Pyth
7. File Handling
Modes: r (read), w (write), a (append), rb/wb (binary)
with open("[Link]","w") as f:
[Link]("Hello Python")
with open("[Link]","r") as f:
print([Link]())
8. Exception Handling
try:
num = int(input("Enter number: "))
print(10/num)
except ZeroDivisionError:
print("Cannot divide by zero!")
9. Object-Oriented Programming (OOP)
Concepts: Class, Object, Encapsulation, Inheritance, Polymorphism, Abstraction
class Student:
def __init__(self,name,age):
[Link] = name
[Link] = age
def display(self):
print([Link],[Link])
s1 = Student("Kraven",20)
[Link]()
10. Modules & Packages
Module: File containing Python code (.py)
Package: Collection of modules
import math
print([Link](16))
11. Advanced Python
Iterators: objects with __iter__() and __next__()
Generators: yield keyword
Decorators: modify functions
Virtual Environment: python -m venv myenv
12. Python Libraries
NumPy – numerical computing
Pandas – data analysis
Matplotlib – data visualization
Tkinter – GUI development
Requests – APIs
13. Best Practices
- Use meaningful variable names
- Comment code properly
- Follow PEP8 style guide
- Use exception handling
- Write modular & reusable code