• info@introtallent.com
  • +91 8431610064
Introtallent
  • Courses
    • Data Science PRO [OFFLINE]
    • Data Science PRO [Live Online]
    • Analytics PRO [Live Online]
    • AI Engineer Certificate [Live Online]
  • Hire From Us
  • Student
    • Download Android App
    • Resources
      • SQL Interview Questions
      • Tableau Interview Questions
      • Power BI Interview Questions
      • Excel Interview Questions
      • Python Interview Question
    • LMS Login
  • Login
  • |
  • Sign up
    • Login
    • Sign up

Python codes for interview preparation

  • April 1, 2021June 4, 2023

Palindrome:

A palindrome is a word, sentence, verse, or number that reads the same backward or forward. For example: madam, level, noon, etc

# Python program to check if a string is palindrome or not

x = input('Enter a number or string:')

# make it suitable for caseless comparison
x = x.lower()

# reverse the string
rev_x = reversed(x)

# check if the string is equal to its reverse
if list(x) == list(rev_x):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

 

#palindrome test using index to reverse it


x = input('Enter a number or string:')
if(x==x[::-1]):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

 

#Using loop

#Enter input string
x = input('Enter a number or string:')

#Declare an empty string variable
rev_x=""

#Iterate string with for loop
for i in x:
rev_x = i+rev_x

if(x==rev_x):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")


 

#check if a number is palindrome or not


x = int(input("Enter a number:"))
temp = x
rev = 0
while x > 0:
digit = x %10
rev = rev * 10 + digit
x = x // 10

if(temp == rev):
print("This number is a palindrome")
else:
print("This number is not a palindrome")

Armstrong number:

If a positive integer is equal to the sum of each digit powered with the total number of digits, then that number is called Armstrong Number.

Armstrong numbers are also called narcissist numbers.

For example: 153 is an armstrong number because 1 to the power 3 + 5 to the power 3 + 3 to the power 3=153

153, 370, 371,407, 1634 are armstrong numbers

So, a positive integer will be known as an Armstrong number of order n if

abcd… = an + bn + cn + dn + …

 

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

#initialize a variable to store the sum
numsum = 0

#check the number of digits in the number
n_len = len(str(num))

#store the number in a temporary variable
temp = num

while temp > 0:
digit = temp % 10
numsum= numsum+digit ** n_len
temp= temp//10

if num == numsum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")


 

 

#Print Armstrong numbers in a given range


num1 = int(input("Enter lower range: "))
num2 = int(input("Enter upper range: "))

for num in range(num1,num2 + 1):

#initialize a variable to store the sum
numsum = 0

#check the number of digits in the number
n_len = len(str(num))

#store the number in a temporary variable
temp = num

#login to test armstrong number
while temp > 0:
digit = temp % 10
numsum= numsum+digit ** n_len
temp = temp // 10

#if value is num variable is same as numsum then it's an armstrong number
if num == numsum:
print(num)

Fibonacci Series:

The Fibonacci series is a set of integers that starts with a zero, followed by a one, and then follows the rule that each number is equal to the sum of the preceding two numbers. Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, ….

 

# Program to display the Fibonacci series up to n-th term

n_terms = int(input("How many terms of fibonacci series you want to print? "))

# declare first two terms
n1=0
n2=1
count = 0

# check if the number of terms is valid or not
if n_terms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1

elif n_terms == 1:
print("Fibonacci series upto",n_terms,"is")
print(n1)

# generate fibonacci series
else:
print("Fibonacci series:")

while count < n_terms:
print(n1)
nth = n1 + n2

# update values
n1 = n2
n2 = nth
count += 1

List Comprehension

  • List comprehension is creating a list from an existing list.
  • List comprehension is considerably faster than processing a list using the for loop.
  • List comprehension contains three parts:
    • an expression
    • one or more for loop, and
    • one or more if conditions (based on a need).

 


#Create a list of even numbers using List Comprehension

even_num_list = [x for x in range(50) if x%2 == 0]
print(even_num_list)


#Print the list of numbers greater than 5 using list comprehension

num_list = [2,4,5,6,7,10,12,15,20]
num_gt_5 = [x for x in num_list if x > 5]

print(num_gt_5)

 

#Create a list of results with even or odd in that based on a given list of integers
my_list = ["Even" if i%2==0 else "Odd" for i in range(10)]
print(my_list)

 

One of the applications of list comprehension is to flatten a list comprising of multiple lists into a single list.

#Create a flat list by using the a list comprising of multiple lists.
my_list=[[5,10,15],[20,25,30],[35,35,40]]
flat_list=[i for row in my_list for i in row]
print(flat_list)

 

#Create a list of names that contains 'm' 
name_list = ['Ram', 'Sam', 'Kris', 'Tom', 'Jim', 'Raj', 'Tina']
name_has_m = [i for i in name_list if 'm' in i]
print(name_has_m)

 

#Create a list by taking all combinations of items from two lists in the form of a tuple and add them in a third list object.

