Python-Tutorial

What is Python?

Python is a high-level, easy-to-learn programming language used for:

  • Web development
  • Data analysis
  • Artificial Intelligence (AI)
  • Machine Learning
  • Automation
  • Desktop applications
  • Game development
  • Cybersecurity

Python is popular because its syntax is simple and resembles English.

Data Types & SYntax

Example: Hello World

 
print("Hello, World!")
 

Output:

 
Hello, World!
 

Variables

 
name = "Frank"
age = 25

print(name)
print(age)
 

If Statement

 
age = 18

if age >= 18:
print("Adult")
else:
print("Minor")
 

Loop

 
for i in range(1, 6):
print(i)
 

Function

 
def greet(name):
return f"Hello, {name}"

print(greet("Frank"))
 

List

 
fruits = ["Apple", "Mango", "Banana"]

for fruit in fruits:
print(fruit)
Python Advanced

1. List Comprehensions

Instead of:

 
numbers = [1, 2, 3, 4, 5]

squares = []
for n in numbers:
squares.append(n * n)

print(squares)
 

Use:

 
numbers = [1, 2, 3, 4, 5]

squares = [n * n for n in numbers]

print(squares)
 

2. Lambda Functions

 
square = lambda x: x * x

print(square(5))
 

Output:

 
25
 

3. Map, Filter, Reduce

Map

 
numbers = [1, 2, 3, 4]

result = list(map(lambda x: x * 2, numbers))

print(result)
 

Filter

 
numbers = [1, 2, 3, 4, 5]

even = list(filter(lambda x: x % 2 == 0, numbers))

print(even)
 

4. Exception Handling

 
try:
num = int(input("Enter Number: "))
print(10 / num)

except ZeroDivisionError:
print("Cannot divide by zero")

except ValueError:
print("Invalid input")
 

5. File Handling

Write File

 
with open("sample.txt", "w") as file:
file.write("Hello Python")
 

Read File

 
with open("sample.txt", "r") as file:
content = file.read()
print(content)
 

6. Modules

mymodule.py

 
def add(a, b):
return a + b
 

main.py

 
import mymodule

print(mymodule.add(10, 20))
OOPS
class Student:

def __init__(self, name, course):
self.name = name
self.course = course

def display(self):
print(self.name, self.course)

student = Student("Frank", "Python")

student.display()
 

8. Inheritance

 
class Person:

def __init__(self, name):
self.name = name

class Employee(Person):

def show(self):
print(self.name)

emp = Employee("Frank")

emp.show()
 
Working with MySQL

import mysql.connector

conn = mysql.connector.connect(
host=”localhost”,
user=”root”,
password=”password”,
database=”college”
)

cursor = conn.cursor()

cursor.execute(“SELECT * FROM students”)

for row in cursor:
print(row)

Working with APIs

mport requests

response = requests.get(
“https://jsonplaceholder.typicode.com/users”
)

print(response.json())

Decorators

 
def logger(func):

def wrapper():
print("Function Started")
func()
print("Function Ended")

return wrapper

@logger
def hello():
print("Hello")

hello()

Generators

 
def numbers():

for i in range(1, 6):
yield i

for num in numbers():
print(num)

Virtual Environment

Create:

 
python -m venv venv
 

Activate (Linux):

 
source venv/bin/activate
 

Activate (Windows):

 
venv\Scripts\activate
 
Pandas (Data Analysis)

import pandas as pd

data = {
“Name”: [“Frank”, “John”],
“Age”: [25, 30]
}

df = pd.DataFrame(data)

print(df)