Introduction:
Welcome to our new blog post Python Coding Interview Questions for Freshers. here in this blog, you are going to prepare all the coding questions mostly asked in the Interview.
Python is a high-level, interpreted programming language known for its simplicity and readability. It’s popular because of its versatility, extensive libraries, and wide range of applications, from web development to data analysis and machine learning.
Python Coding Interview Questions for Freshers
01. Write Python code to swap the values of two variables without using a temporary variable.
You can swap the values of two variables without using a temporary variable in Python like this:
a = 5
b = 10
a = a + b
b = a - b
a = a - b
print("a =", a)
print("b =", b)
Output:
a = 10
b = 5
02. Write a Python function to check if a given number is prime.
def is_prime(num):
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
i = 5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True
# Input from the user
user_input = input("Enter a number: ")
try:
num = int(user_input)
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
except ValueError:
print("Invalid input. Please enter a valid integer.")
Output:
Enter a number: 4
4 is not a prime number.
03. Implement a Python function to reverse a string.
def reverse_string(input_str):
return input_str[::-1]
# Input from the user
user_input = input("Enter a string: ")
reversed_str = reverse_string(user_input)
print(f"Reversed string: {reversed_str}")
Output:
Enter a string: Hello, World!
Reversed string: !dlroW ,olleH
04. Write a Python function to calculate the factorial of a non-negative integer.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
# Input from the user
user_input = input("Enter a non-negative integer: ")
try:
num = int(user_input)
if num >= 0:
result = factorial(num)
print(f"The factorial of {num} is {result}")
else:
print("Please enter a non-negative integer.")
except ValueError:
print("Invalid input. Please enter a valid integer.")
Output:
Enter a non-negative integer: 5
The factorial of 5 is 120
05. Implement a Python function to find the maximum element in a list.
def find_max(lst):
if not lst:
return None # Return None for an empty list
max_value = lst[0] # Initialize max_value with the first element
for item in lst:
if item > max_value:
max_value = item # Update max_value if a larger element is found
return max_value
numbers = [3, 7, 2, 8, 5]
max_num = find_max(numbers)
print(f"The maximum element in the list is: {max_num}")
Output:
The maximum element in the list is: 8
6. Write a Python program to calculate simple interest
# Input from the user
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (as a percentage): "))
time = float(input("Enter the time (in years): "))
# Calculate simple interest
simple_interest = (principal * rate * time) / 100
# Display the result
print(f"Simple Interest: {simple_interest}")
Output:
Enter the principal amount: 1000
Enter the annual interest rate (as a percentage): 5
Enter the time (in years): 3
Simple Interest: 150.0
07. Python program to check if a number is an Armstrong number
# Input from the user
num = int(input("Enter a number: "))
# Calculate the number of digits
num_str = str(num)
num_digits = len(num_str)
# Calculate the sum of each digit raised to the power of the number of digits
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
# Check if it's an Armstrong number
if armstrong_sum == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Output:
Enter a number: 153
153 is an Armstrong number.
08. Python program that generates the Fibonacci series up to a specified number of terms
# Input from the user
num_terms = int(input("Enter the number of Fibonacci terms to generate: "))
# Initialize the first two terms of the Fibonacci sequence
a, b = 0, 1
# Display the Fibonacci sequence
if num_terms <= 0:
print("Please enter a positive integer.")
elif num_terms == 1:
print(a)
else:
print("Fibonacci Sequence:")
for _ in range(num_terms):
print(a, end=" ")
a, b = b, a + b
Output:
Enter the number of Fibonacci terms to generate: 10
Fibonacci Sequence:
0 1 1 2 3 5 8 13 21 34
09. Python program to swap two numbers using a third variable
# Input from the user
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
# Swapping with a third variable
temp = a
a = b
b = temp
print("After swapping:")
print("First number:", a)
print("Second number:", b)
Output:
Enter the first number: 5
Enter the second number: 10
After swapping:
First number: 10
Second number: 5
10. Python program to calculate compound interest, taking input from the user:
# Input from the user
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (as a decimal): "))
n = int(input("Enter the number of times interest is compounded per year: "))
time = float(input("Enter the time (in years): "))
# Calculate compound interest
compound_interest = principal * ((1 + rate/n)**(n*time)) - principal
# Display the result
print(f"Compound Interest: {compound_interest}")
Output:
Enter the principal amount: 1000
Enter the annual interest rate (as a decimal): 0.05
Enter the number of times interest is compounded per year: 12
Enter the time (in years): 3
Compound Interest: 157.62815624999958
Reference:
Read More Blogs: