0% found this document useful (0 votes)
5K views

Python Module 1 Question Bank Answers

The document contains question bank answers for a Problem Solving Using Python Programming course. It includes definitions and examples of algorithms, flowcharts, Python character sets and keywords. It also provides algorithms, flowcharts, and programs to solve problems involving calculating area and perimeter of shapes, temperature conversions, interest calculations, and basic math operations. Programming concepts like variables, assignment statements, operators, operands and expressions are defined along with rules for naming variables in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

Python Module 1 Question Bank Answers

The document contains question bank answers for a Problem Solving Using Python Programming course. It includes definitions and examples of algorithms, flowcharts, Python character sets and keywords. It also provides algorithms, flowcharts, and programs to solve problems involving calculating area and perimeter of shapes, temperature conversions, interest calculations, and basic math operations. Programming concepts like variables, assignment statements, operators, operands and expressions are defined along with rules for naming variables in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

FACULTY: GELUVARAJ.

B, SUB: PROBLEM SOLVING USING PYTHON


PROGRAMMING, SECTION: R

QUESTION BANK ANSWERS

MODULE 1 - Basics of Python

1. Define algorithm and flowchart. Explain the characteristics of an


algorithm with example.
Ans:

 An ordered sequence of steps that describe the solution of problem is called an


algorithm.
 A flowchart is a diagrammatic representation of an algorithm.
 Characteristics of an algorithm are as follows:
o Input – algorithm should have one or more inputs.
o Output – algorithm must have at least one output.
o Finiteness – algorithm should have a finite number of steps to solve to
problem and get a valid output.
 Ex- An algorithm to determine a student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of marks of four
subjects (100 marks each)

Algorithm:

Step 1: Input M1, M2, M3, M4

Step 2: GRADE = (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then,

Print “FAIL”

else

Print “PASS”

End

2. Explain the character set of python and keywords available in python.


Ans:

 Character set is the set of valid characters that a language can recognize. A
character represents any letter, digit or any other symbol.
 Python has the following character sets:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

o Letters – A to Z, a to z
o Digits – 0 to 9 · Special Symbols - + - * / etc.
o Whitespaces – Blank Space, tab, return, newline, etc.
o Other characters – Python can process all ASCII and Unicode
characters as part of data or literals.
 Keywords are used to give special meaning to the interpreter and are used by
Python interpreter to recognize the structure of program. These are reserved for
special purpose and must not be used as normal names.
 Python keywords are as follows:
and as assert break class continue def del elif else except
False finally for from global if import in is lambda None
nonlocal not or pass raise return True try while with Yield

3. Design an algorithm, flowchart, and implement a program to compute the


following:
i) Area and perimeter of a circle
ii) Simple and compound interest
iii) Degree to radians
Ans:

i.Area and Circumference of a Circle:

 Algorithm:

Step 1: Start
Step 2: Input radius
Step 3: Let pi = 3.14
Step 4: Area = pi * radius * radius
Step 5: Circumference = 2 * pi * radius
Step 6: Print area and circumference
Step 7: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

ii. Simple and Compound Interest:

 Algorithm:

Step 1: Start
Step 2: Input principle amount(p), rate of interest(r), time over which interest
should be found(t)
Step 3: Calculate SI=p*t*r/100
Step 4: Calculate CI=p{*[(1+r/100)**t]}-p
Step 5: Print SI and CI
Step 6: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

iii. Degree to Radian:

 Algorithm:

Step 1: Start
Step 2: Input degree
Step 3: Calculate radian=degree*0.0175
Step 4: Print radian
Step 5: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

4. Design an algorithm, flowchart, and implement a program to compute the


following:
i. Celsius to Fahrenheit.
ii. Area of a triangle when base and height is given and all the
sides are given.
iii. Area of a rectangle, circle and square
iv. Simple calculator operations like addition, subtraction,
multiplication & division
v. Swapping of two numbers.

Ans:

i. Celsius to Fahrenheit:
 Algorithm:

Step 1: Start
Step 2: Input temperature in celsius
Step 3: Fahrenheit = (Celsius*1.8)+32
Step 4: Print Fahrenheit
Step 5: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

ii. Area of a triangle when base and height is given and all the sides are given.

 Algorithm:

Step 1: Start
Step 2: Input base, height, side1, side2 and side3 of
triangle
Step 3: Let s = (side1+side2+side3)/2
Step 4: Area = (base*height)/2 or [s*(s-side1)*(s-
side2)*(s-side3)]**0.5
Step 5: Print area
Step 6: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

iii. Area of a rectangle, circle and square

 Algorithm:

Step 1: Start
Step 2: Input length, breadth, radius and side
Step 3: Let pi = 3.14
Step 4: Area of rectangle = length*breadth
Step 5: Area of circle = pi * radius * radius
Step 6: Area of square = side*side
Step 7: Print area of rectangle, circle and square
Step 8: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

iv. Simple calculator operations like addition, subtraction, multiplication & division

 Algorithm:

Step 1: Start
Step 2: Input a, b
Step 3: Addition: Sum = a+b
Step 4: Subtraction: Difference = a-b
Step 5: Multiplication: Product = a*b
Step 6: Division: Quotient = a/b
Step 7: Print Sum, Difference, Product, Quotient
Step 8: Stop

 Flowchart:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Program:

v. Swapping of two numbers.

 Algorithm:

Step 1: Start
Step 2: Input a, b
Step 3: Let ‘temp’ be a temporary variable
Step 4: temp=a, a=b, b=temp
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

Step 5: Print a, b, temp


Step 6: Stop

 Flowchart:

 Program:

5. Define variable. Explain the assignment statement and explain the rules
to use a variable/python naming convention.

Ans:

 A variable is a named literal which helps to store a value in the program.


Variables may take value that can be modified wherever required in the program.

 Assignment statements are used to assign values to variables. The target of an


assignment statement is written on the left side of the equal sign (=), and the object on
the right can be an arbitrary expression that computes an object.
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

 Rules for naming variables:

1. A variable name must start with a letter or the underscore character.


2. A variable name cannot start with a number.
3. A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
4. Variable names are case-sensitive (age, Age and AGE are three different
variables)

5. Whitespace is not allowed in a variable name

6. Define operators, operands and expression. Explain the different


operators available in python with suitable example.

Ans:

 Operators are special symbols used to indicate specific task.An operator may work on
a single operand or two operands

 Operands are the variables on which the operations are performed.An operator may
work on a single operand or two operands

 Expressions are a combination of values,variables and operators.

 Types of Operators:

o Arithmetic Operators: they are used to perform basic operations as listed below:

Addition: Sum= a+b

Subtraction: Diff = a-b

Multiplication: Pro= a*b

Division: Q= a/b, X = 5/3 (X will get a value 1.666666667)

Floor Division: F = a//b X= 5//3 (X will get a value 1), (returns only integral
part after division)

Modulus: R = a %b (Remainder after dividing a by b), (remainder after


division)

Exponent: E = x** y (means x to the power of y)


FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

o Relational or Comparison Operators: they are used to check the relationship (like less
than, greater than etc) between two operands. These operators return a Boolean value either
True or False.

Greater than: a>b

Less than: a<b

Greater than or equal to: a>=b

Less than or equal to: a<=b

Comparison: a==b

Not equal to: a !=b

o Assignment Operators: Apart from simple assignment operator = which is used for
assigning values to variables, Python provides compound assignment operators. For example,
x= x+y can be written as x+=y. Now, += is compound assignment operator. Similarly, one
can use most of the arithmetic and bitwise operators (only binary operators, but not unary)
like *, /, %, //, &, ^ etc. as compound assignment operators.

o Logical Operators: There are 3 logical operators in Python.

and: Returns true, if both operands are true (a and b)

or: Returns true, if any one of two operands is true (a or b)

not: Return true, if the operand is false (it is a unary operator), (not a)

7. Discuss input () and print () function with syntax and example.

Ans:

 Input() function: In Python, we use input() function to take input from the user.
Whatever you enter as input, the input function converts it into a string. If you enter
an integer value still input() function convert it into a string.
 Syntax: input(prompt)
 Example : Taking input from the user with a message.
# Taking input from the user
name = input("Enter your name")

# Output
print("Hello", name)

Output:
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

Enter your name:pushpa raj


Hello pushpa raj

 Print() function: The print() function prints the specified message to the screen, or
other standard output device. The message can be a string, or any other object, the
object will be converted into a string before written to the screen.
 Example: Print a message onto the screen:
print("Hello World")

output:
Hello World

8. What is expression, write with operator precedence table .

Ans:

 An expression is a combination of operators and operands that is interpreted to


produce some other value. In any programming language, an expression is evaluated
as per the precedence of its operators. So that if there is more than one operator in an
expression, their precedence decides which operation will be performed first.
 The following table lists all operators from highest precedence to lowest.

9. Evaluate the following expression.


1. (2+3) *8/4//3
2. 18/6*2+9
3. (30 * 15) / 5
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

4. 20 + (150/5)
5. 12 //4 + 2 **4–5

Ans:

1. (2+3) *8/4//3:
5*2//3
=10//3
=3

2. 18/6*2+9:
3*2+9
=6+9
=15

3. (30 * 15) / 5:
450/5
=90