list1 = [5, 3, 9]
list2 = [7, 2, 1]
list3=[(i,j) for i in list1 for j in list2]
print(list3)

 

#from a given list, create a list of numbers that are divisible by 3 but not by 5

my_list = [i for i in range(50) if i%3==0 if i%5!=0]
print(my_list)

Prime Number

Check whether a number is prime or not?

#take a number input to test prime number
n=int(input("Enter number: "))

#temporary variable
temp=0

#execute a loop from 2 to the half of the number.
#for example if n is 15 then for loop will execute from 2 to 7 (i.e. 15//2)
for i in range(2,n//2+1):

#test whether the number n has remainder 0 when it is divided by i
if(n%i==0):

#if remainder of n%i is 0 then increase temp
temp=temp+1

#if n is 0 or 1 or temp>0 then n is not a prime
if (n==0) or (n==1) or (temp>0):
print(n,'is not prime')
else:
print(n,"is prime")

 

 

#Print prime numbers up to a given number (in a range from 2 to num)

#enter a value to print the prime numbers
num=int(input("Enter upper limit: "))

#loop starting from to to the given number
for num in range(2,num+1):
#temporary variable
temp=0

#execute a loop from 2 to the half of the number.
#for example if n is 15 then for loop will execute from 2 to 7 (i.e. 15//2)
for i in range(2,num//2+1):

#if remainder of num%i is 0 then increase temp
if(num%i==0):
temp=temp+1

#if temp is <=0 then print num (it's a prime number)
if(temp<=0):
print(num)

Share this:

  • Facebook
  • X

Related

Post navigation

Previous Post
Next Post

Related Posts

  • Understanding the Differences Between Data Science and Data Analytics
    December 17, 2024
  • Key Data Science and AI Trends to Watch in 2025
    November 5, 2024
  • Mastering the XLOOKUP Function in Excel: Step-by-Step
    June 2, 2024
  • The Rise of AI in Business Analytics For Decision-Making
    May 11, 2024
  • What Is Generative AI And How Does It Work? A Comprehensive Guide
    April 12, 2024
  • Machine Learning: A Beginner’s Guide to Understanding the Basics
    April 12, 2024
  • 9 Steps for Crafting an Ultimate Data Scientist Resume
    March 16, 2024
  • Data Science Career Opportunities: Your Guide To Unlock Top Data Scientist Jobs
    December 31, 2023
  • Best Career Options After Pursuing Data Science Course
    December 30, 2023
  • Harness the Power of Data with Six Captivating Tableau Projects
    December 29, 2023
  • Definitive List of Top 10 Data Science Tools
    December 28, 2023
  • Top 15 Data Science Interview Questions You Must Nail!
    November 30, 2023
  • How to Become a Data Scientist in Bangalore?
    November 30, 2023
  • Impact of Data Science in Business and Its Uses
    November 30, 2023
  • Essential Role of Data Scientist in Today’s Tech Landscape
    November 30, 2023
  • 5 Transformative Benefits of Tableau Certification to Boost Career
    October 24, 2023
  • Types of Artificial Intelligence Categories You Must Familiarize With
    October 23, 2023
  • Exploring the Need for Data Science and AI Experts in 2023
    October 20, 2023
  • What Does a Data Analyst Do: Roles, Skills, and Salary?
    October 20, 2023
  • 5 Key Reasons Why Data Analytics is Important to Business
    September 26, 2023
  • Elevate Your Career with Data Science Jobs: A Complete Guide
    September 26, 2023
  • Is AI Engineering a Good Career Option in 2023 ?
    September 26, 2023
  • Ultimate Guide to Statistical Analysis for Data Science
    September 16, 2023
  • What is Data Science – A complete overview with future
    August 15, 2023
  • Power BI Interview Questions and Answers
    August 3, 2023
  • Top 50 Data Science Interview Questions with Answers
    September 7, 2022
  • Excel Interview Success: Top 40 Questions and How to Answer Them
    July 5, 2022
  • SQL Interview Questions with Answers – An Ultimate Guide
    June 20, 2022
  • 12 Must Know Problem Solving Questions Asked in Analytics Interview
    May 8, 2022
  • 17 Ultimate Non-Tech Interview Questions to Empower Your Career Path
    May 2, 2021

Introtallent

Best Data Science
training institute
in Bangalore.

Follow Us

Contact Us

  • #10, 1st Floor,
    12th Cross, CMH Road, Indiranagar, Bangalore 560037

  • info@introtallent.com

  • +91 843 161 0064

Feel free to contact us.
  • About Us
  • Analytics PRO
  • Analytics Training
  • Artificial Intelligence Course
  • Blog
  • Data Science and Analytics Jobs
  • Data Science PRO
  • Hire from us
  • Home
  • Introtallent in News
  • Introtallent Placements
  • Privacy Policy
  • Student Registration
  • Tableau Course
 © 2017-2024 Introtallent . All Rights Reserved