Question Bank With Answers For Unit1
Question Bank With Answers For Unit1
a)Features of python
ANS
a)"Features of python. There are various reasons why python is having popularity programming
languages when compared with others. The following are some of important features of python.
En: >>>a=10
10
>>>type (a)
<class lint'>
>>> b=10
10
<class 'int'>
>>>a= 10.1
>>>print (a)
10.1
>>>type (a)
<class 'float'>
>>>
b)Flavours of python:
→ Flavours of python refers to different datatypes of python compilers. The following are some of them.
1. C-python: This is a standard python compiler implemented in c language. This python software
being downloaded directly from https://www.cPython.org. In this python program, initially
converted into byte code using c language. The advantage is that it is able to execute both C
programs &'C++' programs
2. Jython: This is early known as J-Python This is implementation of python program language
which is designed to run on java platform. This brytecode is executed by java virtual machine.
This can be downloaded from https://www.jpython.org'
3. Iron python : This is another implimentation, python program for 'dotnet'frame work. This is
written in C sharp (C#) language. The python program when compiled gives an intermediate
language which runs on "Common language runtime” (CLR) to produce the output. Iron
python can be downloaded from https://www.Ironpython.net.
4. Py Py: This is python implementation using python language. actually PyPy is written in a
language called 'rpython' which is created in python language. RPython is suitable for creating
language interpreter PYPY can be downloaded by. visiting The page https://www.PyPy.org
5. Ruby Python: This is a bridge between ruby and python interpreter. It encloses python
interpreter inside ruby application. This can be downloaded from 'https://rubygems
org/gems/ruby Python/version /0.6.3.
6. Stackless python : Small task which should run individually are called task- -lets. Tasklets run
independently on CPU and can communicate with other via channels.
A thread is a process which can run 100's of such tasklets. These threads and tasklets
can be created in stacklese python without implementing stack .This can be downloaded
from "https://PyPi-Python.org/pypi/stackless Python/10.0.
7. Pythonxy: This is written as Python (x,y) which is implemented after adding scientific and
engineering related packages.This can be downloaded from
'https:// Pythonxy.github.io/downloads html’.
8. Anaconda Python:When python is redeveloped for handling large scale data processing
predictive analysis and scientific computation. It is called anaconda python. In this we can
develope python programs & R Programming language.
a=2
b=6
C = 2*6
print ("Result", c)
we can type this program in text editor like Notepad and save program as first. py.It means
first.py file contains source code.Now compile the source code and the result is 12.
Now we want to see bytecode instructions that were created internally by the python compiler.
To view the byte code, follow the procedure.
C:/>python-m dis first.py
we are specifying 'dis' module which is used to display the list of programs.The output is
generated as below.
2 0 LOAD_CONST 0(10)
3 DUP_TOP
4 STORE_NAME 0(a)
7 STORE_NAME 1(b)
3 10 LOD_NAME 2(print)
13 LOAD_CONST 1('sum=')
16 LOAD_NAME 0(a)
19 LOAD_NAME 1(b)
22 BINARY_ADD
The preceding byte code is displayed by 'dis' module which is known as 'dis assembler'
The LOAD_NAME specifies byte code instructions as 2 arguments.
The LOAD_Constant represent the string constant Binary-add instruction adds the previous
values.
Gudio Van Rossum - Dutch scientist introduced Python in maths-cs in 1990 but public started
using in Feb 20 1991
Python is the programming language that combines the features of "c' and `java'.
Python was developed by Gudio Van Rossum at centre for mathematics and computer science in
1991 february 20
Van Rossum picked the name "Python" from a TV show 'monty Python's flying circus.
The logo of python shows two intertwisted stakes.
Execution:
Lets assume we write a python program with a name first.py. Here first is known "source code"
using interpreter and `'.py' is the extension.
Every python program is typed with extension '.Py'.
The compiler (interpreter) converts the python program into byte code.
Byte code represents a fixed set of instructions that represents all operation like arithmetic
operations, comparision operation, memory related operations etc which can run on any
operating system.
The size of byte code instruction is one byte, and hence they are called with the name "byte
code”.
These byte code instructions are contained in file “first.pyc".
The next step is to run the program, if we give the byte code to the computer,it cannot run, it
cannot execute.
Any computer can execute only binary code which have 0's & 1's. It is also known as machine
code.If it is necessary to convert the byte code into machine code, for this we use PVM.
PVM uses an interpreter which understands the byte code and convert it into machine code.These
machine code instructions are executed by processor and results are displayed.
An interpreter translates the source code line by line. The speed of interpreter is slow when
compared to compiler. To resolve this, PVM is added with JIT (JUST IN TIME) compiler.
we compile python program, we cannot see '.pyc' files. This is done internally in the memory and
output is given. The reason is all the steps in the sequence source code → bytecode →
executable→output code are internally compiled and then final result is displayed.
To separately create '.pyc' files,folow the procedure.
Open command prompt, type python import compile py-compile.compile ("Program
name.py")
In this command we are calling Python compiler which is going to generate byte code or '.pyc".
Example program
a=10
b=20
c=a+b
print(“sum is “,c)
3. Define
a) PVM
b) Comments and list out different types of comments with syntax and program
Computer can understand 0's & 1's which is known as machine code.
We should convert into machine code before it is submitted to execution. For this we should use
compiler.
A compiler normally converts programs source code into machine code.
Each Python program, statement is converted into bytecode instructions.
The size of each byte code is 1' byte. So it is known as bytecode instruction.We can find byte
code instruction ".pyc' shows the role of virtual machine converting bytecode instruction into
machine code.
Python virtual machine is to convert byte code instruction into machine code so that computer
can execute machine code instructions & display final output.
The interrupter converts byte code into machine Code & sends machine code to computer
processor for execution.
PVM is also called as interpreter.
b)Comments:
>>>#b=20
>>> print(b)
>>>Print (a)
10
Here the first line is starting with# and hence the entire line is treated as comment.
In 2nd line a=10 is a statement. After this statement #symbol starts the comment describing
that the value is stored in variable 'a'. The part of this line from where # is started treated as
comment.
Multiline comments:
1. When to make several lines as comments by using #instead we can use multiline comments.
>>>#this program
>>># is for
>>>#comments.
The previous code can be written in triple double quotes or triple single quotes. in the
beginning & ending of the block.
>>>"""This is
. . . multiline
. . . comment"""
>>>'''This is
. . . multiline
. . . Comment'''
>>>
The triple double quotes or triple single quotes are called multiline comments or block
comments. They are used to enclose a block of line as comment.
(Q)4. Explain about built in data types (None,numeric,Boolean) with suitable examples
ANS:
None
Datatypes -----------
Numeric
boolean
A datatype represents type of data stored into variable or memory. The datatypes which are
already in python are known as builtin datatypes.
Builtin datatypes:
There are 5 builtin datatypes type none, numeric, sequence, set, mapping
1. None:
In python the none datatype represents an object that doesn't contain any value.
In other languages, it is called as null object. In python program maximum of only 1 none
object is provided, when calling the function if no value is passed then the default value will
be taken as none where some value is passed to the function.
Ex:>>>x=None
>>> print(x)
None
>>> type (x)
>>>
2. Numeric type: The numeric type represents numbers there are 3 subtypes.
1. int:
int represents integer numbers.
An integer number is without decimals or fractions.
>>>a=-57
-57
>>>type (a)
<clays 'int'>
Here 'a' is called variable since it is storing -57 which is integer type. In python there is no limit
for size of integers.
2. Float: The float datatype represents the floating point number, a float number is a number that
contains decimal values.
>>>a=5.6923
5.6923
<class 'float'>
>>> b= 2.4e4
24000.0
>>>type (b)
<class 'float'>
here 'a' is called float type variable and it is storing float values.
floating point numbers can also be written in e or E to represent the power of 10.Here e or E
represents exponential.
Here the float value 'b' represents
Example
b=2.4e4
>>> print(b)
24000.0
>>> type(b)
<class 'float'>
3. Complex numbers: A complex number is written in the form of a+bj or a-bj. Here 'a' represents
real part 'b'represents imaginary part. "j"' represents square root value of -1
>>> C1 = 2·5+2·5j;
>>> C2 = 3.5+3.5j
>>> (6+6j)
>>>type (C1)
<class 'Complex'>
>>>n=0b011o.
>>>print (n1)
>>type (n1)
<class 'int'>
450
>>>type (n3)
<Class int'>
>>>Ex:>>>n2=0o15
13
<class 'int'>
3. Bool datatype
The bool datatype in Python represents boolean values.
There are only two boolean values True or False that can be represented by this datatype. Python
internally is also represented as False. represents True as 1 and False as 0.
A blank string like “ “ is also represented as False.
Conditions will be evaluated internally to either True or False. For example,
a = 10
b = 20
In the previous code, the condition a<b which is written after if - is evaluated to True and hence
it will execute print("Hello").
a = 10>5 # here 'a' is treated as bool type variable
a = 5>10
(Q)5.Discuss the differences between str, bytes, and bytearray data types in Python with example
program
ANS.
Sequences in Python
We can also write strings inside “””(triple double quotes) or ‘’’(triple double quotes)
We can modify or edit the elements of the bytearray. For example we can write
x[0] = 88
x[1] = 99
We will write program to create a bytearray type array and then modify the first two elements. Then we
will display all the elements using a for loop. This can be seen in Program .
x=bytearray(elements)
x[0]=88
x[1]=99
For i in x : print(i)
Output
88
99
40
15
6. Define list and show how the list is mutable with different programs
ANS.List
1.Lists in Python can be created by placing the sequence inside the square brackets[].
2. Checking type
Syntax
If the start is not mention it is assumed to be ‘0’ and stepsize is taken as 1,stop is one
element prior to ‘stop’.
Program :
9. Updating list
Lists are mutable.It means we can modify the contents of list,append the list,update or
delete the elements in list
Program: #appending the list is adding elements to list at the end
10. Slicing in list : selecting from any index number to any index number
11. Removing elements from list: Deleting elements from the list
Strings
Simply using “+”symbol operator on two lists to join. If write ‘x+y’,the list ‘y’ is added to
‘x’,if write ‘y+x’ , the list ‘x’ is added to ‘y’.
14. Repeation of list: Repeat the elements of a list ‘n’ number of times using ‘*’ operator.
15. Membership in list : If element is a member of a list or not by using ‘in’ and ‘not in’ operator.
If element is a member of the list,then in operator returns true else false.
Program
18. Nesting of list: List containing one more list in it is know as nesting
With this we can say list is mutable where elements can be changed
7. What are
a) Constants in python with example program
b) Difference between c and python
ANS:
Constanats: Constants name should be written in all capital letters. If a constant has several words, then
each word should be separated by an underscore (_)
Example program
# Constant name
MAX_RETRIES = 5
# Function name
def calculate_area(radius):
# Class name
class Circle:
# Method name
def get_area(self):
return calculate_area(self._radius)
ANS:
b) Dictionaries
c)set