Python Interview Qs Flashcards
What is Python?
high-level, interpreted, strongly and dynamically typed general purpose programming language
What are the benefits of Python?
Simply, easy to read syntax - reduces the cost of program maintenance.
It is both a scripting and programming language
It’s open source and supports third-party packages
What is a dynamically typed language?
Data types are checked during execution. Python is a strong dynamically typed language - there is no implicit conversion of data types. Python executes each statement line by line and thus type-checking is done on the fly, during execution.
What is an interpreted language?
Executes statements line by line - Python, JS, R, PHP, Ruby. Program runs directly from source code with no intermediary compilation step.
What is PEP 8 and why is it important?
Official style guidelines for Python Code. Important in open-source
What is Scope in Python?
Every object functions within a scope - block of code where an object is relevant. Defines where objects can be used and in what ways.
Local scope, global scope, module-level scope, outermost scope
Local scope
local objects available in the current function. Local scope objects can be synced with global scope objects with keyword global
Global scope
objects available throughout the code execution since their inception
module-level scope
global objects of the current module accessible in the program
outermost scope
all the built-in names callable in the program. Objects in this scope are searched last to find the name reference
Difference between lists and tuples
Both sequence data types that can store a collection of objects in Python.
Lists are mutable. Tuples are not.
Common built-in data types
None type
Numeric types: int, float, complex (stores complex numbers in form (A + Bj) and has attr real and imag), bool
Sequence types: list, tuple, range, str, bytearray, bytes, memoryview
Mapping types: dict
Set types: set, frozenset
What is pass in Python?
null operation used as a placeholder for empty code blocks
Benefits of using modules
Simplicity, Maintainability, Reusability, Scoping
What are protected attributes?
Defined with single underscore prefix. Can be accessed and modified from outside the class they are defined in but a responsible developer should refrain from doing so
What are private attributes?
double underscore prefix. Cannot be accessed or modified from outside and will result in AttributeError if attempt is made
How can you make a Python Script executable on Unix?
script file must begin with
#!/usr/bin/env/python
Difference between arrays and lists
Arrays can only contain elements of same data type vs lists can contain difference data types (disadvantage of using more memory)
How is memory managed in Python?
Handled by the Python Memory Manager. Memory is allocated by the manager in the form of a private heap space dedicated to Python. Python objects are stored in heap and since its private, it’s inaccessible to the programmer.
Python has built-in Garbage Collection to recycle the unused memory for the private heap space.