| |

Python Code Examples

Last Updated on May 13, 2026 by Admin

1. Hello, world!

print('Hello, world!')
Python

2. Add two numbers

a = 10
b = 5
total = a + b
print(total)
Python

3. Display Python version

import platform

print("Python version: ", platform.python_version())
Python

4. Store a string in a variable

name = "Thushara"
print(name)
Python

5. Store numbers in variables

age = 30
height = 1.75

print(age)
print(height)
Python

6. Check data types

x = 10
y = 3.14
z = "Python"

print(type(x))
print(type(y))
print(type(z))
Python

7. Basic arithmetic

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
Python

8. Floor division and remainder

a = 10
b = 3

print(a // b)
print(a % b)
Python

9. Exponentiation

base = 2
power = 5

print(base ** power)
Python

10. Using f-strings

name = "Thushara"
age = 30

print(f"My name is {name} and I am {age} years old.")
Python

11. Get user input

name = input("Enter your name: ")
print(f"Hello, {name}")
Python

12. Convert input to integer

age = int(input("Enter your age: "))
print(f"Next year you will be {age + 1}")
Python

13. Convert input to float

price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))

total = price * quantity
print(f"Total: {total}")
Python

14. Simple if statement

age = 20

if age >= 18:
    print("Adult")
Python

15. if-else statement

age = 16

if age >= 18:
    print("Adult")
else:
    print("Minor")
Python

16. if-elif-else statement

score = 75

if score >= 85:
    print("High Distinction")
elif score >= 75:
    print("Distinction")
elif score >= 65:
    print("Credit")
else:
    print("Pass or below")
Python

17. Logical operators

age = 25
income = 70000

if age >= 18 and income > 50000:
    print("Eligible")
else:
    print("Not eligible")
Python

18. Using or

has_ticket = True
is_vip = False

if has_ticket or is_vip:
    print("Entry allowed")
else:
    print("Entry denied")
Python

19.Using not

is_raining = False

if not is_raining:
    print("Go for a walk")
Python

20. For loop with range

for i in range(5):
    print(i)
Python

21. For loop from 1 to 5

for i in range(1, 6):
    print(i)
Python

22. Count backwards

for i in range(5, 0, -1):
    print(i)
Python

23. While loop

count = 1

while count <= 5:
    print(count)
    count += 1
Python

24. Break from loop

for i in range(1, 10):
    if i == 5:
        break
    print(i)
Python

25. Continue in loop

for i in range(1, 6):
    if i == 3:
        continue
    print(i)
Python

26. Nested loops

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)
Python

27. Print a triangle pattern

for i in range(1, 6):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
Python

28. Create a list

numbers = [10, 20, 30]
print(numbers)
Python

29. Access list items

numbers = [10, 20, 30]
print(numbers[0])
print(numbers[1])
Python

30. Modify a list item

numbers = [10, 20, 30]
numbers[1] = 99
print(numbers)
Python

31. Append to a list

numbers = [10, 20]
numbers.append(30)
print(numbers)
Python

32. Insert into a list

numbers = [10, 30]
numbers.insert(1, 20)
print(numbers)
Python

33. Remove from a list

numbers = [10, 20, 30]
numbers.remove(20)
print(numbers)
Python

34. Pop from a list

numbers = [10, 20, 30]
last = numbers.pop()
print(last)
print(numbers)
Python

35. Sort a list

numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)
Python

36.Reverse sort a list

numbers = [5, 2, 9, 1]
numbers.sort(reverse=True)
print(numbers)
Python

37. Loop through a list

names = ["BHP", "CBA", "NAB"]

for name in names:
    print(name)
Python

38. List length

numbers = [10, 20, 30]
print(len(numbers))
Python

39. Sum list values

numbers = [10, 20, 30]
print(sum(numbers))
Python

40. Find maximum and minimum

numbers = [10, 20, 30]
print(max(numbers))
print(min(numbers))
Python

41. List slicing

numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Python

42. Create a tuple

point = (10, 20)
print(point)
Python

43. Tuple unpacking

point = (10, 20)
x, y = point

print(x)
print(y)
Python

44. Create a dictionary

student = {
    "name": "Alex",
    "score": 85
}

print(student)
Python

45. Access dictionary value

student = {"name": "Alex", "score": 85}
print(student["name"])
Python

46. Use dictionary get

student = {"name": "Alex", "score": 85}
print(student.get("grade", "Not available"))
Python

47. Add dictionary item

student = {"name": "Alex"}
student["score"] = 85
print(student)
Python

48. Update dictionary item

student = {"name": "Alex", "score": 85}
student["score"] = 90
print(student)
Python

49. Loop through dictionary keys

student = {"name": "Alex", "score": 85}

for key in student:
    print(key)
Python

50. Loop through dictionary values

student = {"name": "Alex", "score": 85}

for value in student.values():
    print(value)
Python

51. Loop through dictionary items

student = {"name": "Alex", "score": 85}

for key, value in student.items():
    print(key, value)
Python

52. Count values using a dictionary

values = [4, 8, 7, 7]
counts = {}

for value in values:
    if value in counts:
        counts[value] += 1
    else:
        counts[value] = 1

print(counts)
Python

xx. Right Pascal Triangle

n = 5

# Upper part
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

# Lower part
for i in range(n - 1, 0, -1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
Python

xx. Heart

n = 5

for i in range(6):
    for j in range(7):
        if (i==0 and j%3==0) or (i==1 and j%3==0) or (i==1 and j%3==0) or (i-j==2) or (i+j==8):
            print("*", end =" ")
        else:
            print(" ", end =" ")
    print()
Python

xx. Turtle pattern

from turtle import *
import colorsys

speed(0)
bgcolor("black")
h = 0

for i in range(16):
    for j in range(18):
        c = colorsys.hsv_to_rgb(h, 1,1)
        color(c)
        h += 0.005
        rt(90)
        circle(150 - j * 6, 90)
        lt(90)
        circle(150 - j * 6, 90)
        rt(180)
        circle(40, 24)

done()
Python

xx. Rainbow spiral

from turtle import *
import colorsys

speed(0)
bgcolor("black")
h = 0

for i in range(200):
    c = colorsys.hsv_to_rgb(h, 1, 1)
    color(c)
    h += 0.01
    forward(i)
    right(59)

done()
Python

xx. Efficient Frontier (Matplotlib)

xx. Efficient Frontier (Plotly)

Similar Posts