You are on page 1of 8

VR20

20ES2103B: PYTHON PROGRAMMING


Sessional - I Questions
SHORTS
1. List out the key features of Python Programming language which make it stand out of all other
high-level languages.
 Easy Language
 Readable
 Interpreted Language
 Object-Oriented
 Popular and Large Community Support
 Open-Source
 Large Standard Library
 Platform-Independent
 GUI Support

2. List out any 10 keywords in Python along with their functionalities.

Keyword Description

and A logical operator

break To break out of a loop

continue To continue to the next iteration of a loop

def To define a function

elif Used in conditional statements, same as else if

else Used in conditional statements

for To create a for loop

from To import specific parts of a module

if To make a conditional statement

while To create a while loop

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

3. List out the


i) comparison operators
ii) equality operators and
iii) logical operators that are used in Python

Operator Description

<=
< Comparison
> Operators
>=

<>
Equality
==
Operators
!=

not
or Logical Operators
and

4. What does [::-1] do?


The [::-1] reverses the whole string.

name = "Fredric"
print(name[::-1])

Output:

cirderF

5. What is the purpose of indentation in python?


Indentation refers to the spaces at the beginning of a code line. In other programming languages
the indentation in code is for readability only. But the indentation in Python is very important.
Python uses indentation to indicate a block of code.

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

6. How do you write comments in python?


Comment lines are written with a hashtag (#) at the start of the line.

# This is a comment line  Example of a comment line


<< code >>
<< code >>

7. What is the modulo ‘%’ operator in Python? How is it used?


Modulo: Given two positive numbers, a and b, a modulo b (a % b, abbreviated as a mod b) is the
remainder of the Euclidean division of a by b, where a is the dividend and b is the divisor.
Syntax:

a % b

Example:

x = 10
y = 2
mod = x % y
print("Modulo of the numbers: ", mod)

Output:
Modulo of the numbers: 0

8. What are python modules? Name some commonly used built-in modules in Python?
Module: A module is a file containing Python definitions and statements. A module can define
functions, classes, and variables. A module can also include runnable code. Grouping related code
into a module makes the code easier to understand and use. It also makes the code logically
organized. Programmer can import modules from packages using the dot (.) operator.
Modules in Python can be of two types: Built-in Modules, User-defined Modules.
Ex: date.py, sqrt.py, etc.

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

9. Explain ‘string’ and ‘list’ data types in Python.


i) Strings: Strings are arrays of bytes representing Unicode characters. A string is a collection of
one or more characters put in a single quote, double-quote or triple quote. In python there is no
character data type, a character is a string of length one. It is represented by str class.

x = "Python"
print("String: ", x)
print(type(x))

Output:

String: Python
<class 'str'>

ii) Lists: They are just like the arrays, declared in other languages which is an ordered collection
of data. It is very flexible as the items in a list do not need to be of the same type.

x = ["Fork", "and", "Knife"]


print("List: ", x)
print("First element: ", x[0])
print("Middle element: ", x[1])
print("Last element: ", x[2])
print(type(x))

Output:

List: ['Fork', 'and', 'Knife']


First element: Fork
Middle element: and
Last element: Knife
<class 'list'>

10. Write a Python program to find area of triangle.

from math import sqrt


a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))
s = (a+b+c)/2
area = sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ",round(area,2))

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

Output:

Enter first side: 8


Enter second side: 6
Enter third side: 5
Area of the triangle is: 14.98

11. What are functions in Python?


 Python Functions is a block of related statements designed to perform a computational,
logical, or evaluative task.
 The idea is to put some commonly or repeatedly done tasks together and make a function so
that instead of writing the same code again and again for different inputs, we can do the
function calls to reuse code contained in it over and over again.
 Functions can be both built-in or user-defined. It helps the program to be concise, non-
repetitive, and organized.

12. List any 10 built-in functions in Python along with their functionalities.

Function Description

abs() Returns the absolute value of a number

all() Returns True if all items in an iterable object are true

any() Returns True if any item in an iterable object is true

ascii() Returns a readable version of an object. Replaces none-ascii characters


with escape character

bin() Returns the binary version of a number

bool() Returns the boolean value of the specified object

bytearray() Returns an array of bytes

bytes() Returns a bytes object

callable() Returns True if the specified object is callable, otherwise False

chr() Returns a character from the specified Unicode code.

13. Explain int() and float() type conversions in Python with example.
1) int(): This function converts any data type to integer
2) float(): This function is used to convert any data type to a floating-point number

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

Example for int(), float():

string = "10010"
conv_int = int(string)
conv_float = float(string)
print("Before converting: ", string)
print("After converting to integer : ",conv_int)
print("After converting to float : ", conv_float)

Output:

Before converting: 10010


After converting to integer : 10010
After converting to float : 10010.0

14. How can you generate random numbers in Python?


Functions used for generating random numbers in Python are included in the random module.
1) random() 5) choice()
2) randint() 6) seed()
3) randrange() 7) shuffle()
4) sample() 8) uniform()

15. Explain random() and randint() functions in Python with example.


random(): generates a float number between 0 and 1.
Example:

import random
rand_num = random.random()
print("Random float between 0 and 1:", rand_num)

Output:

Random float between 0 and 1: 0.2852215948313275

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

randint(): generates a integer between a given range of numbers.


Example:

import random
rand_num = random.randint(0,22) # (first,last)
print("Random number in the range:", rand_num)

Output:

Random number in the range: 16

16. List out any 10 math functions in ‘math’ module with functionalities.

Function Description

ceil(x) Returns the smallest integer greater than or equal to x.

copysign(x, y) Returns x with the sign of y

fabs(x) Returns the absolute value of x

factorial(x) Returns the factorial of x

floor(x) Returns the largest integer less than or equal to x

fmod(x, y) Returns the remainder when x is divided by y

frexp(x) Returns the mantissa and exponent of x as the pair (m, e)

fsum(iterable) Returns an accurate floating point sum of values in the iterable

isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a Number)

isinf(x) Returns True if x is a positive or negative infinity

17. if a = True and b = False, what does print(a and b) give?

a = True
b = False
print(a and b)

Output:

False

Department of Electronics & Instrumentation Engineering, VRSEC


VR20

18. Use the lambda() function to find the sum of the integers 5, 6 and 2.

x = lambda a, b, c : a + b + c
print(x(5, 6, 2))

Output:

13

Department of Electronics & Instrumentation Engineering, VRSEC

You might also like