Simple Program Code In Python ( Python For Beginners) Part-1
1. Hello World Program:
print("Hello, World!")
2. Simple Calculator:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Quotient:", a / b)
3. Factorial of a Number:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print("Factorial:", factorial(num))
4. Fibonacci Sequence:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
terms = int(input("Enter the number of terms: "))
print("Fibonacci sequence:")
for i in range(terms):
print(fibonacci(i))
5. Check for Prime Number:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
if is_prime(num):
print("Prime")
else:
print("Not prime")
6. Simple Interest Calculator:
p = float(input("Enter the principal amount: "))
r = float(input("Enter the rate of interest: "))
t = float(input("Enter the time period: "))
interest = (p * r * t) / 100
print("Simple Interest:", interest)
7. Check for Even or Odd:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
8. Area of a Circle:
import math
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius * radius
print("Area:", area)
9. List Comprehension:
squares = [i ** 2 for i in range(10)]
print("Squares:", squares)
10. Simple File Handling:
# Writing to a file
with open("output.txt", "w") as file:
file.write("Hello, this is a sample text.")
# Reading from a file
with open("output.txt", "r") as file:
data = file.read()
print("Data from file:", data)
0 Comments