Here are some basic Python examples to get you started with your first lesson:
Hello, World!
The classic first program in any language is to print "Hello, World!" to the screen.
print("Hello, World!")
Variables and Data Types
Python supports various data types such as integers, floats, strings, and booleans.
# Integer
age = 25
# Float
height = 5.9
# String
name = "Alice"
# Boolean
is_student = True
Basic Operations
You can perform arithmetic operations with Python.
# Addition
sum = 5 + 3
# Subtraction
difference = 10 - 2
# Multiplication
product = 4 * 7
# Division
quotient = 20 / 4
# Exponentiation
power = 2 ** 3
Lists
Lists are used to store multiple items in a single variable.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
# Adding an item to the list
fruits.append("orange")
Conditional Statements
Conditional statements allow you to execute code based on certain conditions.
if age > 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops
Loops are used for iterating over a sequence (like a list or a string).
For Loop
for fruit in fruits:
print(fruit)
While Loop
count = 0
while count < 5:
print(count)
count += 1
Functions
Functions are blocks of code that only run when they are called.
def greet(name):
return f"Hello, {name}!"
print(greet("Bob")) # Output: Hello, Bob!
These examples cover some of the basics of Python programming.