0% found this document useful (0 votes)
8 views7 pages

Python Programs

Uploaded by

Yashwant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Python Programs

Uploaded by

Yashwant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Print Hello World

print("Hello, World!")

2. Add Two Numbers

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
sum = a + b
print("Sum:", sum)

3. Check Even or Odd

num = int(input("Enter a number: "))


if num % 2 == 0:
print("Even")
else:
print("Odd")

4. Find Largest of Three Numbers

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Largest is:", a)
elif b >= a and b >= c:
print("Largest is:", b)
else:
print("Largest is:", c)

5. Simple Calculator (Add, Subtract, Multiply, Divide)

a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
op = input("Choose operation (+, -, *, /): ")

if op == '+':
print("Result:", a + b)
elif op == '-':
print("Result:", a - b)
elif op == '*':
print("Result:", a * b)
elif op == '/':
print("Result:", a / b)
else:
print("Invalid operator")

6. Print First 10 Natural Numbers Using While Loop

i = 1
while i <= 10:
print(i)
i += 1

7. Check if Number is Prime


num = int(input("Enter a number: "))
is_prime = True

if num <= 1:
is_prime = False
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break

if is_prime:
print("Prime number")
else:
print("Not a prime number")

8. Reverse a String

s = input("Enter a string: ")


print("Reversed string:", s[::-1])

9. Sum of Digits of a Number

num = int(input("Enter a number: "))


sum = 0
while num > 0:
digit = num % 10
sum += digit
num //= 10
print("Sum of digits:", sum)

10. Factorial of a Number

num = int(input("Enter a number: "))


fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
11. Check Palindrome (string)

text = input("Enter a word: ")


if text == text[::-1]:
print("Palindrome")
else:
print("Not a palindrome")

12. Check Leap Year

year = int(input("Enter a year: "))


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")

13. Print Fibonacci Series (First n Terms)

n = int(input("Enter number of terms: "))


a, b = 0, 1
for i in range(n):
print(a, end=' ')
a, b = b, a + b

14. Count Vowels in a String

s = input("Enter a string: ")


vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
print("Number of vowels:", count)

15. Print Multiplication Table

num = int(input("Enter a number: "))


for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

16. Sum of Elements in a List

numbers = [5, 10, 15, 20]


total = sum(numbers)
print("Sum of list:", total)

17. Find the Largest Element in a List

numbers = [4, 17, 9, 21, 3]


print("Largest number:", max(numbers))

18. Find Factor of a Number

num = int(input("Enter a number: "))


print("Factors are:")
for i in range(1, num + 1):
if num % i == 0:
print(i)

19. Print All Prime Numbers in a Range

start = int(input("Start: "))


end = int(input("End: "))
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num)

20. Check if Number is Armstrong

(A number is Armstrong if sum of cubes of its digits is equal to the number itself.)

num = int(input("Enter a number: "))


temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if sum == num:
print("Armstrong number")
else:
print("Not an Armstrong number")
✅ 21. Guess the Number Game

import random
number = random.randint(1, 10)
guess = int(input("Guess a number between 1 and 10: "))

if guess == number:
print("Congratulations! You guessed it right.")
else:
print(f"Sorry, the correct number was {number}")

✅ 22. Pattern Printing (Right Triangle)

rows = int(input("Enter number of rows: "))


for i in range(1, rows + 1):
print("*" * i)

✅ 23. Find Unique Elements from a List

nums = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(nums))
print("Unique elements:", unique)

✅ 24. Calculator Using Functions

def add(a, b):


return a + b

def sub(a, b):


return a - b

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
print("Addition:", add(a, b))
print("Subtraction:", sub(a, b))

✅ 25. Check if a String is Anagram

str1 = input("Enter first string: ")


str2 = input("Enter second string: ")

if sorted(str1) == sorted(str2):
print("Anagram")
else:
print("Not anagram")

✅ 26. Count Words in a Sentence

sentence = input("Enter a sentence: ")


words = sentence.split()
print("Number of words:", len(words))

✅ 27. Simple Digital Clock (using time module)

import time
print("Press Ctrl+C to stop.")
while True:
print(time.strftime("%H:%M:%S"), end='\r')
time.sleep(1)

✅ 28. Rock, Paper, Scissors Game

import random
options = ["rock", "paper", "scissors"]
user = input("Choose rock, paper, or scissors: ").lower()
comp = random.choice(options)

print(f"You chose {user}, computer chose {comp}")

if user == comp:
print("It's a tie!")
elif (user == "rock" and comp == "scissors") or \
(user == "scissors" and comp == "paper") or \
(user == "paper" and comp == "rock"):
print("You win!")
else:
print("You lose!")

✅ 29. Create a Simple Quiz

score = 0
print("Q1: What is the capital of India?")
ans = input("Your answer: ")
if ans.lower() == "new delhi":
score += 1

print("Q2: 5 + 7 = ?")
ans = input("Your answer: ")
if ans == "12":
score += 1

print(f"You got {score}/2 correct.")

✅ 30. Basic Turtle Graphics Drawing

import turtle

t = turtle.Turtle()
t.color("blue")
t.pensize(3)

for i in range(4):
t.forward(100)
t.right(90)

turtle.done()

You might also like