0% found this document useful (0 votes)
2 views5 pages

Python Programs

Uploaded by

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

Python Programs

Uploaded by

minimilitia4556
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programs - Answers

1. Hello World
print("Hello World")

2. Take input and display


name = input("Enter something: ")
print("You entered:", name)

3. Swap two numbers


a = int(input("Enter a: "))
b = int(input("Enter b: "))
a, b = b, a
print("After swap:", a, b)

4. Even or Odd
n = int(input("Enter number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")

5. Largest of three numbers


a = int(input())
b = int(input())
c = int(input())
print("Largest:", max(a, b, c))

6. Factorial
n = int(input())
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial:", fact)
7. Fibonacci
n = int(input())
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a+b

8. Reverse number
n = int(input())
rev = 0
while n > 0:
rev = rev*10 + n%10
n //= 10
print("Reverse:", rev)

9. Prime check
n = int(input())
if n > 1:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

10. Sum of digits


n = int(input())
s=0
while n > 0:
s += n % 10
n //= 10
print("Sum:", s)

11. Reverse string


s = input()
print(s[::-1])
12. Palindrome string
s = input()
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

13. Count vowels and consonants


s = input().lower()
v=c=0
for ch in s:
if [Link]():
if ch in "aeiou":
v += 1
else:
c += 1
print("Vowels:", v, "Consonants:", c)

14. Length without len()


s = input()
count = 0
for i in s:
count += 1
print("Length:", count)

15. Remove spaces


s = input()
print([Link](" ", ""))

16. Count substring


s = input()
sub = input()
print([Link](sub))

17. Uppercase
s = input()
print([Link]())
18. Replace vowels with *
s = input()
vowels = "aeiouAEIOU"
res = ""
for ch in s:
if ch in vowels:
res += "*"
else:
res += ch
print(res)

19. Anagram
s1 = input()
s2 = input()
if sorted(s1) == sorted(s2):
print("Anagram")
else:
print("Not Anagram")

20. First non-repeated char


s = input()
for ch in s:
if [Link](ch) == 1:
print(ch)
break

21. Largest in list


lst = list(map(int, input().split()))
print(max(lst))

22. Smallest in list


lst = list(map(int, input().split()))
print(min(lst))

23. Sum of list


lst = list(map(int, input().split()))
print(sum(lst))
24. Remove duplicates
lst = list(map(int, input().split()))
print(list(set(lst)))

25. Sort list


lst = list(map(int, input().split()))
[Link]()
print(lst)

You might also like