Write and run Python code directly in your browser using Pyodide! Try out the examples below or write your own code.
# Basic arithmetic operations x = 10 y = 5 print(f"Addition: {x + y}") print(f"Subtraction: {x - y}") print(f"Multiplication: {x * y}") print(f"Division: {x / y}") print(f"Power: {x ** 2}")
# Working with lists numbers = [1, 2, 3, 4, 5] print("Original list:", numbers) numbers.append(6) print("After append:", numbers) print("Sum of numbers:", sum(numbers)) print("Maximum:", max(numbers)) print("Minimum:", min(numbers))
# String operations text = "Python Programming" print("Original text:", text) print("Uppercase:", text.upper()) print("Lowercase:", text.lower()) print("Length:", len(text)) print("Split words:", text.split())
# Working with dictionaries student = { "name": "John", "age": 20, "grades": [85, 90, 88] } print("Student info:", student) print("Name:", student["name"]) print("Average grade:", sum(student["grades"]) / len(student["grades"])) # Adding new key-value pair student["subject"] = "Python" print("Updated info:", student)
# Demonstrating loops print("For loop with range:") for i in range(5): print(f"Number: {i}") print("\nWhile loop:") count = 0 while count < 3: print(f"Count: {count}") count += 1 print("\nLoop through list:") fruits = ["apple", "banana", "orange"] for fruit in fruits: print(f"Fruit: {fruit}")
# Function examples def greet(name): return f"Hello, {name}!" def calculate_area(length, width): return length * width # Testing functions print(greet("Python Learner")) print(f"Area of rectangle: {calculate_area(5, 3)}") # Function with default parameter def power(base, exponent=2): return base ** exponent print(f"Square of 4: {power(4)}") print(f"4 to power 3: {power(4, 3)}")