Viva Review

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Review of Python | Vineeta Garg

REVIEW OF PYTHON
VIVA QUESTIONS

1. What does IDLE stand for? What is Python IDLE?

IDLE is an acronym of Integrated Development Environment and is the


standard, most popular Python development environment.

To run a program, we basically need an editor to write it, an interpreter/compiler


to execute it and a debugger to catch and remove the errors. Python IDLE provides
all these tools as a bundle. It lets edit, run, browse and debug Python Programs
from a single interface and makes it easy to write programs.

2. What is the difference between Interactive mode and Script mode?

Python IDLE can be used in two modes: Interactive mode and Script mode.
Python shell is an interactive interpreter. Python editor allows us to work
in script mode i.e. we can create and edit python source file.

3. What are tokens?


Tokens- Smallest individual unit of a program is called token. Example keywords,
identifiers, literals, operators and punctuators.

4. What are Keywords?


They are the words used by Python interpreter to recognize the structure of
program. As these words have specific meaning for interpreter, they cannot be used
for any other purpose.

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

NOTE: All these keywords are in small alphabets, except for False, None, True,
which are starting with capital alphabets.

5. What are identifiers and literals?


IDENTIFIERS: These are the names given to identify any memory block,
program unit or program objects, function, or module. Examples of identifiers are:
num, roll_no, name etc.

1
Review of Python | Vineeta Garg

LITERALS: A fixed numeric or non-numeric value is called a literal. Examples of


literals are: 502, -178.76, “Rajan” etc.

6. What is a variable?
Variables are like containers which are used to store the values for some input,
intermediate result of calculations or the final result of a mathematical operation
in computer memory.

7. What are the naming conventions of identifiers/variables/literals.


➢ Variable names are case sensitive. For eg. num and NUM are treated as two
different variable names.
➢ Keywords or words, which have special meaning, should not be used as the
variable names.
➢ Variable names should be short and meaningful.
➢ All variable names must begin with a letter or an underscore (_).
➢ After the first initial letter, variable names may contain letters and digits (0
to 9) and an underscore (_), but no spaces or special characters are allowed.
Examples of valid variable names: sum, marks1, first_name, _money

Examples of invalid variables names: marks%, 12grade, class, last-name

8. What do you understand by the terms L-value and R-value?


L-value is a value which has an address. Thus, all variables are l-values since
variables have addresses. As the name suggests l-value appears on left hand side
but it can appear on right hand side of an assignment operator(=). l-value often
represents as identifier.

R-value refers to data value that is stored at some address in memory. An r-


value is a value which is assigned to an l-value. An r-value appears on right but not
on left hand side of an assignment operator(=).

9. What is the default separator and terminator in print() function in


Python? How can we change it?

By default, print uses a single space as a separator and a \n as a terminator (appears


at the end of the string).
We can use the parameters sep and end to change the separator and terminator
respectively.

10.Compare and contrast list, tuple and dictionary.

List Tuple Dictionary

2
Review of Python | Vineeta Garg

A list is an ordered A tuple is an ordered A dictionary is an


collection of comma- collection of commas unordered collection of
separated values (items) separated values. items where each item is
within square brackets. The comma separated
a key: value pair. We can
values can be enclosed in
also refer to a dictionary
parenthesis but
as a mapping between a
parenthesis are not
set of keys/indices and a
mandatory.
set of values. The entire
dictionary is enclosed in
curly braces.

it is mutable it is immutable. it is mutable


list2=[“Raman”, 100, tup1=(‘Sunday’, dict1={'R':'RAINY' ,
200, 300, “Ashwin”] ‘Monday’, 10, 20)
'S':'SUMMER',
'W':'WINTER' ,
'A':'AUTUMN'}

11. What is the difference between mutable and immutable data types?

Mutable data types immutable data types


Variables of mutable data types can be
changed after creation and assignment Variables of immutable data types can
of values. not be changed or modified once they
are created and assigned the value. If an
attempt is made to change the value of
variable of immutable datatype, the new
is stored in some other memory location
and the variable starts pointing to that
location.

Some of the mutable data types in


Python are list and dictionary some of the immutable data types
are int, float, decimal, bool, string
and tuple.

12. What are the comments? Declare a single line comment and a multi-
line comment.
Comments are the line that compiler ignores to compile or execute. There are two
types of comments in Python

3
Review of Python | Vineeta Garg

• Single line comment: This type of comment deactivates only that line
where comment is applied. Single line comments are applied with the help of
“ #”. For e.g.

# This program calculates the sum of two numbers


>>>n1=10
>>>n2=20
>>>sum= n1 + n2 # sum of numbers 10 and 20
>>>print (“sum=”, sum)

• Multi line Comment: This Type of comment deactivates group of lines when
applied. This type of comments is applied with the help of triple quoted
string.

'''This is a
multiline comment'''

or

"""This is a
multiline comment"""

13. What are the different types of errors in a Python program?

SYNTAX ERROR: An error in the syntax of writing code that does not conform
to the syntax of the programming language is called a syntax error.
LOGICAL ERROR: It is an error in a program's source code that results in
incorrect or unexpected result. It is a type of runtime error that may simply
produce the wrong output or may cause a program to crash while running.
RUN TIME ERROR: A runtime error is an error that occurs during execution of
the program and causes abnormal termination of program.

14. Write functions for the following:

To convert string to int int()


To convert string to float float()
To convert numeric data to str()
string

15. What are operators?


Operators performs some action on data. Various types of operators are:
• Arithmetic (+,-,*,/,%)
• Assignment Operator (=)

4
Review of Python | Vineeta Garg

• Relational/comparison (<,>, <=,>=,==,!=)


• Logical (AND, OR, NOT)
• Identity Operators (is, is not)
• Membership Operators (in, not in)

16. What is difference between /, //, %?

/ // %
Divides two operands Integer division Divides two operands
and gives quotient and gives remainder
10/5=2 5//2=2 10%5=0
10/3.0 = 3.3 5.0//2=2.0
10.0/3=3.3
10/3=3

17. What is the difference between * and **?

* **
Multiplies two operands Exponentiation
10*5=50 2**4= 16

18.What is the difference between = and ==?

= ==
Assigns the value Checks if the value of left operand is
equal to the value of right operand, if
yes then condition becomes true.
x=50 15= = 15, true
16 = = 15, false

19. What is the difference between concatenation and repetition?


Concatenation (+) Repetition (*)
Joins the objects (strings, lists etc.) Concatenates multiple copies of the same
on either side of the operator object (strings, lists etc.) given number of
times
>>> str1="Tech" >>> str1*2
>>> str2="Era" 'TechTech'
>>> str1+str2
'TechEra'

>>> str3=str2+str1
5
Review of Python | Vineeta Garg

>>> print(str3)
EraTech

20. What is the difference between is and in operators?

is, is not - Identity Operators in, not in - Membership Operators


Identity operators are used to Membership operators are used to
compare two variables whether they validate the membership of a value. It
are of same type with the same tests for membership in a sequence,
memory location. such as strings, lists, or tuples.
The is operator evaluates to true if the The in operator is used to check if a
variables on either side of the operator value exists in a sequence or not.
point to the same memory location Evaluates to true if it finds a variable
and false otherwise. in the specified sequence and false
>>>x = 5 otherwise.
>>>type(x) is int >>>x = [1,2,3,4,5]
True >>>3 in x
True
The is not operator evaluates to true The not in operator evaluates to true
if the variables on either side of the if it does not find a variable in the
operator point to the different specified sequence and false
memory location and false otherwise. otherwise.
>>>x = 5 >>>x = [1,2,3,4,5]
>>>type(x) is not int >>>7 not in x
False True

21. What is the use of indentation in Python?


Indentation in Python is used to form block of statements also known as suite. It
is required for indicating what block of code, a statement belongs to. Indentation
is typically used in flow of control statements (like if, for etc.), function, class etc.
Although we can give any number of spaces for indentation but all the statements
in a block must have the same indent.
Unnecessary indentation results in syntax error and incorrect indentation results
in logical error.

