Sie sind auf Seite 1von 4

GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING

QUESTION BANK
2 marks with Answers

Unit II

1. Define the two modes in Python.


Python has two basic modes: script and interactive. The normal mode is the mode where

H
the scripted and finished .py files are run in the Python interpreter. Interactive mode is a
command line shell which gives immediate feedback for each statement, while running
previously fed statements in active memory. As new lines are fed into the interpreter, the fed

P
program is evaluated both in part and in whole. To start interactive mode, simply type python
without any arguments.
2. Give the various data types in Python.

SE
Python has five standard data types
Numbers
String
List
Tuple
JO
Dictionary
3. Point Out the rules to be followed for naming any identifier.
Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to
Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 and
print_this_to_screen, all are valid example.
An identifier cannot start with a digit. 1variable is invalid, but variable1 is
perfectly fine.
K

Keywords cannot be used as identifiers.


We cannot use special symbols like !, @, #, $, % etc. in our identifier
Identifier can be of any length
JU

4. Compose the importance of indentation in python.


Python programs get structured through indentation, i.e. code blocks are defined
by their indentation. In Python language, indentation is used to determine the structure
instead of using braces or keywords. Here, indentation is meaningful to the interpreter. It
is more than only clarity. The reader will be comfortable enough to go through it and
understand the meaning. Proper code indentation will make it.
JI

5. Select and assign how an input operation was done in python.


Python provides the function input(). input has an optional parameter, which is the
prompt string. If the input function is called, the program flow will be stopped until the
user has given an input and has ended the input with the return key. The text of the
optional parameter, i.e. the prompt, will be printed on the screen. Let's have a look at the
following example:
name = input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are already " + str(age) + " years old, " + name + "!")
We save the program as "input_test.py" and run it: $ python input_test.py
What's your name? "Frank"
Nice to meet you Frank!

H
Your age? 30
So, you are already 30 years old, Frank!
6. What are the various operators in python.

P
Python language supports the following types of operators.
Arithmetic Operators
Comparison (Relational) Operators

SE
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
7. Discover the difference between logical and bitwise operator.
JO
A Bitwise And operator is represented as & and a logical operator is represented
as &&. Following are some basic differences between the two operators.
a) The logical and operator && expects its operands to be boolean expressions
(either 1 or 0) and returns a boolean value.The bitwise and operator & works on
Integral (short, int, unsigned, char, bool, unsigned char, long) values and return
Integral value.
b) If an integral value is used as an operand for && which is supposed to work on
K

boolean values, following rule is used in C.


..A zero is considered as false and non-zero is considered as true.
c) The && operator doesnt evaluate second operand if first operand becomes
false. Similarly || doesnt evaluate second operand when first operand becomes
JU

true. The bitwise & and | operators always evaluate their operands.
The same differences are there between logical OR || and bitwise OR |.
8. Give the reserved words in Python.
Keywords are the reserved words in Python. We cannot use a keyword as variable
name, function name or any other identifier. They are used to define the syntax and
JI

structure of the Python language.


H
9. Give the operator precedence in python.
Highest precedence at top and lowest at bottom.
Operators in the same box evaluate left to right.

P
SE
JO
K

10. Define recursive function.


Recursion is a way of programming or coding a problem, in which a function calls itself
one or more times in its body. Usually, it is returning the return value of this function call. If a
JU

function definition fulfils the condition of recursion, we call this function a recursive function.

11. Point out the difference between recursive and iterative technique.
Recursive approach: In recursive approach the function calls itself until the condition is
met. And it is slower than iteration, which means it uses more memory than iteration.
JI

Recursion is like a selection structure, and which makes code smaller and clean. And a
function partially defined by itself. Here tracing the code will be more difficult in the case
large programs.
Iterative approach: Iterative approach is a repetition process until the condition fails,
here loops are used such as for, while etc. Here code may be longer but it is faster than
recursive. And it consumes less memory compared to recursive approach. If the loop
condition is always true in such cases it will be an infinite loop.

12. Give the syntax for variable length arguments.


The special syntax, *args and **kwargs in function definitions is used to pass a
variable number of arguments to a function. The single asterisk form ( *args ) is used to
pass a non-keyworded, variable-length argument list, and the double asterisk form is used

H
to pass a keyworded, variable-length argument list
Syntax for a function with non-keyword variable arguments is this
def functionname([formal_args,] *var_args_tuple ):

P
"function_docstring"
function_suite
return [expression]

SE
JO
K
JU
JI