Python Interview Questions
Python Interview Questions
1) What is Python?
Python was created by Guido van Rossum, and released in 1991.
It is a general-purpose computer programming language. It is a high-level, object-
oriented language which can run equally on different platforms such as Windows,
Linux, UNIX, and Macintosh. Its high-level built-in data structures, combined with
dynamic typing and dynamic binding. It is widely used in data science, machine
learning and artificial intelligence domain.
It is easy to learn and require less code to develop the applications.
2) Why Python?
5) What is PEP 8?
PEP 8 stands for Python Enhancement Proposal, it can be defined as a document
that helps us to provide the guidelines on how to write the Python code. It is
basically a set of rules that specify how to format Python code for maximum
readability. It was written by Guido van Rossum, Barry Warsaw and Nick Coghlan in
2001.
After stripping, all the whitespaces are removed, and now the string looks like the
below:
15) Why do we use join() function in Python?
The join() is defined as a string method which returns a string value. It is
concatenated with the elements of an iterable. It provides a flexible way to
concatenate the strings. See an example below.
Example:
1. str = "Rohan"
2. str2 = "ab"
3. # Calling function
4. str2 = str.join(str2)
5. # Displaying result
6. print(str2)
Output:
aRohanb
Output:
Original LIST1:
['Z', 'Y', 'X', 'W', 'V', 'U']
After the first shuffle of LIST1:
['V', 'U', 'W', 'X', 'Y', 'Z']
6
It is immutable. So updating tuple will lead to an error.
Example:
1. # Declaring tuple
2. tup = (2,4,6,8)
3. # Displaying value
4. print(tup)
5.
6. # Displaying Single value
7. print(tup[2])
8.
9. # Updating by assigning new value
10. tup[2]=22
11. # Displaying Single value
12. print(tup[2])
Output:
tup[2]=22
TypeError: 'tuple' object does not support item assignment
(2, 4, 6, 8)
Arithmetic operators perform basic arithmetic operations. For example "+" is used
to add and "?" is used for subtraction.
Example:
1. # Adding two values
2. print(12+23)
3. # Subtracting two values
4. print(12-23)
5. # Multiplying two values
6. print(12*23)
7. # Dividing two values
8. print(12/23)
Output:
35
-11
276
0.5217391304347826
Relational Operators are used to comparing the values. These operators test the
conditions and then returns a boolean value either True or False.
# Examples of Relational Operators
Example:
1. a, b = 10, 12
2. print(a==b) # False
3. print(a<b) # True
4. print(a<=b) # True
5. print(a!=b) # True
Output:
False
True
True
True
Assignment operators are used to assigning values to the variables. See the
examples below.
Example:
1. # Examples of Assignment operators
2. a=12
3. print(a) # 12
4. a += 2
5. print(a) # 14
6. a -= 2
7. print(a) # 12
8. a *=2
9. print(a) # 24
10. a **=2
11. print(a) # 576
Output:
12
14
12
24
576
Logical operators are used to performing logical operations like And, Or, and Not.
See the example below.
Example:
1. # Logical operator examples
2. a = True
3. b = False
4. print(a and b) # False
5. print(a or b) # True
6. print(not b) # True
Output:
False
True
True
Membership operators are used to checking whether an element is a member of
the sequence (list, dictionary, tuples) or not. Python uses two membership operators
in and not in operators to check element presence. See an example.
Example:
1. # Membership operators examples
2. list = [2,4,6,7,3,4]
3. print(5 in list) # False
4. cities = ("india","delhi")
5. print("tokyo" not in cities) #True
Output:
False
True
Identity Operators (is and is not) both are used to check two values or variable which
are located on the same part of the memory. Two variables that are equal does not
imply that they are identical. See the following examples.
Example:
1. # Identity operator example
2. a = 10
3. b = 12
4. print(a is b) # False
5. print(a is not b) # True
Output:
False
True
Bitwise Operators are used to performing operations over the bits. The binary
operators (&, |, OR) work on bits. See the example below.
Example:
1. # Identity operator example
2. a = 10
3. b = 12
4. print(a & b) # 8
5. print(a | b) # 14
6. print(a ^ b) # 6
7. print(~a) # -11
Output:
8
14
6
-11
27) What are the rules for a local and global variable in Python?
Global Variables:
o Variables declared outside a function or in global space are called global
variables.
o If a variable is ever assigned a new value inside the function, the variable is
implicitly local, and we need to declare it as 'global' explicitly. To make a
variable globally, we need to declare it by using global keyword.
o Global variables are accessible anywhere in the program, and any function can
access and modify its value.
Example:
1. A = "JavaTpoint"
2. def my_function():
3. print(A)
4. my_function()
Output:
JavaTpoint
Local Variables:
o Any variable declared inside a function is known as a local variable. This
variable is present in the local space and not in the global space.
o If a variable is assigned a new value anywhere within the function's body, it's
assumed to be a local.
o Local variables are accessible within local body only.
Example:
def my_function2():
K = "JavaTpoint Local"
1. print(K)
2. my_function2()
Output:
JavaTpoint Local
Example:
1. Q = "JavaTpoint, Python Interview Questions!"
2. print(Q[2:25])
Output:
vaTpoint, Python Interv
35) What is a negative index in Python and why are they used?
The sequences in Python are indexed and it consists of the positive as well as
negative numbers. The numbers that are positive uses '0' that is uses as first index
and '1' as the second index and the process go on like that.
The index for the negative number starts from '-1' that represents the last index in
the sequence and '-2' as the penultimate index and the sequence carries forward like
the positive number.
The negative index is used to remove any new-line spaces from the string and allow
the string to except the last character that is given as S[:-1]. The negative index is also
used to show the index to represent the string in correct order.
39) What are the differences between Python 2.x and Python
3.x?
Python 2.x is an older version of Python. Python 3.x is newer and latest version.
Python 2.x is legacy now. Python 3.x is the present and future of this language.
The most visible difference between Python2 and Python3 is in print statement
(function). In Python 2, it looks like print "Hello", and in Python 3, it is print ("Hello").
String in Python2 is ASCII implicitly, and in Python3 it is Unicode.
The xrange() method has removed from Python 3 version. A new keyword as is
introduced in Error handling.
41) What is the shortest method to open a text file and display
its content?
The shortest way to open a text file is by using "with" command in the following
manner:
Example:
1. with open("FILE NAME", "r") as fp:
2. fileData = fp.read()
3. # To print the contents of the file
4. print(fileData)
Output:
"The data of the file will be printed."
Output:
So, the data itself is not stored client side. This is good from a security perspective