Basic Python Questions and Solutions

@IanChriste
Nov 1 · 3 min read

For Full Description and Even better Explanations go to: https://pynative.com

Question 1: Accept two int values from the user and return their product. If the product is greater than 1000, then return their sum

def multiplication_or_sum(num1, num2):
product = num1 *num2
if(product < 1000):
return product
else:
return num1 +num2

number1 = int(input("Enter first number "))
number2 = int(input("Enter second number"))

result = multiplication_or_sum(number1, number2)
print("The result is", result)

Question 2: Given a range of numbers. Iterate from o^th number to the end number and print the sum of the current number and previous number

def sumNum(num):
previousNum=0
for i in range(num):
sum = previousNum + i
print(sum)
previousNum = i

print("Printing current and previous number sum in a given range")
sumNum(10)

Question 3: Accept string from the user and display only those characters which are present at an even index

def printEveIndexChar(str):
for i in range(0, len(str)-1, 2):
print("index[",i,"]", str[i] )

inputStr = input("Enter String ")
print("Original String is ", inputStr)

print("Printing only even index chars")
printEveIndexChar(inputStr)

Question 4: Given a string and an int n, remove characters from string starting from zero up to n and return a new string

def removeChars(str, n):
return str[n:]

print("Removing n number of chars")
print(removeChars("pynative", 4))

Question 5: Given a list of ints, return True if first and last number of a list is same.

def isFirst_And_Last_Same(numberList):
firstElement = numberList[0]
lastElement = numberList[-1]
if(firstElement == lastElement):
return True
else:
return False

numList = [10, 20, 30, 40, 10]

print("The first and last number of a list is same")
print("result is", isFirst_And_Last_Same(numList))

Question 6: Given a list of numbers, Iterate it and print only those numbers which are divisible of 5

def findDivisible(numberList):
for num in numberList:
if (num % 5 == 0):
print(num)

numList = [10, 20, 33, 46, 55]
print("Finding divisible of 5 in a list")
findDivisible(numList)

Question 7: Return the number of times that the string “Emma” appears anywhere in the given string

def count_jhon(statement):
count = 0
for i in range(len(statement)-1):
count += statement[i:i+4] == 'Emma'
return count

count = count_jhon("Emma is good developer. Emma is aslo a writer")
print("Emma appeared ", count, "times")

Question 8: Print the following pattern

1 
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for num in range(10):
for i in range(num):
print (num, end=" ") #print number
# new line after each row to display pattern correctly
print("\n")

Question 9: Reverse a given number and return true if it is the same as the original number

def reverseCheck(number):
originalNum = number
reverseNum=0
while(number > 0):
reminder = number % 10
reverseNum = (reverseNum *10) + reminder
number = number // 10
if(originalNum == reverseNum):
return True
else:
return False

print("orignal and revers number is equal")
print(reverseCheck(121))

Question 10: Given a two list of ints create a third list such that should contain only odd numbers from the first list and even numbers from the second list

def mergeList(listOne, listTwo):
thirdList = []
for num in listOne:
if(num % 2 != 0):
thirdList.append(num)
for num in listTwo:
if(num % 2 == 0):
thirdList.append(num)
return thirdList

print("Merged List is")
listOne = [10, 20, 23, 11, 17]
listTwo = [13, 43, 24, 36, 12]

print(mergeList(listOne, listTwo))
@IanChriste

Written by

Data Science Enthusiast

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade