0% found this document useful (0 votes)
5 views4 pages

Python Basic Interview Programs

This document provides a collection of basic Python programs commonly asked in interviews, featuring simple logic and clean code. It includes examples such as printing 'Hello, World!', checking for even or odd numbers, calculating factorials, and handling exceptions. The document serves as a practice guide for candidates preparing for Python interviews.

Uploaded by

test1a6601
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)
5 views4 pages

Python Basic Interview Programs

This document provides a collection of basic Python programs commonly asked in interviews, featuring simple logic and clean code. It includes examples such as printing 'Hello, World!', checking for even or odd numbers, calculating factorials, and handling exceptions. The document serves as a practice guide for candidates preparing for Python interviews.

Uploaded by

test1a6601
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

PYTHON BASIC PROGRAMS FOR

INTERVIEWS
This PDF contains the most commonly asked Python basic programs in interviews with simple logic
and clean code.

1. Hello World Program


print("Hello, World!")

2. Add Two Numbers


a = int(input('Enter a: '))
b = int(input('Enter b: '))
print(a + b)

3. Check Even or Odd


n = int(input('Enter number: '))
if n % 2 == 0:
print('Even')
else:
print('Odd')

4. Check Prime Number


n = int(input('Enter number: '))
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
if flag and n > 1:
print('Prime')
else:
print('Not Prime')

5. Factorial of a Number
n = int(input('Enter number: '))
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)

6. Fibonacci Series
n = int(input('Enter terms: '))
a, b = 0, 1
for i in range(n):
print(a)
a, b = b, a + b

7. Palindrome Number
n = input('Enter number: ')
if n == n[::-1]:
print('Palindrome')
else:
print('Not Palindrome')

8. Armstrong Number
n = int(input('Enter number: '))
temp = n
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if sum == n:
print('Armstrong')
else:
print('Not Armstrong')

9. Reverse a String
s = input('Enter string: ')
print(s[::-1])

10. Count Vowels in String


s = input('Enter string: ')
count = 0
for ch in s:
if [Link]() in 'aeiou':
count += 1
print(count)

11. Find Largest of Three Numbers


a = int(input())
b = int(input())
c = int(input())
print(max(a, b, c))

12. Swap Two Numbers


a = int(input())
b = int(input())
a, b = b, a
print(a, b)
13. Sum of Elements in List
lst = [1,2,3,4]
print(sum(lst))

14. Remove Duplicates from List


lst = [1,2,2,3,4,4]
print(list(set(lst)))

15. Count Words in Sentence


s = input('Enter sentence: ')
print(len([Link]()))

16. Read File Content


file = open('[Link]', 'r')
print([Link]())
[Link]()

17. Exception Handling Example


try:
x = int(input())
print(10/x)
except ZeroDivisionError:
print('Cannot divide by zero')

18. Class and Object Program


class Student:
def __init__(self, name):
[Link] = name
s = Student('Ravi')
print([Link])

19. Lambda Function


add = lambda a, b: a + b
print(add(2, 3))

20. Count Character Frequency


s = 'hello'
freq = {}
for ch in s:
freq[ch] = [Link](ch, 0) + 1
print(freq)
END OF INTERVIEW PROGRAMS
Practice these programs to crack Python interviews.

You might also like