6

Python – Notes

1. Introduction

  • Python is a high-level, interpreted, general-purpose programming language.

  • Developed by Guido van Rossum in 1991.

  • Known for simple syntax, readability, and versatility.

  • Supports Object-Oriented, Procedural, and Functional Programming.


2. Features of Python

  1. Easy to learn and read

  2. Interpreted – Code executes line by line

  3. Cross-platform – Works on Windows, Linux, Mac

  4. High-level – Abstracts complex details

  5. Dynamic typing – No need to declare variable type

  6. Extensive libraries – e.g., NumPy, Pandas, Matplotlib

  7. Open-source


3. Python Syntax

  • Python uses indentation instead of {}

 
if 5 > 2: print("Five is greater than two")

4. Variables & Data Types

  • Variables: No need to declare type

 
x = 10 name = "Dinesh" pi = 3.14 flag = True
  • Data types:

    • Numbers: int, float, complex

    • String: “Hello”, ‘Hello’

    • Boolean: True, False

    • List: [1,2,3]

    • Tuple: (1,2,3)

    • Set: {1,2,3}

    • Dictionary: {"name":"Dinesh", "age":25}


5. Type Conversion

 
x = int(3.5) # float to int y = float(5) # int to float s = str(100) # int to string

6. Operators

  1. Arithmetic: + - * / % ** //

  2. Comparison: == != > < >= <=

  3. Logical: and or not

  4. Assignment: = += -= *= /= %=

  5. Membership: in, not in

  6. Identity: is, is not


7. Strings

 
s = "Hello Python" print(len(s)) # Length print(s.upper()) # Uppercase print(s.lower()) # Lowercase print(s[0]) # Indexing print(s[0:5]) # Slicing print(s.split()) # Split into list print(s.replace("Python","World")) # Replace

8. Lists

 
fruits = ["apple","banana","orange"] fruits.append("mango") # Add element fruits.remove("banana") # Remove element fruits.pop(1) # Remove by index print(fruits[0]) # Access element print(len(fruits)) # Length fruits.sort() # Sort list

9. Tuples

  • Immutable list

 
t = (1,2,3,4) print(t[0])

10. Sets

  • Unique unordered elements

 
s = {1,2,3,2,1} s.add(4) s.remove(3) print(s)

11. Dictionaries

 
person = {"name":"Dinesh","age":25} print(person["name"]) person["city"] = "Aurangabad" del person["age"]

12. Conditional Statements

 
age = 18 if age >= 18: print("Adult") elif age > 12: print("Teenager") else: print("Child")

13. Loops

  1. for loop

 
for i in range(5): print(i)
  1. while loop

 
i = 0 while i < 5: print(i) i += 1
  1. break & continue

 
for i in range(5): if i==3: break print(i)

14. Functions

 
def greet(name): return "Hello "+name print(greet("Dinesh"))
  • Default parameters:

 
def greet(name="Guest"): return "Hello "+name
  • Variable-length arguments:

 
def add(*nums): return sum(nums)

15. Lambda Functions

  • Anonymous single-line functions

 
square = lambda x: x*x print(square(5))

16. Object-Oriented Programming

 
class Car: def __init__(self, color, model): self.color = color self.model = model def drive(self): print("Car is driving") myCar = Car("Red","2025") myCar.drive()
  • Inheritance

 
class Vehicle: def info(self): print("Vehicle info") class Car(Vehicle): pass myCar = Car() myCar.info()
  • Encapsulation: Use _ or __ for private variables

  • Polymorphism: Same method name, different class


17. Modules & Packages

  • Import built-in or custom modules

 
import math print(math.sqrt(16)) from math import sqrt print(sqrt(25))
  • Create package: folder with __init__.py


18. File Handling

 
# Write file with open("test.txt","w") as f: f.write("Hello World") # Read file with open("test.txt","r") as f: print(f.read())

19. Exception Handling

 
try: x = 5/0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Execution finished")

20. Python Libraries (Popular)

  1. NumPy – Numerical computations

  2. Pandas – Data analysis

  3. Matplotlib/Seaborn – Data visualization

  4. Tkinter – GUI development

  5. Requests – HTTP requests

  6. BeautifulSoup – Web scraping


21. Important Notes

  • Python is case-sensitive

  • Indentation is mandatory

  • Dynamic typing → variables can change type

  • Comments:

 
# Single line """ Multi-line """