Python for Beginners: Complete Tutorial
Learn Python programming from scratch with practical examples and hands-on coding exercises. Perfect for absolute beginners.
Python is one of the most popular and beginner-friendly programming languages in the world. It's used by companies like Google, Netflix, NASA, and Instagram for everything from web development to artificial intelligence.
Let's start with the classic "Hello, World!" program. This is traditionally the first program every programmer writes:
print("Hello, World!")That's it! Just one line. The print() function displays text on the screen. Notice how simple and readable Python is.
What are Variables?
Variables are containers for storing data. In Python, you don't need to declare the type - Python figures it out automatically!
# Strings (text) name = "Alice" message = "Hello, Python!" # Numbers age = 25 price = 19.99 # Booleans (True/False) is_student = True is_logged_in = False # Print them out print(name) # Output: Alice print(age) # Output: 25
Common Data Types
Math Operations
# Addition result = 10 + 5 # 15 # Subtraction result = 10 - 5 # 5 # Multiplication result = 10 * 5 # 50 # Division result = 10 / 5 # 2.0 # Power result = 2 ** 3 # 8 (2 to the power of 3)
String Operations
# Combining strings first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name # "John Doe" # String repetition laugh = "ha" * 3 # "hahaha" # String length message = "Hello" length = len(message) # 5
Conditional statements let your program make decisions based on conditions:
age = 18 if age >= 18: print("You are an adult")
else: print("You are a minor") # Multiple conditions
score = 85 if score >= 90: print("Grade: A")
elif score >= 80: print("Grade: B")
elif score >= 70: print("Grade: C")
else: print("Grade: F")Important:
Python uses indentation (spaces or tabs) to define code blocks. Make sure to indent consistently!
For Loops
Use for loops to repeat an action a specific number of times:
# Print numbers 1 to 5 for i in range(1, 6): print(i) # Loop through a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
While Loops
While loops continue until a condition becomes False:
count = 0 while count < 5: print(count) count += 1 # Increment by 1
Functions let you organize code into reusable blocks:
# Define a function
def greet(name): return f"Hello, {name}!" # Call the function
message = greet("Alice")
print(message) # Output: Hello, Alice! # Function with multiple parameters
def calculate_area(width, height): area = width * height return area result = calculate_area(5, 10)
print(result) # Output: 50Lists are ordered collections that can hold multiple items:
# Create a list
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"] # Access items (indexing starts at 0)
first_fruit = fruits[0] # "apple" # Add items
fruits.append("orange") # Remove items
fruits.remove("banana") # List length
count = len(fruits) # Loop through list
for fruit in fruits: print(fruit)Congratulations! You've learned the fundamentals of Python. Here's what to learn next:
Practice Python Online
Ready to write your own Python code? Use our online Python editor to practice these concepts, run code instantly, and see results in real-time - no installation required!