print
function.# Python program to print Hello World
print("Hello, World!")
Output:Hello, World!
Suggested Visual: A simple infographic explaining the structure of a Python program.# Python program to add two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("The sum is:", sum)
Input:Enter first number: 5
Enter second number: 3
Output:The sum is: 8.0
Suggested Visual: Flowchart demonstrating user input, addition, and output.Area = πr²
, calculate the area of a circle.# Python program to calculate the area of a circle
import math
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius ** 2
print("The area of the circle is:", area)
Input:Enter the radius of the circle: 5
Output:The area of the circle is: 78.53981633974483
Suggested Visual: Diagram showing a circle with a labeled radius and formula.# Python program to swap two variables using a temporary variable
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
temp = x
x = y
y = temp
print("After swapping: x =", x, "and y =", y)
# Python program to swap two variables without a temporary variable
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
x, y = y, x
print("After swapping: x =", x, "and y =", y)
Suggested Visual: Side-by-side code comparison with diagrams for both methods.# Python program to find the factorial of a number using recursion
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
number = int(input("Enter a number: "))
print("The factorial of", number, "is", factorial(number))
Input:Enter a number: 5
Output:The factorial of 5 is 120
Suggested Visual: Recursive tree diagram for calculating factorial.random
module.# Python program to generate random numbers
import random
random_num = random.randint(1, 100)
print("Random number between 1 and 100:", random_num)
math
module for these calculations.# Python program to find LCM and GCD of two numbers
import math
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
gcd = math.gcd(num1, num2)
lcm = (num1 * num2) // gcd
print("GCD:", gcd)
print("LCM:", lcm)
# Python calculator
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4' and num2 != 0:
print("Result:", num1 / num2)
else:
print("Invalid input")
Suggested Visual: UI mockup of a basic calculator.# Python program to add two matrices
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]
print("Resultant Matrix:", result)