Introduction Examples
Hello World Program
print("Hello, World!")
Simple Calculation
result = 2 + 3
print(result) # Output: 5
Basics Examples
Variables and Data Types
x = 5
y = 3.14
name = "Python"
is_fun = True
print(type(x)) #
Operators
a = 10
b = 3
print(a + b) # 13
print(a // b) # 3
print(a == b) # False
Control Structures
if x > 0:
print("Positive")
for i in range(3):
print(i) # 0, 1, 2
Functions
def greet(name):
return f"Hello, {name}!"
print(greet("World")) # Hello, World!
Advanced Examples
OOP
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof!"
dog = Dog("Buddy")
print(dog.bark()) # Buddy says woof!
Exceptions
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Algorithms
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
print(bubble_sort([64, 34, 25, 12, 22])) # [12, 22, 25, 34, 64]
Data Analysis
import numpy as np
import pandas as pd
# NumPy
arr = np.array([1, 2, 3])
print(arr * 2) # [2 4 6]
# Pandas
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
print(df)