4. 20 + (150/5)=
20+30
=50

5. 12//4+2**4-5= 14
3+16-5
=3+11
=14

10. List the features of python programming language.

Ans:

 Features of python programming are:

o 1. Easy to code:
Python is a high-level programming language. Python is very easy to
learn the language as compared to other languages like C, C#,
Javascript, Java, etc. It is very easy to code in python language and
anybody can learn python basics in a few hours or days. It is also a
developer-friendly language.
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

o 2. Free and Open Source:


Python language is freely available at the official website and you can
download it from the given download link below click on
the Download Python keyword.
Download Python
Since it is open-source, this means that source code is also available to
the public. So you can download it as, use it as well as share it.
o 3. Object-Oriented Language:
One of the key features of python is Object-Oriented programming.
Python supports object-oriented language and concepts of classes,
objects encapsulation, etc.
o 4. GUI Programming Support:
Graphical User interfaces can be made using a module such as PyQt5,
PyQt4, wxPython, or Tk in python.
PyQt5 is the most popular option for creating graphical apps with
Python.
o 5. High-Level Language:
Python is a high-level language. When we write programs in python,
we do not need to remember the system architecture, nor do we need
to manage the memory.
11. What are the comparison operators and solve the given expressions?
1. (5 > 4) and (3 == 5)
2. not (5 > 4)
3. (5 > 4) or (3 == 5)
4. not ((5 > 4) or (3 == 5))
5. (True and True) and (True == False)
6. (not False) or (not True)
Ans:

1. (5 > 4) and (3 == 5)
= True and False (1 and 0)
= False
2. not (5 > 4)
= not True (not 1)
= False
3. (5 > 4) or (3 == 5)
= True or False (1 or 0)
= True
4. not ((5 > 4) or (3 == 5))
= not True or False (not 1 or 0)
= False or False (0 or 0)
= False
5. (True and True) and (True == False)
= True and False (1 and 0)
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

= False
6. (not False) or (not True)
= True or False (1 or 0)
= True

12. What are all the different types of comments and write about type
conversions with example.

Ans:

 Comments are non-executable statements in Python. The 2 types of comments


