0% found this document useful (0 votes)
4 views12 pages

Q11

The document explains the programming cycle for Python, highlighting its rapid turnaround due to the absence of compile or link steps, allowing immediate execution after changes. It also covers Python's interpretation process, static analysis tools like Pychecker and Pylint, and the concept of decorators. Additionally, it discusses data types in Python, including numeric and string types, as well as memory management, PEP 8 guidelines, and Boolean expressions.

Uploaded by

Narottam Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Q11

The document explains the programming cycle for Python, highlighting its rapid turnaround due to the absence of compile or link steps, allowing immediate execution after changes. It also covers Python's interpretation process, static analysis tools like Pychecker and Pylint, and the concept of decorators. Additionally, it discusses data types in Python, including numeric and string types, as well as memory management, PEP 8 guidelines, and Boolean expressions.

Uploaded by

Narottam Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Q1. Explain the programming cycle for Python.

Ans.

1. Python’s programming cycle is dramatically shorter than that of traditional programming


cycle.

2. In Python, there are no compile or link steps.

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.

5. Fig. 1 shows Python’s impact on the programming cycle.

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.

Python: Python is a high-level, interpreted, interactive and object-oriented scripting language. It is a


highly readable language. Unlike other programming languages, Python provides an interactive mode
similar to that of a calculator.

Interpretation of Python :

 1. An interpreter is a kind of program that executes other programs.

 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.

 10. It is the last step of the Python interpreter.

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.

 Python has six basic data types which are as follows:

 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

 Single quotes or double quotes are used to represent strings.


 A string in Python can be a series or a sequence of alphabets, numerals and special
characters.

 For Example

Q4. Discuss list and tuple data types in detail.

Ans.

List:

1. A list can contain the same type of items.

2. Alternatively, a list can also contain different types of items.

3. A list is an ordered and indexable sequence.

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:

1. A tuple is also used to store sequence of items.

2. Like a list, a tuple consists of items separated by commas.

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.

Programming cycle for Python :

 Python’s programming cycle is dramatically shorter than that of traditional programming


cycle.

 In Python, there are no compile or link steps.

 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.

 Fig. 1 shows Python’s impact on the programming cycle.

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.

 Python has six basic data types which are as follows:

 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

 Single quotes or double quotes are used to represent strings.

 A string in Python can be a series or a sequence of alphabets, numerals and special


characters.
 For Example

List:

6. A list can contain the same type of items.

7. Alternatively, a list can also contain different types of items.

8. A list is an ordered and indexable sequence.

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:

4. A tuple is also used to store sequence of items.

5. Like a list, a tuple consists of items separated by commas.

6. Tuples are enclosed within parentheses rather than within square brackets.
Dictionary:

1. A Python dictionary is an unordered collection of key-value pairs.

2. When we have the large amount of data, the dictionary data type is used.

3. Keys and values can be of any type in a dictionary.

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 :

 1. In a programming language, mostly data is stored in the form of alphanumeric but


sometimes we need to store the data in the form of ‘Yes’ or ‘No’.

 2. In terms of programming language, Yes is similar to True and No is similar to False.

 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.

Type conversion in Python :

 1. The process of converting one data type into another data type is known as type
conversion.

 2. There are mainly two types of type conversion methods in Python :

a. Implicit 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.

b. Explicit type conversion:

 Explicit type conversion is also known as type casting.

 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.

Ans. Memory management :

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:

1. A PEP is a design document providing information to the Python community, or describing a


new feature for Python or its processes or environment.

2. The PEP should provide a concise technical specification of the feature.

3. PEP is actually an acronym that stands for Python Enhancement Proposal.

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.

5. A PEP is a design document providing information to the Python community, or describing a


new feature for Python or its processes or environment.

You might also like