- A simple program that prints "Hello, World!" to the console:
print("Hello, World!")
- A program that takes user input and calculates the factorial of the number:
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num+1):
factorial *= i
print("The factorial of", num, "is", factorial)
- A program that uses a function to calculate the area of a triangle:
def area_of_triangle(base, height):
return (base * height) / 2
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
print("The area of the triangle is", area_of_triangle(base, height))
- A program that uses a while loop to print the first 10 numbers in the Fibonacci sequence:
a, b = 0, 1
count = 0
while count < 10:
print(a)
c = a + b
a = b
b = c
count += 1
These are just basic examples of Python programming, Python is a versatile language with a vast library and can be used to build a wide variety of software applications, such as web development, data analysis, machine learning and artificial intelligence, and many more.
0 Comments