are:
o A single-line comment begins with a hash (#) symbol and is useful in
mentioning that the whole line should be considered as a comment until
the end of the line.
o Multi-line comment is useful when we need to comment on many lines.
In Python Triple double quote (""") and single quote (''') are used
for Multi-line commenting. It is used at the beginning and end of the
block to comment on the entire block. Hence it is also called block
comments.
 Python defines type conversion functions to immediately convert one data
type to another which is useful in day-to-day and aggressive programming.

13. What is python? Compare python with any other high-level language.

Ans:

Python is an easily adaptable programming language that offers a lot of features. Its concise
syntax and open-source nature promote readability and implementation of programs which
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

makes it the fastest-growing programming language in current times. Python has various
other advantages which give it an edge over other popular programming languages such as
Java and C++.

1)Python vs Java:

 In Python there is no need for semicolon and curly braces in the program as
compared to Java which will show syntax error if one forgot to add curly braces or
semicolon in the program.
 Python is dynamically typed that means one has to only assign a value to a variable
at runtime, Python interpreter will detect the data type on itself as compare to Java
where one has to explicitly mention the data type.

2) Python vs C++ :

 Python is more memory efficient because of its automatic garbage collection as


compared to C++ which does not support garbage collection.
 Python code is easy to learn, use and write as compare to C++ which is hard to
understand and use because of its complex syntax.

14. What are python modules. Write its syntax and examples.

Ans:

 Modules refer to a file containing Python statements and definitions.


 A file containing Python code, for example: example.py, is called a module, and its
module name would be example.
 We use modules to break down large programs into small manageable and organized
files. Furthermore, modules provide reusability of code.

1)Import Module in Python – Import statement

We can import the functions, classes defined in a module to another module using the import
statement in some other Python source file.

SYNTAX:
Import module

EXAMPLE:
#importing module calc.py
import calc
print(calc.add(10, 2))

OUTPUT-
12
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

2)The from import Statement


Python’s from statement lets you import specific attributes from a module without importing
the module as a whole.
EXAMPLE-

# importing sqrt() and factorial from the


# module math
from math import sqrt, factorial

# if we simply do "import math", then


# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))

OUTPUT-

4.0
720

3)Import all Names – From import * Statement

The * symbol used with the from import statement is used to import all the names from a
module to a current namespace.

SYNTAX-

from module_name import *

EXAMPLE-

# importing sqrt() and factorial from the


# module math
from math import *

# if we simply do "import math", then


# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))

print(factorial(6))

OUTPUT-

4.0
720
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

4)Locating Modules

Whenever a module is imported in Python the interpreter looks for several locations. First, it
will check for the built-in module, if not found then it looks for a list of directories defined in
the sys.path.

EXAMPLE-

# importing sys module


import sys
# importing sys.path
print(sys.path)

5)Importing and renaming module

We can rename the module while importing it using the as keyword.

EXAMPLE-

# importing sqrt() and factorial from the


# module math
import math as gfg
# if we simply do "import math", then
# math.sqrt(16) and math.factorial() are required.
print(gfg.sqrt(16))
print(gfg.factorial(6))

OUTPUT-

4.0
720

6)The dir() function

The dir() built-in function returns a sorted list of strings containing the names defined by a
module. The list contains the names of all the modules, variables, and functions that are defined
in a module.

SYNTAX-

# Import built-in module random


import random
print(dir(random))

EXAMPLE-

# Import math Library


FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

import math

# Print the value of pi

print (math.pi)

OUTPUT-
3.141592653589793

15. Explain the different data types in python with examples.

Ans:

1)Dictionary Data Type:-

Dictionary is an unordered collection of data values, which is used to store data values like a
map, which, unlike other Data Types that hold only a single value as an element, a Dictionary
consists of key-value pair. Key-value is provided within the dictionary to form it more
optimized.

SYNTAX:
Key:value

EXAMPLE-
Dict1 = {1:'Hello',2:5.5, 3:'World'}
print(Dict1)
Output: {1: ‘Hello’, 2: 5.5, 3: ‘World’}

2)Set Data Type:-

A set is an unordered collection of items. Every set element is exclusive (no duplicates) and
must be immutable

EXAMPLE-
Set = {4,3,6.6,"Hello"}
print(Set)
Output: {‘Hello’, 3, 4, 6.6}

3)Tuple Data Type:-


FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

A tuple is defined as an ordered collection of Python objects. The only difference between
tuple and list is that tuples are immutable i.e. tuples can’t be modified after it’s created. It is
represented by tuple class. we can represent tuples using parentheses ( ).

EXAMPLE-
Tuple = (25,10,12.5,"Hello")
print("Tuple[1] = ", Tuple[1])
Output: Tuple[1] = 10
print("Tuple[0:3] =", Tuple[0:3])
Output: Tuple[0:3] = (25,10,12.5)

4)List Data Type:-

A list is formed(or created) by placing all the items (elements) inside square brackets [ ],
separated by commas.
It can have any number of items and they may or may not be of different types (integer, float,
string, etc.).
A list is mutable, which suggests we will modify the list.

EXAMPLE-
List1 = [3,8,7.2,"Hello"]
print("List1[2] = ", List[2])
Output: List1[2] = 7.2
print("List1[1:3] = ", List[1:3])
Output: List1[1:3] = [8, 7.2]

5)String Data Type:-

The string is a sequence of Unicode characters. A string may be a collection of 1 or more


characters put during a quotation mark, double-quote, or triple quote. It can be represented
using an str class.

EXAMPLE-

string1= “Hello World”


print(string1)
output: Hello World
i)Concatenation: It includes the operation of joining two or more strings together.

EXAMPLE-

String1 = "Hello"
String2 ="World"
print(String1+String2)
Output: Hello World

ii)Slicing: Slicing is a technique for extracting different parts of a string.


FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

EXAMPLE-

String1 = "Hello"
print(String1[2:4])
Output: Hello
6)Numeric Data Type:-

In Python, numeric data type represents the data that has a numeric value. The numeric value
can be an integer, floating number, or even complex number. These values are defined as int,
float, and complex classes in Python.

i)Integers – This data type is represented with the help of int class. It consists of positive or
negative whole numbers (without fraction or decimal). In Python, there’s no limit to how
long integer values are often.

Example:-

a = 2

print(a, "is of type", type(a))


Output: 2 is of type

ii)Float – This type is represented by the float class. It is a true number with floating-point
representation. It is specified by a decimal point. Optionally, the character e or E followed by
a positive or negative integer could even be appended to specify scientific notation.

Example:-

b = 1.5
print(b, "is of type", type(b))
Output: 1.5 is of type
iii)Complex Numbers – Complex numbers are represented by complex classes. It is specified
as (real part) + (imaginary part)j, For example – 4+5j.

Example:-

c = 8+3j
print(c, "is a type", type(c))
Output: (8+3j) is a type

*****THANKS TO R SECTION ROYALS*****

*****YASHASVI, KSHAMITHA, MAHITHA, JAHANAVI, ALWYN,


LEELASAGAR, SATWIK****
FACULTY: GELUVARAJ.B, SUB: PROBLEM SOLVING USING PYTHON
PROGRAMMING, SECTION: R

You might also like