Python Code Templates

Hello World

Prints "Hello World" to the console.

Hello World Preview
print("Hello World")

Variables

Assign values to variables and print them.

Variables Preview
x = 10
y = 5
print(x + y)

If Statement

Basic if-else condition example.

If Statement Preview
age = 18
if age >= 18:
    print("Adult")
else:
    print("Minor")

For Loop

Loop through numbers 1 to 5.

For Loop Preview
for i in range(1, 6):
    print(i)

While Loop

Loop until a condition is false.

While Loop Preview
count = 0
while count < 5:
    print(count)
    count += 1

Functions

Define and call a simple function.

Functions Preview
def greet(name):
    print(f"Hello {name}")

greet("Alice")

Lists

Create a list and print each element.

Lists Preview
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Dictionaries

Create a dictionary and access elements.

Dictionaries Preview
person = {"name": "Alice", "age": 25}
print(person["name"])

Import Module

Import and use the math module.

Math Module Preview
import math
print(math.sqrt(16))

Random Number

Generate a random number between 1 and 10.

Random Number Preview
import random
print(random.randint(1, 10))