Introduction to Python Code Examples | Generated by AI

Home 2025.10

Okay, “some Python code” can mean a lot of things! Python is incredibly versatile. I’ll give you a few common examples, from very basic to slightly more involved, covering different concepts.


1. Hello World (The Classic Start)

This is the simplest possible Python program.

# This is a comment. Comments are ignored by Python.
print("Hello, World!") 

Explanation:


2. Variables, Basic Arithmetic, and User Input

This code demonstrates how to store data in variables, perform simple math, and get input from the user.

# Variables to store data
name = input("What is your name? ") # Get input from the user
age_str = input("How old are you? ")

# Convert age from string to integer for calculations
age = int(age_str) 

# Basic arithmetic
year_of_birth = 2023 - age # Assuming current year is 2023 for simplicity
# You could also use: import datetime; year_of_birth = datetime.date.today().year - age

# Print formatted output using an f-string (formatted string literal)
print(f"Hello, {name}! You are {age} years old.")
print(f"You were likely born in {year_of_birth}.")

# A simple conditional check
if age < 18:
    print("You are a minor.")
else:
    print("You are an adult.")

Explanation:


3. Lists and Loops

Lists are ordered collections of items. Loops are used to repeat a block of code multiple times.

# A list of fruits
fruits = ["apple", "banana", "cherry", "date"]

print("\nMy favorite fruits:")

# Loop through the list using a 'for' loop
for fruit in fruits:
    print(f"- {fruit.capitalize()}") # .capitalize() makes the first letter uppercase

# Accessing elements by index (lists are zero-indexed)
print(f"\nThe first fruit is: {fruits[0]}")
print(f"The last fruit is: {fruits[-1]}") # Negative index accesses from the end

# Add an element
fruits.append("elderberry")
print(f"Updated list: {fruits}")

# Remove an element
fruits.remove("banana")
print(f"List after removing banana: {fruits}")

# Loop using 'range' to get indices
print("\nFruits with their indices:")
for i in range(len(fruits)): # len() gives the number of items in the list
    print(f"Index {i}: {fruits[i]}")

Explanation:


4. Functions

Functions are blocks of reusable code that perform a specific task.

# Define a function that takes arguments
def greet_person(name, age):
    """
    This function greets a person and tells them their age.
    It's a good practice to include a docstring like this.
    """
    return f"Hello, {name}! You are {age} years old."

# Call the function
message1 = greet_person("Alice", 30)
print(message1)

# Call it again with different arguments
message2 = greet_person("Bob", 25)
print(message2)

# Define a function with a default parameter
def calculate_area(length, width=10):
    """Calculates the area of a rectangle."""
    return length * width

print(f"\nArea of a 5x10 rectangle: {calculate_area(5)}") # Uses default width
print(f"Area of a 5x7 rectangle: {calculate_area(5, 7)}") # Overrides default width

Explanation:


5. Dictionaries

Dictionaries store data in key-value pairs. They are unordered (in older Python versions, ordered in Python 3.7+).

# A dictionary representing a person
person = {
    "first_name": "Charlie",
    "last_name": "Smith",
    "age": 40,
    "city": "London",
    "occupations": ["Engineer", "Teacher"] # A list can be a value
}

print("\nPerson's details:")

# Accessing values by key
print(f"Name: {person['first_name']} {person['last_name']}")
print(f"Lives in: {person.get('city', 'Unknown')}") # .get() is safer, returns None/default if key not found
print(f"First occupation: {person['occupations'][0]}")

# Modifying a value
person["age"] = 41
print(f"New age: {person['age']}")

# Adding a new key-value pair
person["email"] = "charlie.smith@example.com"
print(f"Email: {person['email']}")

# Looping through a dictionary
print("\nAll keys:")
for key in person: # Loops through keys by default
    print(key)

print("\nAll values:")
for value in person.values():
    print(value)

print("\nAll key-value pairs:")
for key, value in person.items():
    print(f"{key}: {value}")

Explanation:


This is just scratching the surface, but these examples cover many fundamental Python concepts you’ll use frequently!


Back

google/gemini-2.5-flash

Donate