Q11
Q11
Ans.
3. Python programs simply import modules at runtime and use the objects they
contain.Because of this, Python programs run immediately after changes are made.
4. In cases where dynamic module reloading can be used, it is even possible to change and
reload parts of a running program without stopping it at all.
6. Since Python is interpreted, there is a rapid turnaround after program changes. And because
Python’s parser is embedded in Python-based systems, it is easy to modify programs at
runtime.
Q2. What is Python? How is Python interpreted? What are the tools that help to find bugs or
perform static analysis? What Are Python decorators?
Ans.
Interpretation of Python :
2. When we write Python programs, it converts source code written by the developer into an
intermediate language which is again translated into the machine language that is executed.
3. The python code we write is compiled into python bytecode, which creates a file with the
extension .pyc.
4. The bytecode compilation happened internally and was almost completely hidden from
the developer.
5. Compilation is simply a translation step, and byte code is a lower-level, and platform-
independent, representation of source code.
6. Each of the source statements is translated into a group of bytecode instructions. This
bytecode translation is performed to speed execution. Bytecode can be run much quicker
than the original source code
statements.
7. The .pyc file, created in the compilation step, is then executed by appropriate virtual
machines.
8. The Virtual Machine iterates through bytecode instructions, one by one, to carry out their
operations.
9. The Virtual Machine is the runtime engine of Python and it is always present as part of the
Python system, and is the component that actually runs the Python scripts.
The following tools are the static analysis tools that help to find bugs in python :
1.Pychecker: Pychecker is an open source tool for static analysis that detects the bugs from source
code and warns about the style and complexity of the bug.
2. Pylint:
a.Pylint is highly configurable and it acts like special programs to control warnings and errors,
it is an extensive configuration file
b. It is an open source tool for static code analysis and it looks for programming errors and is
used for coding standard.
c. It also integrates with Python IDEs such as Pycharm, Spyder, Eclipse, and Jupyter.
Python decorators:
1. Decorators are very powerful and useful tool in Python since it allows programmers to
modify the behavior of function or class.
2. Decorators allow us to wrap another function in order to extend the behavior of wrapped
function, without permanently modifying it.
3. In decorators, functions are taken as the argument into another function and then called
inside the wrapper function.
4. Syntax :
@gfg_decorator
def hello_decorator():
print(“Gfg”)
5. gfg_decorator is a callable function, will add some code on the top of some another callable
function, hello_decorator function and return the wrapper function.
Q3. What do you mean by data types? Explain numeric and string data type with examples.
Ans.
Data types:
The data stored in the memory can be of many types. For example, a person’s name is stored
as an alphabetic value and his address is stored as an alphanumeric value.
Numeric
String
List
Tuple
Dictionary
Boolean
Numeric
Numeric data can be broadly divided into integers and real numbers (i.e., fractional
numbers). Integers can be positive or negative.
The real numbers or fractional numbers are called, floating point numbers in
programming languages. Such floating point numbers contain a decimal and a
fractional part.
For example:
String
For Example
Ans.
List:
4. To declare a list in Python, we need to separate the items using commas and enclose them
within square brackets ([ ]).
5. Operations such as concatenation, repetition and sub-list are done on list using plus (+),
asterisk (*) and slicing (:) operator.
Tuple:
3. Tuples are enclosed within parentheses rather than within square brackets.
Q5. What do you mean by Boolean expression?
OR
Write short notes with example: The programming cycle for Python, elements of Python, type
conversion in Python, operator precedence, and Boolean expression.
Ans.
Python programs simply import modules at runtime and use the objects they
contain.Because of this, Python programs run immediately after changes are made.
In cases where dynamic module reloading can be used, it is even possible to change and
reload parts of a running program without stopping it at all.
Since Python is interpreted, there is a rapid turnaround after program changes. And because
Python’s parser is embedded in Python-based systems, it is easy to modify programs at runtime.
Elements of Python :
Data types:
The data stored in the memory can be of many types. For example, a person’s name is stored
as an alphabetic value and his address is stored as an alphanumeric value.
Numeric
String
List
Tuple
Dictionary
Boolean
Numeric
Numeric data can be broadly divided into integers and real numbers (i.e., fractional
numbers). Integers can be positive or negative.
The real numbers or fractional numbers are called, floating point numbers in
programming languages. Such floating point numbers contain a decimal and a
fractional part.
For example:
String
List:
9. To declare a list in Python, we need to separate the items using commas and enclose them
within square brackets ([ ]).
10. Operations such as concatenation, repetition and sub-list are done on list using plus (+),
asterisk (*) and slicing (:) operator.
Tuple:
6. Tuples are enclosed within parentheses rather than within square brackets.
Dictionary:
2. When we have the large amount of data, the dictionary data type is used.
4. Items in dictionary are enclosed in the curly-braces () and separated by the comma (,).
5. A colon (:) is used to separate key from value. A key inside the square bracket [ ] is used for
accessing the dictionary items.
6. For example:
Boolean :
3. This True and False data is known as Boolean data and the data types which stores this
Boolean data are known as Boolean data types.
1. The process of converting one data type into another data type is known as type
conversion.
When the data type conversion takes place during compilation or during the run time, then it
called an implicit data type conversion.
Python handles the implicit data type conversion, so we do not have to explicitly convert the
data type into another data type
In the given example, we have taken two variables of integer and float data types and added
them.
Further, we have declared another variable named ‘sum’ and stored the result of the
addition in it.
When we checked the data type of the sum variable, we can see that the data type of the
sum variable has been automatically converted into the float data type by the Python
compiler. This is called implicit type conversion.
Explicit type conversion takes place when the programmer clearly and explicitly defines the
variables in the program.
For example:
In the given example, the variable a is of the number data type and variable b is of the string
data type.
Operator precedence:
1. When an expression has two or more operator, we need to identify the correct sequence
to evaluate these operators. This is because result of the expression changes depending on
the precedence.
For example : Consider a mathematical expression: 10+ 5/5
When the given expression is evaluated left to right, the final answer becomes 3.
2. However, if the expression is evaluated right to left, the final answer becomes 11. This
shows that changing the sequence in which the operators are evaluated in the given
expression also changes the solution.
3. Precedence is the condition that specifies the importance of each operator relative to the
other.
Boolean expression: A boolean expression may have only one of two values: True or False.
For example: In the given example comparison operator (==) is used which compares two operands
and prints true if they are equal otherwise print false :
>>> 5 == 5
True #Output
>>> 5 == 6
False #Output
Q5. How memory is managed in Python? Explain PEP 8. Write a Python program to print even
length words in a string.
1. Memory management in Python involves a private heap containing all Python objects and
data structures.
2. The management of this private heap is ensured internally by the Python memory manager.
3. The Python memory manager has different components which deal with various dynamic
storage management aspects, like sharing, segmentation, preallocation or caching.
4. At the lowest level, a raw memory allocator ensures that there is enough room in the private
heap for storing all Python-related data by interacting with the memory manager of the
operating system.
5. On top of the raw memory allocator, several object-specific allocators operate on the same
heap and implement distinct memory management policies adapted to the peculiarities of
every object type.
6. For example, integer objects are managed differently within the heap than strings, tuples or
dictionaries because integers imply different storage requirements and speed/space
tradeoffs.
7. Python memory manager thus delegates some of the work to the object-specific allocators,
but ensures that the latter operate within the bounds of the private heap.
PEP 8:
4. PEP 8 is Python’s style guide. It is a set of rules for how to format the Python code to
maximize its readability.