Professional Documents
Culture Documents
Python (Interview Questions)
Python (Interview Questions)
Python (Interview Questions)
~Interview Questions~
1.What is Python?
Python was created by Guido van Rossum, and released in 1991.
domain.
dynamic semantics.
● Python allows a developer to write programs with fewer lines than some other
programming languages.
3.what is the difference between c and python
C is a general-purpose, procedural computer programming language.
● It is statically typed language
● C Program syntax is harder than Python.
● C has a limited number of built-in functions.
● C programs are compiled by the compiler
● C is generally used for hardware related applications.
2) String literals
3) Boolean literals
2. We cannot use keyword as variable names, function names and any other identifier.
● We use the predefined functions like int(), float(), str(), etc to perform explicit
type conversion.
Eg:x = 10,x=’python’,z=6.1
12.Input/Output Statements in python?
input():
Output():
This function is used to output the data or print the result on monitor
Eg: print(“Python”)
13. What are the Basic Data Types in python?
*Every value in Python has a datatype.
● Text Type: str
numbers
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
18.Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
Example:
19.Python Comparison Operators
Comparison operators are used to compare two values
Example:
20.Python Logical Operators
Logical operators are used to combine conditional statements
Example:
21.Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if
they are actually the same object, with the same memory location:
Example:
22.Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Example:
23.Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers
Examples:
24. Python Assignment Operators
Assignment operators are used to assign values to variables:
Example:
25.What is operator precedence in Python?
Operator precedence in Python simply refers to the order of operations. Operators are
used to perform operations on variables and values.
26.What is Associativity of Python Operators?
● When two operators have the same precedence, associativity helps to
determine which the order of operations.
● Associativity is the order in which an expression is evaluated that has
multiple operator of the same precedence.
● Almost all the operators have left-to-right associativity
Eg : print(5 * 2 // 3) #L to R O/P: 3
❏ if statement
❏ if else statements
❏ elif ladder
❏ Nested if statements
28.What are Repetition Statements(loops) in python?
1.for loop
2, while loop
29. What is the use of Break and continue in python?
● In Python, break and continue statements can alter the flow of a normal
loop.
● Loops iterate over a block of code until test expression is false, but
★ Python is one of the best programming and scripting languages among all.
★ The interpreter analyzes the entire program every time and halts the
Interpreter Compiler
● It takes a single line of code ★ It takes an entire program at a
time.
● Slower ★ Comparatively fast
● It requires less memory as it does ★ Memory requirement is more due to
not create intermediate object code. the creation of object code.
● Displays error of each line one by ★ Displays all errors after compilation
one ,all at the same time
● Error Detection is Easy ★ Error Detection is Difficuilt
● Python,Perl,PHP,Ruby,etc., ★ C,C++,Java,Scala,etc.,
37 Difference between mutable and immutable.
mutable immutable
★ Mutable means, wecan change the ★ Immutable means, we cannot
values of a variable, which we change the values of a variable,
have stored. which we have stored.
★ mutable :List,Set,Dictionary ★ Immutable:Tuple,Strings,
Note: Numbers
*Set is mutable, but every element
is unique and must be immutable
*Keys in dictionary are unique and
they are immutable
(strings,numbers,tuples)
38.Difference between lists and tuple
List Tuple
★ List is a collection of ordered ★ Tuple is collection of ordered elements
elements,surrounded by square surrounded by round brackets().
brackets[]. ★ It is immutable(unchangeable)
★ List is mutable(changeable) ★ Tuples are fixed in length,
★ List is dynamic. They grow and shrink heterogeneous, and can be nested.
on demand. ★ Tuple data type is appropriate for
★ The list is better for performing accessing the elements
operations, such as insertion and ★ Tuple consume less memory as
deletion. compared to the list
★ Lists consume more memory ★ Tuple does no have must built-in
★ Lists have several built-in methods methods.
39.what is list comprehension
➢ Comprehensions in Python provide us with a short and concise way to construct new
sequences (such as lists, set, dictionary etc.) using sequences which have been already
defined.
➢ List comprehension allows us to create a new list of elements that satisfy a condition
from an iterable. An iterable is any Python construct that can be looped over like lists,
strings, tuples, sets. In list comprehensions we use square brackets
Arrays: Lists:
**An array is a data structure that holds **The list is written as the list of
fix number of elements and these commas separated values inside the
elements should be of the same data square bracket.
type.
**There are two important parts of the **The elements in the list are not
array: compulsory of the same datatype.
1. Element: Each item stored in the
array is called an element
**It can be represented with positive
2. Index: Every element in the array
index or negative index.
has its own numerical value to identify
the element.
43.What are Built in libraries in python?
Built-in-functions:
The Python built-in functions are defined as the functions whose functionality
is pre-defined in Python.
Eg: input(),print(),range(),len(),type(),sort(),etc.,
46.Use of functions in python?
➔ Functions help break our program into smaller and modular chunks.
➔ As our program grows larger and larger, functions make it more organized and
manageable.
➔ The idea is to put some commonly or repeatedly done tasks together and make a
function so that instead of writing the same code again and again for different
inputs.
➔ Understanding programs becomes easier.
➔ Users can create their own functions and use them in various locations in the
main program.
47.How to define Function in Python?
1. Function definition starts with the keyword def
4. Parameters or arguments that the function accepts should be placed inside the
parentheses.
def my_function():
print("Hello from a function")
48. Arguments
1. Arguments are the values passed inside the parentheses of the function.
2. A function can have any number of arguments separated by a comma..
3. Arguments are specified after the function name, inside the parentheses.
49.Difference between Parameters & Arguments?
The terms parameter and argument can be used for the same thing:
information that are passed into a function.
1. Positional Arguments
2. Default Arguments
3. Keyword Arguments
4. Variable length Arguments
51. Positional Arguments?
Positional arguments are arguments that can be called by their position in the
function definition.
● First positional argument always needed to be listed first when the function is
called.
● Second positional argument always needed to be listed second when the
function is called and so on
52.Default Arguments in python?
➢Python allows function arguments to have default values.
➢ If the function is called without the argument, the argument gets its default value.
➢ We can provide a default value to an argument by using the assignment operator (=).
➢ Default arguments allow a function call to pass less parameters than the number of
parameters in the function definition. (no.of parameters in function call and function
header need not be same)
53.Keyword arguments
Example:
54.Variable-length Arguments in python?
1. In some cases we cannot exactly tell how many parameters are needed in a
function definition. In such cases we can use variable length arguments.
➢ Python does not allow programmers to create multiple identifiers with same name.
Note: When we import both modules into same program and try
to refer sample(), error will be generated:
To avoid the error, we have to use fully qualified name as follows:
hiding, etc into programming. It also allows binding data and code
together.
61. Why we use OOPs?
★ Inheritance
★ Encapsulation
★ Polymorphism
★ Data Abstraction
65. Difference between Procedural programming and OOPs?
Procedural Programming Object-oriented programming
-It is based on functions. -It is based on real-world objects.
-It shows the data to the entire program. -It encapsulates the data.
-It does not have scope for code reuse. -It provides more scope of code reuse.
-It follows a the concept of top-down -It follows a bottom-up programming
programming. paradigm.
-Nature of the language is complicated. -It is less complicated in nature, so it is
easier to modify, extend and maintain
-It is hard to modify, extend and
maintain the code
66. What is inheritance?
★ Single inheritance
★ Multiple inheritance
★ Multilevel inheritance
★ Hierarchical inheritance
★ Hybrid inheritance
68.What is polymorphism?
➔ Polymorphism contains two words "poly" and "morphs". Poly means many, and
morph means shape.
➔ By polymorphism, we understand that one task can be performed in different
ways.
➔ For example - you have a class animal, and all animals speak. But they speak
differently. Here, the "speak" behavior is polymorphic in a sense and depends on
the animal.
➔ So, the abstract "animal" concept does not actually "speak", but specific animals
(like dogs and cats) have a concrete implementation of the action "speak".
69.What is Encapsulation?
★ Encapsulation is also an essential aspect of object-oriented
programming.
★ It is used to restrict access to methods and variables.
★ In encapsulation, code and data are wrapped together within a
single unit from being modified by accident.
70.What is Data Abstraction?