Python Basics - Quick Summary
1. Introduction to Python
Python is a high-level, interpreted programming language.
It's known for its readability, simplicity, and wide community support.
2. Data Types
a = 10 # Integer
b = 3.14 # Float
name = "Apoorva" # String
is_valid = True # Boolean
fruits = ["apple", "banana", "cherry"] # List
colors = ("red", "green", "blue") # Tuple
person = {"name": "Apoorva", "age": 25} # Dictionary
3. Variables & Comments
# This is a single-line comment
""" This is a
multi-line comment """
x=5
y = "Hello"
4. Conditional Statements
age = 18
if age >= 18:
print("You are an adult")
elif age > 13:
print("You are a teenager")
else:
print("You are a child")
5. Loops
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
6. Functions
def greet(name):
return "Hello, " + name
print(greet("Apoorva"))
7. Built-in Functions
print(len("Python"))
print(type(3.14))
print(int("5"))
8. File Handling
# Writing to a file
with open("sample.txt", "w") as file:
file.write("Hello, Python!")
# Reading from a file
with open("sample.txt", "r") as file:
content = file.read()
print(content)
9. Exception Handling
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Always runs")
10. Object-Oriented Basics
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hi, my name is", self.name)
p = Person("Apoorva")
p.greet()