Basics Py
Mon 30 June 2025
# Declare variable
x = 5
# Print variable
print(x)
5
# Update variable
x = 10
# Print new value
print("Updated x:", x)
Updated x: 10
text = "Hello Sara"
print(text)
Hello Sara
print("Length:", len(text))
Length: 10
print("Upper:", text.upper())
Upper: HELLO SARA
print("Replace:", text.replace("Sara", "Queen"))
Replace: Hello Queen
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
apple
fruits.append("orange")
print(fruits)
['apple', 'banana', 'cherry', 'orange']
fruits.remove("banana")
print(fruits)
['apple', 'cherry', 'orange']
a = 15
b = 4
print("Addition:", a + b)
Addition: 19
print("Subtraction:", a - b)
Subtraction: 11
print("Multiplication:", a * b)
Multiplication: 60
print("Division:", a / b)
Division: 3.75
print("Modulus:", a % b)
Modulus: 3
print("Exponentiation:", a ** b)
Exponentiation: 50625
# Casting to int
num = int("5")
print("Integer:", num)
Integer: 5
# Casting to float
f = float("3.14")
print("Float:", f)
Float: 3.14
# Casting to string
s = str(123)
print("String:", s)
String: 123
# Type checking
print(type(f))
<class 'float'>
print(type(s))
<class 'str'>
# For loop
for i in range(3):
print("For loop iteration:", i)
For loop iteration: 0
For loop iteration: 1
For loop iteration: 2
# While loop
count = 0
while count < 2:
print("While loop count:", count)
count += 1
While loop count: 0
While loop count: 1
# Break
for i in range(5):
if i == 3:
break
print("Breaking at:", i)
Breaking at: 0
Breaking at: 1
Breaking at: 2
# Continue
for i in range(5):
if i == 2:
continue
print("Continue at:", i)
Continue at: 0
Continue at: 1
Continue at: 3
Continue at: 4
# Continue
for i in range(5):
if i == 2:
continue
print("Continue at:", i)
Continue at: 0
Continue at: 1
Continue at: 3
Continue at: 4
# Pass
for i in range(1):
pass # Placeholder
# Pass
for i in range(1):
pass # Placeholder
x = 10
if x > 5:
print("x is greater than 5")
x is greater than 5
if x == 10:
print("x is exactly 10")
x is exactly 10
if x < 5:
print("x is less than 5")
else:
print("x is not less than 5")
x is not less than 5
if x > 5 and x < 15:
print("x is between 5 and 15")
x is between 5 and 15
if x == 5 or x == 10:
print("x is either 5 or 10")
x is either 5 or 10
print("basics done")
basics done
Score: 40
Category: basics