Skip to content

Commit b571e3f

Browse files
authored
Add files via upload
1 parent b136ecb commit b571e3f

File tree

10 files changed

+669
-0
lines changed

10 files changed

+669
-0
lines changed

Days-1-3.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
**Day 1: Introduction to Python**
2+
- Python is a high-level programming language.
3+
- It's known for its readability and simplicity.
4+
- Python code is executed line by line.
5+
- Example:
6+
```python
7+
print("Hello, World!")
8+
```
9+
10+
**Day 2: Variables and Data Types**
11+
- Variables are used to store data.
12+
- Common data types include strings, integers, floats, lists, tuples, and dictionaries.
13+
- Example:
14+
```python
15+
name = "John"
16+
age = 30
17+
height = 6.1
18+
fruits = ["apple", "banana", "cherry"]
19+
person = {"name": "Alice", "age": 25}
20+
```
21+
22+
**Day 3: Conditional Statements and Loops**
23+
- Conditional statements allow you to make decisions in your code.
24+
- `if`, `elif`, and `else` are used for branching.
25+
- Loops like `for` and `while` are used for repetitive tasks.
26+
- Example:
27+
```python
28+
# Conditional statement
29+
x = 10
30+
if x > 5:
31+
print("x is greater than 5")
32+
elif x == 5:
33+
print("x is equal to 5")
34+
else:
35+
print("x is less than 5")
36+
37+
# For loop
38+
fruits = ["apple", "banana", "cherry"]
39+
for fruit in fruits:
40+
print(fruit)
41+
42+
# While loop
43+
count = 0
44+
while count < 5:
45+
print(count)
46+
count += 1
47+
```
48+
49+
Remember to run these examples in a Python environment. You can install Python on your computer by downloading it from the official Python website (https://www.python.org/downloads/). Once installed, you can use a code editor or Python's built-in IDLE to write and run Python code.
50+
51+
These examples should give you a good start in understanding basic Python concepts and how to run Python code.

Days-11-14.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
**Day 11: Functions**
2+
- Functions are reusable blocks of code that perform a specific task.
3+
- You can define your own functions in Python.
4+
5+
**Example of defining and calling a function:**
6+
```python
7+
def greet(name):
8+
print(f"Hello, {name}!")
9+
10+
greet("Alice") # Call the greet function with the argument "Alice".
11+
```
12+
13+
**Day 12: Function Parameters and Return Values**
14+
- Functions can take parameters (inputs) and return values (outputs).
15+
- You can use the `return` statement to return a value from a function.
16+
17+
**Example of a function with parameters and a return value:**
18+
```python
19+
def add(x, y):
20+
result = x + y
21+
return result
22+
23+
sum_result = add(3, 5)
24+
print(sum_result) # Prints the result of the add function, which is 8.
25+
```
26+
27+
**Day 13: Built-in Modules**
28+
- Python has many built-in modules that provide additional functionality.
29+
- You can use the `import` statement to access these modules.
30+
31+
**Example of importing and using a built-in module (math):**
32+
```python
33+
import math
34+
35+
sqrt_result = math.sqrt(25) # Calculate the square root of 25.
36+
print(sqrt_result) # Prints the result, which is 5.0.
37+
```
38+
39+
**Day 14: Creating Your Own Modules**
40+
- You can create your own Python modules to organize and reuse your code.
41+
- A module is simply a Python file containing functions and variables.
42+
43+
**Example of creating a custom module (my_module.py):**
44+
```python
45+
# my_module.py
46+
def greet(name):
47+
print(f"Hello, {name}!")
48+
49+
# main.py
50+
import my_module
51+
52+
my_module.greet("Bob") # Call the greet function from the custom module.
53+
```
54+
55+
Understanding functions and modules is crucial for writing organized and reusable code in Python. You can create your own functions and modules to efficiently structure your programs and make them more maintainable. Practice with these examples to become proficient in using functions and modules in your Python projects.

Days-15-18.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
**Day 15: File Handling**
2+
- File handling in Python allows you to read from and write to files.
3+
- You can use the `open()` function to interact with files.
4+
5+
**Example of reading from a file:**
6+
```python
7+
# Open a file for reading
8+
file = open("example.txt", "r")
9+
10+
# Read the contents of the file
11+
content = file.read()
12+
print(content)
13+
14+
# Close the file
15+
file.close()
16+
```
17+
18+
**Example of writing to a file:**
19+
```python
20+
# Open a file for writing
21+
file = open("example.txt", "w")
22+
23+
# Write content to the file
24+
file.write("Hello, Python!")
25+
26+
# Close the file
27+
file.close()
28+
```
29+
30+
**Day 16: File Modes and Context Managers**
31+
- File modes (e.g., "r" for read, "w" for write) determine how the file is opened.
32+
- Context managers (with statement) simplify file handling and ensure file closure.
33+
34+
**Example of using a context manager to read a file:**
35+
```python
36+
# Using a context manager to automatically close the file
37+
with open("example.txt", "r") as file:
38+
content = file.read()
39+
print(content)
40+
```
41+
42+
**Day 17: Error Handling (Try-Except Blocks)**
43+
- Error handling allows you to gracefully handle exceptions and errors in your code.
44+
- You can use `try`, `except`, `else`, and `finally` blocks to manage errors.
45+
46+
**Example of handling an exception:**
47+
```python
48+
try:
49+
num = int("abc") # This will raise a ValueError
50+
except ValueError as e:
51+
print(f"An error occurred: {e}")
52+
```
53+
54+
**Day 18: Error Handling (Multiple Exceptions and Custom Exceptions)**
55+
- You can handle multiple exceptions with multiple `except` blocks.
56+
- You can create custom exceptions by defining new exception classes.
57+
58+
**Example of handling multiple exceptions and creating a custom exception:**
59+
```python
60+
try:
61+
result = 10 / 0 # This will raise a ZeroDivisionError
62+
except ZeroDivisionError as e:
63+
print(f"Division by zero error: {e}")
64+
except Exception as e:
65+
print(f"An unexpected error occurred: {e}")
66+
67+
# Custom exception
68+
class MyCustomError(Exception):
69+
pass
70+
71+
try:
72+
raise MyCustomError("This is a custom error.")
73+
except MyCustomError as e:
74+
print(f"Custom error caught: {e}")
75+
```
76+
77+
Understanding file handling and error handling is essential for robust and reliable Python programming. These skills help you work with files and gracefully handle errors that may occur during the execution of your code. Practice with these examples to become proficient in file handling and error handling in Python.

Days-19-22.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
**Day 19: Introduction to Object-Oriented Programming (OOP)**
2+
- Object-Oriented Programming is a programming paradigm that uses objects to represent real-world entities.
3+
- In Python, everything is an object.
4+
5+
**Example of creating a simple class and an object:**
6+
```python
7+
# Define a simple class
8+
class Dog:
9+
def __init__(self, name):
10+
self.name = name
11+
12+
def bark(self):
13+
print(f"{self.name} says woof!")
14+
15+
# Create an object (instance) of the Dog class
16+
my_dog = Dog("Buddy")
17+
my_dog.bark() # Call the bark method on the object
18+
```
19+
20+
**Day 20: Class Attributes and Methods**
21+
- Classes can have attributes (data) and methods (functions) that define their behavior.
22+
- You can access attributes and call methods on objects.
23+
24+
**Example of class attributes and methods:**
25+
```python
26+
class Circle:
27+
def __init__(self, radius):
28+
self.radius = radius
29+
30+
def area(self):
31+
return 3.14159 * self.radius**2
32+
33+
def circumference(self):
34+
return 2 * 3.14159 * self.radius
35+
36+
my_circle = Circle(5)
37+
print(f"Area: {my_circle.area()}")
38+
print(f"Circumference: {my_circle.circumference()}")
39+
```
40+
41+
**Day 21: Inheritance**
42+
- Inheritance allows you to create a new class that is a modified version of an existing class (parent class).
43+
- The new class inherits the attributes and methods of the parent class.
44+
45+
**Example of inheritance:**
46+
```python
47+
# Parent class
48+
class Animal:
49+
def __init__(self, name):
50+
self.name = name
51+
52+
def speak(self):
53+
pass
54+
55+
# Child class inheriting from Animal
56+
class Dog(Animal):
57+
def speak(self):
58+
return f"{self.name} says woof!"
59+
60+
my_dog = Dog("Buddy")
61+
print(my_dog.speak()) # Calls the speak method of the Dog class
62+
```
63+
64+
**Day 22: Polymorphism**
65+
- Polymorphism allows objects of different classes to be treated as objects of a common superclass.
66+
- It simplifies code and promotes code reusability.
67+
68+
**Example of polymorphism:**
69+
```python
70+
# Common superclass
71+
class Shape:
72+
def area(self):
73+
pass
74+
75+
# Subclasses with different implementations of area
76+
class Circle(Shape):
77+
def __init__(self, radius):
78+
self.radius = radius
79+
80+
def area(self):
81+
return 3.14159 * self.radius**2
82+
83+
class Rectangle(Shape):
84+
def __init__(self, width, height):
85+
self.width = width
86+
self.height = height
87+
88+
def area(self):
89+
return self.width * self.height
90+
91+
shapes = [Circle(5), Rectangle(4, 6)]
92+
93+
for shape in shapes:
94+
print(f"Area: {shape.area()}")
95+
```
96+
97+
Object-Oriented Programming (OOP) is a fundamental concept in Python and many other programming languages. It allows you to model real-world entities, promote code organization, and enhance code reusability. Practice with these examples to become proficient in using OOP principles in Python.

Days-23-26.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
**Day 23: Database Connection**
2+
- Python can connect to various types of databases using different database libraries.
3+
- One common library for working with databases in Python is `sqlite3` for SQLite databases.
4+
5+
**Example of connecting to an SQLite database:**
6+
```python
7+
import sqlite3
8+
9+
# Connect to a database (creates a new one if it doesn't exist)
10+
conn = sqlite3.connect("my_database.db")
11+
12+
# Create a cursor object to execute SQL commands
13+
cursor = conn.cursor()
14+
15+
# Close the connection when done
16+
conn.close()
17+
```
18+
19+
**Day 24: Creating Tables and Inserting Data**
20+
- You can create tables in a database and insert data into them using SQL commands.
21+
- The `execute()` method is used to run SQL queries.
22+
23+
**Example of creating a table and inserting data:**
24+
```python
25+
import sqlite3
26+
27+
conn = sqlite3.connect("my_database.db")
28+
cursor = conn.cursor()
29+
30+
# Create a table
31+
cursor.execute("""
32+
CREATE TABLE IF NOT EXISTS students (
33+
id INTEGER PRIMARY KEY,
34+
name TEXT,
35+
age INTEGER
36+
)
37+
""")
38+
39+
# Insert data into the table
40+
cursor.execute("INSERT INTO students (name, age) VALUES (?, ?)", ("Alice", 25))
41+
cursor.execute("INSERT INTO students (name, age) VALUES (?, ?)", ("Bob", 30))
42+
43+
# Commit the changes and close the connection
44+
conn.commit()
45+
conn.close()
46+
```
47+
48+
**Day 25: Querying Data**
49+
- You can retrieve data from a database table using SQL SELECT queries.
50+
- The `fetchall()` method retrieves all rows from a query.
51+
52+
**Example of querying data from a table:**
53+
```python
54+
import sqlite3
55+
56+
conn = sqlite3.connect("my_database.db")
57+
cursor = conn.cursor()
58+
59+
# Query data from the table
60+
cursor.execute("SELECT * FROM students")
61+
students = cursor.fetchall()
62+
63+
# Print the results
64+
for student in students:
65+
print(f"ID: {student[0]}, Name: {student[1]}, Age: {student[2]}")
66+
67+
conn.close()
68+
```
69+
70+
**Day 26: Updating and Deleting Data**
71+
- You can use SQL UPDATE and DELETE queries to modify or remove data from a database table.
72+
- Be cautious when performing updates or deletions.
73+
74+
**Example of updating and deleting data:**
75+
```python
76+
import sqlite3
77+
78+
conn = sqlite3.connect("my_database.db")
79+
cursor = conn.cursor()
80+
81+
# Update data
82+
cursor.execute("UPDATE students SET age = 26 WHERE name = 'Alice'")
83+
84+
# Delete data
85+
cursor.execute("DELETE FROM students WHERE name = 'Bob'")
86+
87+
# Commit the changes and close the connection
88+
conn.commit()
89+
conn.close()
90+
```
91+
92+
Understanding how to work with databases and SQL in Python is crucial for many real-world applications where data storage and retrieval are required. The `sqlite3` library is suitable for learning and small-scale projects, but for larger databases, you may consider other database systems like MySQL or PostgreSQL. Practice with these examples to become proficient in database operations in Python.

0 commit comments

Comments
 (0)