Python Interview Questions and Answers

Python interview questions and answers

 

Python is very popular language nowadays. its used in data science, Artificial Intelligence Machine learning etc. We will see a list of Python Interview Questions and Answers.

we will cover various interview questions on Python programming language, so that you can crack the interview with confidence.

 

What is Python?

Python is a widely used Programming language. Python is high level language. Python is Object Oriented, Interpreted language. Python is used in Artificial Intelligence, Data science, Machine learning. Python is easy to code. Python was developed by Guido van Rossum.

 

How to save Python file?

Python written code file is saved with .py extension. Any Python file is saved with .py, so that Python compiler can understand it and can execute that file

filename.py

 

How to display anything on console in Python?

In Python print() function is used to display anything on the console.

print(value_to_display);

for example:

print("Hello!! ByteArray.in")

output

Hello!! ByteArray.in

 

How to check the Python version?

Python version can be checked by running Python –version command in Python command line interpreter.

python --version

 

How to comment code in python?

In Python, code is commented through hash sign #. Put # (hash sign) before the lines of Python code to comment.

# This is comment in Python by ByteArray.in code
print("Hello!")

 

How to declare Python variable?

Declare a variable in Python is very easy. we don’t need to mention any data type before declaring any data type. Just need variable name and value. Variable data type will be auto detect by Python programming language.

# Programming code in Python by ByteArray.in
name="Python"
age =23

print(name)
print(age)

 

How to convert String to int in Python?

Strings in Python can be converted into int values by using int() method in Python. Only those Strings values can be converted which are valid numeric values.

# Programming code in Python by ByteArray.in
AgeInString="10"
age = int(AgeInString)
print(age)

 

How to convert int to String value in Python?

integer values are converting to String value with str() method in Python.

# Programming code in Python by ByteArray.in
AgeInNumber=10
AgeInString=str(AgeInNumber)
print(AgeInString)

 

How to assign one value to multiple variables in Python?

In Python one value can be assigned to multiple variables like below

# Python Programming code by ByteArray.in
a=b=c=10

print(a)
print(b)
print(c)

 

What is output variable in Python?

Output variable is that variable which is used to display the output of code in Python. In Python print() is output variable or method which is used to display the result.

# Python Programming code by ByteArray.in
print("Hello")
print(3+7)
a=20
print(a)

 

What is global variable in Python?

Global variable are variable which are accessible globally in the Python code. These variables are declared outside any function.

 

What are various data types in Python?

Python has below data type:

  • String
  • int
  • float
  • list
  • tuple
  • range
  • dict
  • bool
  • complex

 

How to find the data type of any variable in Python?

To find the data type of any variable use type() function in Python. type() give the data type of Python variable.

# Python Programming code by ByteArray.in

name="ByteArray.in"
age=1
price=234.98

print(type(name))
print(type(age))
print(type(price))

 

output

<class 'str'>
<class 'int'>
<class 'float'>

 

How to do substring in python?

Substring means the portion of the String. In Python, often its called slicing.

# Python Programming code by ByteArray.in

name="ByteArray.in"

print(name[0:4])

 

output

Byte

 

How to remove any white-space in Python?

Whitespace means any extra left side and right space. In Python whitespace is removed by strip() method.

#Python code by ByteArray.in

name = " ByteArray.in "
print(name)
host = name.strip()
print(host)

 

output

 

     ByteArray.in    
ByteArray.in

 

What is the use of upper() and lower() method in Python?

upper() and lower() functions convert the Python String characters to uppercase and lowercase respectively.

upper() : It will convert all text to capital letters.

lower() : It will convert all text to small letters.

#Python code by ByteArray.in

name = "ByteArray.in"
print(name.upper())
print(name.lower())

output

BYTEARRAY.IN
bytearray.in

 

What is the purpose of split() function in Python?

split() function is used to split the String into small String elements. These small elements becomes Python list. By default Python split() function divide the String by space.

#Python code by ByteArray.in

name = "hello ByteArray.in how are you"
str=name.split()
print(str)

#check datatype of split string
print(type(str))

output

['hello', 'ByteArray.in', 'how', 'are', 'you']
<class 'list'>

 

How to concatenate two or more strings in Python?

In Python Strings are concatenated with plus sign +.

#Python String concatenate example by ByteArray.in

fname="Byte"
lname="Array.in"

fullname=fname+lname

print(fullname)

 

output

ByteArray.in

 

What is the use of format() function in Python?

format() function finds the variable in {} and replace with actual assigned values.

&lt;/pre&gt;
#Python format function example by ByteArray.in

str="Hello {name}, Are you from {location}?"

print(str.format(name="ByteArray.in",location="India"))

output

Hello ByteArray.in, Are you from India?

 

How is use to escape character in Python?

In Python backslash \ is used for escape the character in Python.

 

What is list in Python?

list is one of the data structure. It is used to store the elements. Its dynamic in nature means it can store as many as elements. Elements are stored in ordered sequence.

In Python, list is created with [] sign.

#Python list example by ByteArray.in

menu=["Coffee","Shakes","Cold Drinks"]

print(menu)

print(type(menu))

 

output

['Coffee', 'Shakes', 'Cold Drinks']
<class 'list'>


How to add the elements in Python?

Using append() method of list, items can be added into the Python list.

#Python list example by ByteArray.in

menu=["Coffee","Shakes","Cold Drinks"]

menu.append("Sandwich")

print(menu)

print(type(menu))

output

['Coffee', 'Shakes', 'Cold Drinks', 'Sandwich']
<class 'list'>

 

What is the use of extend method in list in Python?

extend() method add the elements at the end of the list.

#Python extend function example by ByteArray.in

menu=["Coffee","Shakes","Cold Drinks"]

price=[100,200,300]

menu.extend(price)
print(menu)

output

['Coffee', 'Shakes', 'Cold Drinks', 100, 200, 300]

 

How to get the length of a String in Python?

len() function is used to get the length of a String in Python

#Python len function example by ByteArray.in

str="hello ByteArray.in"

print(len(str))

output

18

 

What pop() method do in list in Python?

Purpose of pop() method is to remove the item from specific position from the list.

menu = ["Coffee","Cold Drinks","Sandwich"]

item= menu.pop(0)

print(menu)

output

['Cold Drinks', 'Sandwich']