Basic Python Codes
Basic Python practice codes for 7 days:
**Day 1: Variables and Data Types**
```python
# Declare a variable and print its value
my_variable = 42
print(my_variable)
# Perform basic arithmetic operations
num1 = 10
num2 = 5
sum_result = num1 + num2
print(sum_result)
```
**Day 2: Conditional Statements**
```python
# Create an if-else statement
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
# Use comparison operators (>, <, ==, !=)
```
**Day 3: Loops**
```python
# Use a for loop to print numbers from 1 to 5
for i in range(1, 6):
print(i)
# Use a while loop to count down from 5 to 1
count = 5
while count > 0:
print(count)
count -= 1
```
**Day 4: Lists**
```python
# Create and manipulate lists
my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # Access an element
my_list.append(6) # Add an element
my_list.remove(3) # Remove an element
print(my_list)
```
**Day 5: Functions**
```python
# Define a function
def greet(name):
print("Hello, " + name + "!")
# Call the function
greet("Alice")
# Return values from a function
def add(x, y):
return x + y
result = add(3, 4)
print(result)
```
**Day 6: Dictionaries**
```python
# Create and access dictionary items
person = {"name": "John", "age": 30, "city": "New York"}
print(person["name"])
print(person.get("age"))
# Modify dictionary values
person["age"] = 31
print(person)
```
**Day 7: Modules and Libraries**
```python
# Import and use a module
import math
print(math.sqrt(25)) # Calculate square root
# Create and use your own module (Create a separate .py file)
# For example, create a file named mymodule.py with a function and import it here.
# from mymodule import my_function
# result = my_function()
# print(result)
```
These are some basic Python practice codes to get you started. Feel free to expand on them and explore more advanced topics as you become more comfortable with Python programming.