Python Code Examples
Last Updated on May 13, 2026 by Admin
1. Hello, world!
print('Hello, world!')Python2. Add two numbers
a = 10
b = 5
total = a + b
print(total)Python3. Display Python version
import platform
print("Python version: ", platform.python_version())Python4. Store a string in a variable
name = "Thushara"
print(name)Python5. Store numbers in variables
age = 30
height = 1.75
print(age)
print(height)Python6. Check data types
x = 10
y = 3.14
z = "Python"
print(type(x))
print(type(y))
print(type(z))Python7. Basic arithmetic
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)Python8. Floor division and remainder
a = 10
b = 3
print(a // b)
print(a % b)Python9. Exponentiation
base = 2
power = 5
print(base ** power)Python10. Using f-strings
name = "Thushara"
age = 30
print(f"My name is {name} and I am {age} years old.")Python11. Get user input
name = input("Enter your name: ")
print(f"Hello, {name}")Python12. Convert input to integer
age = int(input("Enter your age: "))
print(f"Next year you will be {age + 1}")Python13. Convert input to float
price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))
total = price * quantity
print(f"Total: {total}")Python14. Simple if statement
age = 20
if age >= 18:
print("Adult")Python15. if-else statement
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")Python16. 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")Python17. Logical operators
age = 25
income = 70000
if age >= 18 and income > 50000:
print("Eligible")
else:
print("Not eligible")Python18. Using or
has_ticket = True
is_vip = False
if has_ticket or is_vip:
print("Entry allowed")
else:
print("Entry denied")Python19.Using not
is_raining = False
if not is_raining:
print("Go for a walk")Python20. For loop with range
for i in range(5):
print(i)Python21. For loop from 1 to 5
for i in range(1, 6):
print(i)Python22. Count backwards
for i in range(5, 0, -1):
print(i)Python23. While loop
count = 1
while count <= 5:
print(count)
count += 1Python24. Break from loop
for i in range(1, 10):
if i == 5:
break
print(i)Python25. Continue in loop
for i in range(1, 6):
if i == 3:
continue
print(i)Python26. Nested loops
for i in range(1, 4):
for j in range(1, 4):
print(i, j)Python27. Print a triangle pattern
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()Python28. Create a list
numbers = [10, 20, 30]
print(numbers)Python29. Access list items
numbers = [10, 20, 30]
print(numbers[0])
print(numbers[1])Python30. Modify a list item
numbers = [10, 20, 30]
numbers[1] = 99
print(numbers)Python31. Append to a list
numbers = [10, 20]
numbers.append(30)
print(numbers)Python32. Insert into a list
numbers = [10, 30]
numbers.insert(1, 20)
print(numbers)Python33. Remove from a list
numbers = [10, 20, 30]
numbers.remove(20)
print(numbers)Python34. Pop from a list
numbers = [10, 20, 30]
last = numbers.pop()
print(last)
print(numbers)Python35. Sort a list
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)Python36.Reverse sort a list
numbers = [5, 2, 9, 1]
numbers.sort(reverse=True)
print(numbers)Python37. Loop through a list
names = ["BHP", "CBA", "NAB"]
for name in names:
print(name)Python38. List length
numbers = [10, 20, 30]
print(len(numbers))Python39. Sum list values
numbers = [10, 20, 30]
print(sum(numbers))Python40. Find maximum and minimum
numbers = [10, 20, 30]
print(max(numbers))
print(min(numbers))Python41. List slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])Python42. Create a tuple
point = (10, 20)
print(point)Python43. Tuple unpacking
point = (10, 20)
x, y = point
print(x)
print(y)Python44. Create a dictionary
student = {
"name": "Alex",
"score": 85
}
print(student)Python45. Access dictionary value
student = {"name": "Alex", "score": 85}
print(student["name"])Python46. Use dictionary get
student = {"name": "Alex", "score": 85}
print(student.get("grade", "Not available"))Python47. Add dictionary item
student = {"name": "Alex"}
student["score"] = 85
print(student)Python48. Update dictionary item
student = {"name": "Alex", "score": 85}
student["score"] = 90
print(student)Python49. Loop through dictionary keys
student = {"name": "Alex", "score": 85}
for key in student:
print(key)Python50. Loop through dictionary values
student = {"name": "Alex", "score": 85}
for value in student.values():
print(value)Python51. Loop through dictionary items
student = {"name": "Alex", "score": 85}
for key, value in student.items():
print(key, value)Python52. 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)Pythonxx. 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()Pythonxx. 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()Pythonxx. 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)

