This repository contains the fundamental concepts of Python programming, covering a wide range of topics including variables, loops, data structures, functions, modules, file handling, exception handling, and object-oriented programming. It is designed to help beginners understand and practice Python basics.
- Variables
- Loops
- Data Structures
- Functions
- Modules and Packages
- File Handling
- Exception Handling
- Object-Oriented Programming (OOP)
- Common Python Errors
Variables are used to store data in Python. You can assign different types of data to variables:
x = 5 # integer
name = "Alice" # string
price = 19.99 # float
is_available = True # booleanLoops are used to iterate over sequences or execute a block of code multiple times:
for i in range(5): # Will loop from 0 to 4
print(i)count = 0
while count < 3:
print(count)
count += 1A list is used to store multiple items in a single variable. Lists are ordered, changeable, and allow duplicate values.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add an element to the endA tuple is similar to a list, but it is immutable (cannot be changed).
coordinates = (4, 5)A set is an unordered collection that does not allow duplicate items.
unique_numbers = {1, 2, 3, 4}A dictionary is used to store data in key-value pairs.
person = {"name": "Alice", "age": 25}
print(person["name"])Functions are used to group reusable pieces of code.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))Modules are Python files that contain functions or classes. You can import them into your code to reuse the functions:
import math
print(math.sqrt(16))Packages are collections of modules that are organized in directories.
You can use Python to read from or write to files:
with open("example.txt", "r") as file:
content = file.read()
print(content)Exceptions are errors that occur during execution. You can use try and except blocks to handle them gracefully:
try:
result = 10 / 0
except ZeroDivisionError:
print("You cannot divide by zero!")OOP is a paradigm that allows you to create classes and objects:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start(self):
print(f"{self.brand} {self.model} is starting.")
my_car = Car("Toyota", "Corolla")
my_car.start()- SyntaxError: Happens when Python cannot understand the code due to incorrect syntax.
- IndentationError: Occurs when there is incorrect indentation in the code.
- TypeError: Occurs when an operation is applied to an object of inappropriate type.
This repository serves as a starting point for anyone looking to learn Python. Each section contains examples to practice, making it easy to follow along and understand the concepts.