Introduction to Python:
Python is one of the most used programming languages and is interpreted in the nature thereby providing the flexibility of incorporating dynamic semantics. Python is a free and open-source language with very clean and simple syntax. This makes it very easy for developers to learn python. Python supports object-oriented programming and is most widely used to perform general-purpose programming.
Here is the list of most commonly asked python interview question and answers:
Python Interview Questions for beginners
1. Describe Python? What are the advantages of using Python ?
- Python is general-purpose programming language which is a simple, easy-to-learn syntax , emphasizes readability and reduces the cost of program maintenance. The language is also capable of scripting, completely open-source, and supports all third-party packages encouraging modularities and code reuse.
- Python is high-level data structures, combined with the dynamic typing and dynamic binding, attracting a huge community of developers for the Rapid Application Development and deployment.
2. What is meant by dynamically typed language?
- Static – static Data Types are checked before execution.
- Dynamic -dynamic Data Types are checked during execution.
Python is interpreted language, executes at each statement line and type-checking is done during execution. Hence, Python is called as Dynamically Typed Language.
3. Describe an Interpreted language?
4. Explain PEP 8 and why is it considered important?
5. Scope in the Python?
- A local scope refers to local objects available in current function.
- A global scope refers to objects available throughout code execution since their inception.
- A module-level scope refers to global objects of current module accessible in program.
- An outermost scope refers to all built-in names callable in program. The objects used in this scope are searched at last to find the name referenced.
6. What are the lists and tuples? What is key difference between the two terms?
['sara', 6, 10.19], while tuples represented with parantheses
('ansh', 5, 0897).
But the real difference between the two?The key difference between the two is while lists are mutable, tuples are immutable objects. This means that the lists can be modified, appended or sliced on go but tuples remain constantly and cannot be modified in any manner. You can run following example on Python IDLE to confirm the generated difference:
my_tuple = ('sarada', 6, 4, 0.97) my_list = ['sarada', 6, 4, 0.97] print(my_tuple) # output => 'sarada' print(my_list) # output => 'sarada' my_tuple = 'anshu' # modifying tuple => throws an error my_list = 'anshu' # modifying list => list modified print(my_tuple) # output => 'sarada print(my_list) # output => 'anshu'
7. What are common built-in data types used in Python?
isinstance() and type()functions to check type of these variables. These type of data can be grouped into following categories-
- None Type:
Nonekeyword which represents the null values inthe Python. Boolean equality operation can also be performed using this None Type objects.
|NoneType||Represents NULL values in Python.|
- Numeric Types:
These are three different numeric types – integers, numbers,floating-point and complex numbers. Additionally, booleans are sub-type of integers.
|int||Stores integer are literals including octal,hex and binary num as integers|
|float||Stores literals which containing decimal values and exponent signs as floating-point no’s|
|complex||Stores complex numbers in form (A + Bj) and has attributes: imag and real|
|bool||Store boolean value (True / False).|
- According to the Python Docs, there are three most basic Sequence Types – tuples, lists,and range objects. Sequence types consist the
not inoperators defined for their traversing elements. These operators shares same priority as the comparison operations.
|list||Mutable sequence used to store the collection of items.|
|tuple||Immutable sequence used to store the collection of items.|
|range||Represents an immutable sequence of the numbers generated during execution.|
|str||Immutable sequence ofthe Unicode code points to store textual data.|
The standard library also includes additional types of processing:
1. Binary data and
2. Text string
- Mapping Types:
Mapping object can be map hashable values to the random objects in the Python. Mapping objects are mutable and also there is currently one and only standard mapping type, the dictionary.
|dict||Stores are comma-separated list of key: value pairs|
- Set Types:
Python has 2 built-in set types – set and frozenset. set type mutable and supports methods like
remove(). frozenset type immutable and can’t be modified after creating.
set is mutable and thus cannot be used as key for a dictionary. On the other hand,
frozenset is immutable and thus, hashable, and can be used as a dictionary key or as an element of another set.
Module is an additional built-in type supported by the Python Interpreter. It supports one special operation, i.e., attribute access:
mymodis a module and myobj references a name defined in m’s symbol table. The module’s symbol table resides in a very special attribute of the module __dict__, but direct assignment to this module is neither possible nor recommended.
- Callable Types:
Callable types are the types to which function call can be applied. They can be user-defined functions, instance methods, generator functions, and some other built-in functions, methods and classes.
Refer to the documentation at docs.python.org for a detailed view of the callable types.
8. Describe pass in Python?
passkeyword is represents a null operation in Python. which is generally used for purpose of filling up empty blocks of code which may be execute during runtime but has to be written. Without pass statement in following code, we may run into some type of errors during code execution.
def myEmptyFunc(): # do nothing pass myEmptyFunc() # nothing happens ## Without the pass keywords # File "<stdin>", line 2 # IndentationError: expected an indented blocks
9. Explain modules and packages in the Python?
- Simplicity: Working on single module helps you to focus on relatively small portion of problem at hand. This makes the development easier and less errors.
- Maintainability: Modules are designed to enforce the logical boundaries between the different problem domains. If they are written in manner that reduces the interdependency, it is always less likely that modifications in module might impact other parts of program.
- Reusability: Functions defined in module can be easily reused by other parts of application.
- Scoping: Modules typically defines a separate namespace, which can help to avoid confusion between identifiers from other parts of program.
In general MODULES , are simple Python files with a extension .py and can have set of classes,functions, or variables defined and implemented. They can be imported and initialized once by using the
import statement. If partial functionality needed, import the requisite classes and functions using
from foo import bar.
Packages allow the hierarchial structuring of the module namespace using the dot notation. As, modules help to avoid clashes between global variable names, in similar manner, packages helps to avoid clashes between the module names.
Creating a package is easy since it makes the use of system’s inherent file structure. So just stuff modules into a folder and there you will have it, the folder name as the name of package . Importing a module or its contents to this package requires the package name as the prefix to the module name joined by a dot.
10.Explain global, protected and private attributes in Python?
- Global variables are the public variables that are defined in global scope. To use this variables in the global scope inside a function, we must use the
- Protected attributes are the attributes defined with an underscore pre-fixed to their identifier like _sara. They can still be accessed and modified from outside class they are defined in .A responsible developer should refrain from doing so.
- Private attributes are the attributes with double underscore prefixed to their identifier like __ansh. They cannot be accessed or modified from outside directly and also will result in AttributeError if such an attempt made.
If you are looking to get certified in Python? Check out the Scaler Topic’s course with certification.