Page |1
UNIT II
2 Marks Questions and Answers
1. What is Python?
Python is a general-purpose, interpreted, interactive, dynamically typed, object oriented,
open source and high level programming language as well as scripting language. It was
created by Guido van Rossum during 1985- 1990.
2. Why python in known us dynamically typed language?
In python there is no need to declare the type of the variable. Variable type will be
automatically determined at run time depends on the value assigned to it. So it is also
known as dynamically typed language.
3. Why python in known as interpreted language?
Python is processed at runtime by the interpreter. You do not need to compile your program
before executing it. So it is also known as interpreted language.
4. Mention any five features of python.
Easy-to-learn: Python is clearly defined and easily readable. The structure of the
program is very simple. It uses few keywords.
Easy-to-maintain: Python's source code is fairly easy-to-maintain.
Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
Interpreted: Python is processed at runtime by the interpreter. So, there is no need to
compile a program before executing it. You can simply run the program.
Extensible: Programmers can embed python within their C, C++, JavaScript, ActiveX, etc.
5. What are the applications of Python?
Web Development
Game Development
Scientific and Numeric Applications
Enterprise-level/Business Applications
Education programs and training courses
Language Development
6. What is meant by interpreter?
The interpreter is a language processor which translates a single statement of source
program into machine code and executes it immediately before moving to the next. (i.e) it
translates only one statement of the program at a time.
7. What is meant by python interpreter?
The python interpreter is a python language processor which translates a single statement
of python program into machine code and executes it immediately before moving to the
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |2
next. Simply we can say that python interpreter is a program that reads and execute python
code.
8. What are the two modes of python interpreter?
There are two modes for executing the python code they are:
1. Interactive mode
2. Script mode
9. What is interactive mode in python?
The Interactive mode or Interpreter mode provides programmers a quick way to execute
commands without creating a python file (without .py). It is most convenient for the user
who is a beginner.
The triple chevron (>>>) is the prompt the interpreter use to indicate that it is ready to
accept instruction in interactive mode.
10.What is script mode in python?
The script mode is used to execute a python program written in a file. Here the full code is
written in a text file and saved with “.py” extension. This code can be edited and reused if
necessary. To write a long program, script mode is used.
11.How will you invoke the python in interactive mode?
Open python IDLE through start all programs Python IDLE (python), a python shell
will appear with chevron (>>>) symbol.
Or
Type “python” in windows command prompt and press enter, python shell will appear with
chevron (>>>) symbol.
12.How will you invoke the python in script mode?
Open python IDLE (Integrated Development Learning Environment), click file and choose
new file. Now type the code in this new file and save it using the extension “.py” and then
run the code by clicking “run module” or simply press F5 key.
13.What are the advantages of interactive mode?
Useful to run small code and get result immediately.
Faster compared to script mode.
Good for beginners to understand the basics.
14.What are the advantages of script mode?
Useful to run large code and get the result
Editing the code is easier.
Can reuse the saved codes
Good for beginners and experts
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |3
15.Discus about the difference between interactive mode and script mode.
interactive mode script mode
1 In this mode the statements are typed Here the full code is typed in a separate
and executed directly in the python file and executed separately.
shell.
2 Useful to run small code and get result Useful to run large code and get the
immediately. result.
3 Can’t edit the code after execution. Editing the code is easier.
4 Can’t reuse the executed codes. Can reuse the saved codes.
5 Good for beginners to understand the Good for beginners and experts.
basics.
16.What is Python IDLE ?
It stands for Integrated Development Learning Environment. Is a graphical user
interface which is completely written in Python.
It is bundled with the default implementation of the python language and also comes
with optional part of the Python packaging.
17.What are the features of IDLE?
Multi-window text editor with syntax highlighting.
Auto completion with smart indentation.
Python shell to display output with syntax highlighting.
18.Define values in python.
A value is one of the basic things a program works with, like a letter or a number or
string.
A programming language contains data in terms of input and output and any kind of
data can be presented in terms of value.
Example: Values are 2, 42.0, ‘A’ and 'Hello, World!'.
19.What is type() function in python?
Python offers a built in method called ‘type’ to know the exact data type of any value.
The syntax to know type of any value is: type (value)
Example:
>>> type(“hello world”)
<class ‘str’>
20.What are the data types available in python?
The different data types available in python are
1. Numeric data types
i) Int
ii) Float
iii) Complex numbers
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |4
2. Sequence data types
i) String
ii) List
iii) Tuple
3. Boolean
4. Set
5. Dictionary
21.Define String data type in python with example.
A string in python can be created using single or double or triple quotes (3 single
quotes). Strings are immutable which means that its value cannot be updated.
Example:
>>> s='welcome'
>>> print(s)
welcome
>>> s1="welcome back"
>>> print(s1)
welcome back
22.What is string concatenation operator?
The ‘ + ’ operator is the string concatenation operator which is used to
merge(concatenate) two strings.
Example:
>>>'good' + 'morning'
'goodmorning‘
23.Define list in python with example.
List is an ordered sequence of values called elements or items. It can be written as a list
of comma-separated items between square brackets[ ]. Items in the lists can be of
different data types.(i.e) The elements in a list do not need to be of same type.
Example:
>>>a=[12,12.5,'hello', True]
>>>print(a)
[12, 12.5, 'hello', True]
24.Define tuple in python with example.
A tuple is same as list, except that the set of elements are enclosed in parentheses
instead of square brackets and it is immutable (cannot be changed).
A tuple is an immutable list because, once a tuple has been created, you can't add
elements to a tuple or remove elements from the tuple.
Example:
tuple1=(10,20,40,50)
print(tuple1)
(10, 20, 40, 50)
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |5
25.Give an example to prove that the tuple is immutable.
A tuple is immutable because, once a tuple has been created, we can't add or remove
elements to/from a tuple and also we can’t alter any elements in the tuple.
Example:
tuple1 = (10, 20, 40, 50)
tuple1[2] = 30
File "<pyshell#4>", line 1, in <module>
tuple1[2]=30
TypeError: 'tuple' object does not support item assignment
26.What are the advantages of using tuple?
Tuples are faster than lists.
If the user wants to protect the data from accidental changes, tuple can be used.
Tuples can be used as keys in dictionaries, while lists can't.
27.Give the difference between list and tuple.
list tuple
1 Lists are mutable. Tuples are immutable.
2 In list all the elements are In tuple all the elements are
enclosed inside square brackets [] enclosed inside parentheses ()
3 List cannot be used as keys in Tuples can be used as keys in
dictionaries dictionaries
4 The unexpected changes and In tuple, it is hard to take place.
errors are more likely to occur.
5 List are not faster than tuples Tuples are faster than list
28.Define Boolean data type in python.
The Boolean data type is a primitive data type having one of the two value, True or
False
Here True is represented as 1 and False is represented as 0
The keyword used to represent this is bool.
Example:
>>>type(True)
<class ‘bool’>
29.Discuss about sets in python.
A set is a unordered and unindexed collection of elements.
Sets are created with curly brackets (i.e) all the elements in the set are enclosed in
curly brackets { }.
Example:
>>> set1={25, 15, 75, 10.5}
>>> print(set1)
{25, 10.5, 75, 15}
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |6
30.Discuss about Dictionaries in python.
Dictionary data type is unordered and mutable.
Dictionaries are created by using curly brackets { }. They are accessed via keys and
not via their position.
Dictionaries are unordered key-value-pairs(The association of a key and a value is
called a key-value pair ) The values of a dictionary can be any Python data type.
Example:
dict1={"dept“ : "CSE", "year“ : "first", "name“ : "siva", "age“ : 18}
print(dict1)
{'dept‘ : 'CSE', 'year‘ : 'first', 'name‘ : 'siva', 'age‘ : 18}
31.Give the difference between list and Dictionary.
Lists are ordered sets of elements, whereas dictionaries are unordered sets of
elements.
List is created by placing elements in [ ], separated by commas “, “ whereas
dictionary is created by placing elements in { }, each element (key value pair) is
separated by commas “, “
In list the elements are accessed via index position whereas in dictionary the
elements are accessed using keys.
32.Define variables in python.
Variables are named memory locations to store values. A variable allows us to store a
value by assigning it a name, which can be used later.
Example:
>>>x= 10
>>>y2= 20
33.What are rules for creating a variable?
A variable name must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
We cannot use a keyword as a variable name.
34.How to assign multiple values to multiple variables?
In python multiple assignments can be made in a single statement.
It supports simultaneous assignment to multiple variables.
Example:
>>> a, b, c = 25, 12.5, “hello”
This is equal to
>>> a = 25
>>> b = 12.5
>>> c = “hello”
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |7
35.How to assign a single value to multiple variables in python?
In python a value can be assigned to more than one variable in a single statement,
instead of assigning this using multiple statements.
Example:
>>> x = y = z = 100
This statement will assign the value 100 to the variable x, y and z
This is equal to
>>> x = 100
>>> y = 100
>>> z = 100
36.Define Keywords in python.
Keywords are the reserved words in Python. They are used to define the syntax and
structure of the Python language. We cannot use a keyword as variable name, function
name or any other identifier.
Example:
If, else, for, while, True, False, import, return etc.
37.What are identifiers in python?
Identifier is the name given to entities like class, functions, variables etc.
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 ( _ ).
Example:
Name of a variable
Name of a function
Name of a class
38.Define expressions in python.
An expression is a combination of values, variables, and operators. A value all by itself
is considered an expression. An expression can be broken down in to operators and
operands.
Operators are symbols which perform mathematical or logical operations.
Operands are the data on which the operators perform the operations.
39.What are the different types of expressions in python?
The different types of expressions in python are:
Constant Expressions
Arithmetic Expressions
Relational Expressions
Logical Expressions etc.
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |8
40.Define statements in python.
A statement is an instruction that the Python interpreter can execute. It is also called
a unit of code that has an effect.
Example: Statement for assigning a value to a variable or displaying a value.
>>> n = 7 Assignment statement
>>> print(n) Output statement
>>> x = input() Input statement
>>> z = (a + b)*3 Processing and Assigning statement
>>> if(a>b): Conditional statement
41.What is augmented assignment statement? Give example.
The arithmetic operators can be combined in assignments to form a augmented
assignment statements.
Example:
>>> x = 5
>>> y = 4
>>> print(x)
9
>>> x *= y
20
42.How will you extend multi-line statement in python?
Python statement can be extended to multiple lines using continuous character ‘ \ ’
Example:
>>> y=2
>>> x =3
>>> z = x + x * y \
/5*2 \
-2+x
Print(z)
6.4
43.How will you combine multiple statements on a single line in python?
The semicolon ‘ ; ‘ allows to combine multiple statements in a single line in python.
Example:
>>> a = 10; b = 20; c = a + b; print(c)
30
44.Define tuple assignment in python with example.
Python has a very powerful tuple assignment features that allows a tuple of variables
on the left of assignment to be assigned to a tuple of values on the right of the
assignment. Here the number of variables on the left side must match the number of
values on the right.
Example:
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
Page |9
>>>(a, b, c) = (10, 20, 30)
>>>print(b)
20
>>>print(c)
30
45.What are operators in python?
Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.
For example: >>> 2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the
output of the operation.
46.What are the different types of operators in python?
The different types of operators in python are:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
47.What are comparison operators in python?
Comparison operators are used to compare values. It returns either True or False
according to the condition. Different Comparison operators used in python are:
48.What are Logical operators in python?
Logical operators are used to combine conditional statements. Logical operators are
the and, or, not operators.
Example:
>>> a, b, c = 10, 20, 30
>>> (a>b) and (c>b)
False
>>> (a<b) and (b<c)
True
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
P a g e | 10
>>> (a>b) or (c>b)
True
>>> not (a>b)
True
49.What are Identity operators in python?
Identity operators are used to compare the objects, not if they are equal, but if they
are actually the same object, with the same memory location.
is and is not are the identity operators in Python. They are used to check if two values
(or variables) are located on the same part of the memory. Two variables that are
equal do not imply that they are identical.
50.What are Membership Operators in python?
They are used to test whether a value or variable is found in a sequence (string, list,
tuple, set and dictionary). in and not in are the membership operators in Python.
Example:
x = 'Hello world'
y = *“apple”, “banana”, “orange”+
print('H' in x)
print('Hello' not in x)
Output
True
False
51.What are Bitwise Operators in python?
Bitwise operators act on operands as if they were strings of binary digits. They operate bit
by bit, hence the name. For example, 2 is 10 in binary and 7 is 111 in binary.
Example:
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
52.Discover the difference between membership and identity operators in python
is and is not are the identity operators in Python. They are used to check if two values
(or variables) are located on the same part of the memory.
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
P a g e | 11
in and not in are the membership operators in Python. They are used to test whether
a value or variable is found in a sequence (string, list, tuple, set and dictionary).
53.Define indentation in python.
Most of the programming languages like C, C++, and Java use braces { } to define a
block of code. Python, however, uses indentation. Whitespace is used for indentation
in Python. All statements with the same distance to the right belong to the same
block of code. Without proper indentation in python code, you will end up seeing
Indentation Error and the code will not get compiled.
Generally, four whitespaces are used for indentation and are preferred over tabs.
Here is an example:
for i in range(1,11):
print(i)
if i == 5:
break
54.What is operator precedence in python?
The operator precedence determines the order in which the python interpreter
evaluates the operators in an expression.
Even though an expression may contain lot of operators, the operations on the
operands are carried out according to the priority known as precedence of operators.
The operator having highest priority is evaluated first, and then the operator having
the next highest priority is evaluated and so on.
Example:
Multiplication and Division have higher precedence than Addition and Subtraction.
So 2 * 3 - 1 = 5, not 4, and 6 + 4 / 2 = 8, not 5.
55.What is Associativity in Precedence?
When the expression contains operators with equal precedence, then associativity
decides which operations is to be performed first.
It implies the direction of execution.
o Left to Right (Left associative)
o Right to Left (Right associative)
Operators with the same precedence are left associative, so they are evaluated from
left to right (except exponentiation and assignment operators).
56.Define comments in python.
As program get bigger and more complicated it is difficult to read and understand
the program. For this reason it is good to add notes (documents) to our program to
explain in natural language what the program is doing. This notes or documents are
called comments.
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
P a g e | 12
They start with a hash (#) symbol and they are called line comment. When the python
interpreter encounter # symbol, it ignores all the text after # symbol on the same line.
Example:
V=u + a * t # Calculating the final velocity
Print(“The final velocity of the object is: ”, V) # Displaying the final velocity’
57.Define docstring in python.
Multiple line comments or comments on several lines are called paragraph
comments. This can be achieved by docstring. Docstring is short for documentation
string.
It is a string that occurs as the first statement in a module, function, class, or method
definition. We must write what a function/class does in the docstring. Triple quotes
(three consecutive quotation marks ‘’’) are used while writing docstring.
Example:
‘’’ This is a function to print the
Velocity of an object’’’
58.Discuss about debugging in python.
Debugging is the process of locating and rectifying errors (bugs) in a program. Python
standard library includes pdb module which contains a set of utilities for the
debugging of Python programs.
To start debugging within the program just insert import pdb in your program. For
debugging our program, we can use the command pdb.set_trace() . This will create a
breakpoint at this line number and enable debugging in your program.
59.Write a python program to swap or exchange the values of two variables using
temporary values.
60.Write a python program to swap or exchange the values of two variables using
temporary values.
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING
P a g e | 13
61.Write a Python program to find the distance between two points.
62.Illustrate the use of * and + operators in string with example.
The * operator performs repetition on strings and the + operator performs
concatenation on strings.
Example: >>> ‘Hello*3’
Output: HelloHelloHello
>>>’Hello+World’
Output: HelloWorld
63.What is the output of the following?
a. float(32)
b. float("3.14159")
Output:
a. 32.0 (The float function converts integers to floating-point numbers.)
b. 3.14159 (The float function converts strings to floating-point numbers.)
GE3151 : PROBLEM SOLVING AND PYTHON PROGRAMMING