Python Module 1 Question Bank Answers
Python Module 1 Question Bank Answers
Algorithm:
Print “FAIL”
else
Print “PASS”
End
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
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:
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:
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:
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:
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:
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
Flowchart:
Program:
5. Define variable. Explain the assignment statement and explain the rules
to use a variable/python naming convention.
Ans:
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
Types of Operators:
o Arithmetic Operators: they are used to perform basic operations as listed below:
Floor Division: F = a//b X= 5//3 (X will get a value 1), (returns only integral
part after division)
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.
Comparison: 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.
not: Return true, if the operand is false (it is a unary operator), (not a)
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
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
Ans:
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
Ans:
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
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:
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++ :
14. What are python modules. Write its syntax and examples.
Ans:
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
OUTPUT-
4.0
720
The * symbol used with the from import statement is used to import all the names from a
module to a current namespace.
SYNTAX-
EXAMPLE-
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-
EXAMPLE-
OUTPUT-
4.0
720
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-
EXAMPLE-
import math
print (math.pi)
OUTPUT-
3.141592653589793
Ans:
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’}
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}
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)
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]
EXAMPLE-
EXAMPLE-
String1 = "Hello"
String2 ="World"
print(String1+String2)
Output: Hello World
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
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