Master Python programming with our comprehensive tutorial. Learn through examples and hands-on practice with Python's elegant syntax.
Python is a high-level, interpreted programming language known for its simplicity and readability.
# Simple Hello World program
print("Hello World!")
A simple Hello World program in Python
Learn fundamental Python concepts including variables, data types, and basic operations.
# Data types and variables
number = 42
pi = 3.14159
grade = 'A'
is_valid = True
name = "John"
# Dynamic typing
x = 100 # x is an integer
x = "Python" # now x is a string
Common data types and variable declarations in Python
# Lists in Python
numbers = [1, 2, 3, 4, 5]
names = []
# List operations
numbers.append(6)
numbers.extend([7, 8])
first_item = numbers[0]
last_item = numbers[-1]
Lists and basic list operations
Functions in Python are defined using the def keyword and are essential for code organization.
def greet(name, greeting="Hello"):
"""
A simple greeting function
with optional parameter
"""
return f"{greeting}, {name}!"
# Function calls
print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Hi")) # Hi, Bob!
# Lambda function
square = lambda x: x * x
print(square(5)) # 25
Functions and lambda expressions in Python
Learn how to control program flow using conditional statements and loops in Python.
# If-elif-else statement
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
else:
grade = 'C'
# For loop with range
for i in range(5):
print(i)
# While loop
count = 0
while count < 3:
print(f"Count: {count}")
count += 1
# List comprehension
squares = [x**2 for x in range(5)]
Examples of control flow statements and loops in Python