Python programming language has become popular with desktop application development. It has become a must skill set on most of the companies’ requirements in a developer. Python programming language has become popular with machine learning community and is a basic need skill for data scientist/machine-learning developer/deep-learning developer. This article helps you through most of the upto date possible Python interview questions; and covers basic, intermediate and advanced Python interview questions. Example programs and references are provided whenever possible.
- Entry Level – Python Interview Question
- Medium Level – Python Interview Question
- Advanced Level – Python Interview Question
Python Interview Questions Entry Level
What kind of language is Python
Python is :
- Interpreted (most of its implementations execute instructions directly, without previously compiling a program into machine level instructions)
- High-level programming language (provides strong abstraction from internal details of computer; and is human readable)
- General-purpose programming language (the language could be used to write wide variety of applications)
Why is Python called scripting language
Python supports scripts: programs written for a Python run-time environment that automate the execution of tasks. These tasks could alternatively be executed one-by-one by a user.
What are the benefits of Python
There are many benefits in using Python language. Some of them are :
- Syntax of Python is easy to learn.
- Python is a high-level programming language. So, it provides a good human readability.
- Python is cross-platform. Scripts written on an operating system could be run on another with ease.
- Python has an active community. Many resources and help is available online.
- Python is extensible. Large variety of third-party modules are available.
- Python is free. Python programming language can be used for projects without any cost.
Give a simple example for Python script
A simple example where a variable is initialized and printed to the console.
i = 10 print(i)
How to execute a Python Script
If python is installed on the machine, command prompt or shell could be used for executing python scripts with python command.
python fileName.py
What is Python Shell
Python Shell is an interactive command line interface. Python statements can be executed using Python Shell.
How do you start Python shell
python command will start python shell.
$ python Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
What is the command to get Python Version
Python Version is displayed when Python Shell is started. Otherwise, without starting Python Shell, python –version command can be used.
$ python --version Python 3.5.2
Which coding convention is preferred for Python
PEP-8[https://www.python.org/dev/peps/pep-0008/] is the style guide recommended by Python Organization.
What are the datatypes available in Python
Following are the in-built datatypes in Python programming :
- Boolean
- Numeric
- int
- float
- long
- complex
- Sequence
- String
- List
- Tuple
- Set
- Mapping
- Dictionary
Which conditional statements are available in Python
Conditional statements are the programming statements which alter the course of execution based on a condition.
Like other functional programming languages, Python has following conditional statements.
Conditional Statements are also called Control Statements.
Which looping statements are available in Python
Looping statements are those which repeat the execution of a set of statements in a cyclic manner based on a condition.
What is the syntax for Python If statement
Python If statement contains if keyword, condition followed by colon symbol (:), and then the statements of if block with indentation.
if condition: statement1 statement2 statementN
Condition should evaluate to a boolean value (true or false).
What is the syntax for Python For loop
for variable in sequence: statement1 statement2 statementN
- for is keyword
- sequence can be a list, string, typle or rance
- for ith iteration, the ith element of sequence is loaded to variable
- statements has to written with indentation
Give an example of Python Tuple
Tuples are used to store values of different datatypes.
tuple1 = ('Sarath', 'Varma', 1997, 'Kochi') tuple2 = (1, 2, 3, 4, 5 )
Elements are separated by comma and are enclosed in parenthesis.
How do you access elements of Tuple
Like in an Array, elements of Tuple can be accessed using index. Index of first element is 0, and index of last element in the tuple is (length of tuple – 1).
How are comments written in Python code
Single Line Comments start with # followed by comment content.
Multiple Line Comments are enclosed between triple single quotes or triple double quotes.
# single line commenet '''Mulitpl Line Commnet''' """Another Multiple line comment"""
Comments improve the readability of a program.
Give an example of Python Class
Following is an example of Python Class.
class Student: 'Common base class for all students' def __init__(self, name, rollNumber): self.name = name self.rollNumber = rollNumber def displayStudent(self): print "Name : ", self.name, ", Roll Number: ", self.rollNumber
The Student class contains a constructor for setting the initial state and a method to display student information.
Python 2x or 3x Which one would you choose and why
Python 2.x is legacy, Python 3.x is the present and future of the language.
How do you declare a variable in Python
How do you convert float, int, complex value, tuple, list or dictionary to a string ? str() function can be used to convert other data type value to string. str() accepts float, int, complex value, tuple, list or dictionary as an argument and returns string value.
What are the mutable builtin datatypes available
Mutable datatype are those
- List
- Sets
- Dictionaries
What are the immutable builtin datatypes available
Immutable datatypes are those
- Strings
- Tuples
- Numbers
How to find substring of a string
str[start_index:end_index] returns substring of a string, where
- str is the main string
- start_index is position of substring from which it starts in main string. If not provided, default value is 0.
- end_index is the ending index of substring in the main string. If not provided, default value is length of the main string.
How to you initialize an array
To initialize an array in Python use assignment operator (=) with values separated by comma and enclosed between square brackets.
arr1 = [10, 20, 30, 40, 50]
Elements of the array can be accessed using index.
arr1[0]; arr1[2]; arr1[1:]
Give an example to iterate through all the elements in an array
For loop can be used to iterate through each element of array.
arr1 = [10, 20, 30, 40, 50] for i in arr1: print(i);
How to find size of a Python Array
len() function can be used to find size of Python array, given the dimension.
arr1 = [{10, 20, 30, 40, 50},{10, 20, 30, 40, 50}] print(len(arr1)) print(len(arr1[0]))
What is Frozen Set in Python
Frozen set is an immutable version of a Python Set object.