Sure! Here are a few more Python examples covering different concepts:
1. Basic Arithmetic Operations
a = 10
b = 5
sum_result = a + b
print("Sum:", sum_result)
2. Conditional Statements
number = int(input("Enter a number: "))
if number > 0:
print("Positive number")
elif number < 0:
print("Negative number")
else:
print("Zero")
3. Loops
For Loop
for i in range(5):
print("Iteration:", i)
While Loop
count = 0
while count < 5:
print("Count:", count)
count += 1
4. Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
5. Lists and List Comprehensions
numbers = [1, 2, 3, 4, 5]
squared = [x**2 for x in numbers]
print("Squared numbers:", squared)
6. Dictionaries
person = {"name": "Alice", "age": 30}
print("Name:", person["name"])
print("Age:", person["age"])
7. Reading from a File
with open('example.txt', 'r') as file:
content = file.read()
print(content)
8. Error Handling
try:
value = int(input("Enter a number: "))
print("You entered:", value)
except ValueError:
print("That's not a valid number!")
These examples cover a range of basic concepts in Python. If you want to explore any specific topic further or need more examples, feel free to ask!
