Python Basics Examples

Start with beginner-friendly Python programs—variables, arithmetic, swapping, random numbers, and everyday unit conversions. These small snippets build confidence and help you understand how Python evaluates expressions and handles simple logic.

Python Program to Calculate the Area of a Triangle

Uses 1/2 * base * height formula.

base = 10
height = 6
area = 0.5 * base * height
print(area)

Output

30.0

Python "Hello, World!" Program

Print a greeting to the console.

print("Hello, World!")

Output

Hello, World!

Python Program to Add Two Numbers

Adds two integers and prints the result.

a = 15
b = 27
print(a + b)

Output

42

Python Program to Find the Square Root

Computes square root using math module.

import math
print(math.sqrt(16))

Output

4.0

Python Program to Solve Quadratic Equation

Finds roots using discriminant.

import cmath
a, b, c = 1, -3, 2
d = b*b - 4*a*c
r1 = (-b + cmath.sqrt(d)) / (2*a)
r2 = (-b - cmath.sqrt(d)) / (2*a)
print(r1)
print(r2)

Output

(2+0j)
(1+0j)

Python Program to Swap Two Variables

Swaps values without temp variable.

a, b = 3, 4
a, b = b, a
print(a, b)

Output

4 3

Python Program to Generate a Random Number

Generates deterministic random with seed.

import random
random.seed(0)
print(random.randint(1, 100))

Output

50

Python Program to Convert Kilometers to Miles

Converts km to miles.

km = 5
miles = km * 0.621371
print(round(miles, 6))

Output

3.106855

Python Program to Convert Celsius To Fahrenheit

Uses F = C*9/5 + 32.

C = 0
F = C * 9/5 + 32
print(F)

Output

32.0

Python Program to Check if a Number is Positive, Negative or 0

Checks sign of integer.

n = 0
print('Positive' if n > 0 else ('Negative' if n < 0 else 'Zero'))

Output

Zero

Python Program to Check if a Number is Odd or Even

Modulo check for parity.

n = 7
print('Even' if n % 2 == 0 else 'Odd')

Output

Odd

Python Strings Basics

Concatenate, format, and slice strings.

s = "Hello, Python"
print(s.lower())
print(s.upper())
print(s[0:5])
print(f"Length: {len(s)}")

Output

hello, python
HELLO, PYTHON
Hello
Length: 13

Keep Practicing

Use the online Python compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

You can use the online Python compiler linked above to run examples instantly. For larger projects, install Python locally.

Yes. Examples cover variables, control flow, data structures, functions, classes, and file I/O—ideal for beginners.

Yes. The programs are tested and produce consistent output across major platforms.

Learn Python by Practicing Examples

Hands-on practice is the fastest way to understand Python. Each example focuses on a single concept—from variables, strings, lists, and dictionaries to loops, functions, classes, exceptions, and file I/O. Modify examples, add new functions, and see how clean design leads to readable, maintainable code.

Use our tools to deepen learning: the Python Compiler to run snippets, and the Python Tutorial for structured lessons.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with small, readable programs.

Practical Patterns

Practice lists, dicts, comprehensions, functions, classes, exceptions, and file handling.

Grow Skills

Explore generators and decorators to write expressive, reusable code.