22. What is the difference between indexing and slicing?


Indexing is used to retrieve an element from an ordered data type like string, list,
tuple etc. Slicing is used to retrieve a subset of values from an ordered data type.
A slice of a list is basically its sub-list.

23. What is the difference among insert, append and extend?

insert() append() extend()

6
Review of Python | Vineeta Garg

It is used to insert data It is used to add an is used to add one or


at the given index element at the end of the more element at the end
position. list. of the list.

z1=[121, "RAMESH", list1=[100, 200, 300, z1=[121, "RAMESH",


890, 453.90] 400, 500] 890, 453.90]
z1.insert(1, “KIRTI”) list2=[“Raman”, 100, z1.insert(1, “KIRTI”)
“Ashwin”] z1.append(10)
list1.extend(list2)
OUTPUT print(list1) print(z1)

[121, 'KABIR',
'RAMESH', 890, OUTPUT OUTPUT
453.9]
[100, 200, 300, 400, [121, 'KABIR',
500, 'Raman', 100, 'RAMESH', 890,
'Ashwin'] 453.9,10]

here list2 is added at the here list2 is added at the


end of the list list1. end of the list list1.

24. What is the difference among del, remove or pop methods to delete
elements from a List?

pop() del remove()


It removes the element The del method The remove method is
from the specified removes the specified used when the index
index, and also return element from the list, value is unknown and the
the element which was but it does not return element to be deleted is
removed. The last the deleted value. known.
element is deleted if no
index value is provided
in pop ( ).

>>> list1 =[100, 200, >>> list1=[100, 200, >>> list1=[100, 200,
90, 'Raman', 100, 'Raman', 100] 50, 400, 500,
'Ashwin'] >>> del list1[2] 'Raman', 100,
>>> print(list1) 'Ashwin']
>>> list1.pop(2)
>>> list1.remove(400)
OUTPUT >>> print(list1)
OUTPUT
90 [100, 200, 100] OUTPUT

7
Review of Python | Vineeta Garg

[100, 200, 50, 500,


'Raman', 100,
'Ashwin']

25. What are the major differences between key and value in a
dictionary?

key value
Keys are unique Values may not be unique
The keys must be of an immutable data The values of a dictionary can be of
any type
type such as strings, numbers, or
tuples.

26. Give the difference between upper() and isupper() functions of string.
The upper() function returns the copy of the string with all the letters in
uppercase. The isupper() function returns True if the string is in uppercase.
Example
>>> print(str3.upper())
ERATECH

>>> str5='tech era *2018*'


>>> print(str5.isupper())
False

27. Compare random(), randint() and randrange().

Function Purpose Example


name
random() Generates a random float number >>> random.random()
between 0.0 to 1.0. 0.21787519275240708

randint() Returns a random integer >>> random.randint(1,50)


between the specified integers. 21
>>> random.randint(1,50)
35

randrange() Returns a randomly selected >>> random.randrange(1,20)


element from the range created 1
by the start, stop and step >>>
random.randrange(1,20,5)
arguments. The value of start is 0

8
Review of Python | Vineeta Garg

by default. Similarly, the value of 16


step is 1 by default.

28. When is the else clause executed in looping statements?

The else clause in looping statements is executed when the loop terminates.

29. Compare break and continue.

BREAK CONTINUE

Break statement causes the Continue statement causes the current


loop to break/ terminate iteration of the loop to skip and go to the
immediately. next iteration.

The loop of which, break All the statements following the


statement is a part, stops. continue statement in the loop will not
be executed and loop control goes to the
next iteration.

s=10; s=10;
for i in range (10, 20, 3): for i in range (10, 20, 3):
s+=i s+=i
if(i==16): if(i==16):
break continue
print(s); print(s);
print("end"); print("end");

OUTPUT: OUTPUT:
20 20
33 33
end 68
end

You might also like