0% found this document useful (0 votes)
100 views

Solution - Python Programming (203105211) Mid Sem

The document is a mid-semester exam for a Python programming course containing multiple choice, short answer, and long answer questions. It has 4 sections - Section 1 contains one-line multiple choice questions worth 5 marks. Section 2 contains a compulsory multiple choice question worth 5 marks. Section 3 contains 4 short answer questions worth a total of 12 marks. Section 4 contains 2 long answer questions worth a total of 8 marks. The exam tests students' knowledge of Python concepts like data types, operators, functions, conditionals, loops, and Fibonacci sequences.

Uploaded by

Jaswanth Sudha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Solution - Python Programming (203105211) Mid Sem

The document is a mid-semester exam for a Python programming course containing multiple choice, short answer, and long answer questions. It has 4 sections - Section 1 contains one-line multiple choice questions worth 5 marks. Section 2 contains a compulsory multiple choice question worth 5 marks. Section 3 contains 4 short answer questions worth a total of 12 marks. Section 4 contains 2 long answer questions worth a total of 8 marks. The exam tests students' knowledge of Python concepts like data types, operators, functions, conditionals, loops, and Fibonacci sequences.

Uploaded by

Jaswanth Sudha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Enrolment Number: _________________

PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
B.TECH MID SEM EXAMINATION
3rd SEMESTER
ACY-2022-23
Python Programming Solution (203105211) Branch: CSE/IT
Date: 05/08/2022 Time: 2.30 PM to 4.00 PM Total Marks: 40

Sr. No. Marks


Q.1 (A) One line Questions 05
1. Indentation refers to the spaces at the beginning of a code line.
2. We can use the int() built-in function.
3. int and float
4. Using #
5. An identifier starts with a letter A to Z or a to z or an underscore (_) followed
by zero or more letters, underscores and digits (0 to 9).
(B) Compulsory Question 05
1. Concatenation
2. 6
3. length
4. a function is a group of related statements that performs a specific task.
5. 'Break' in Python is a loop control statement. It is used to control the sequence
of the loop. Suppose you want to terminate a loop and skip to the next code
after the loop; break will help you do that.
Q.2 Attempt any four(Short Questions) 12
(1) Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.
arithmetic operators:

(2) In Python, we know that a function can call other functions. It is even possible for
the function to call itself. These types of construct are termed as recursive functions.
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
Page 1 of 5
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))

(3) The slice() constructor creates a slice object representing the set of indices
specified by range(start, stop, step).
Syntax:
slice(stop)
slice(start, stop, step)
s1 = slice(3)
s2 = slice(1, 5, 2)
s3 = slice(-1, -12, -2)
(4)
(2**3 + (19 // 6)**(1 + 1))
=(8 + (3)**(2))
=8+9=17

(5)
Python Lambda Functions are anonymous function means that the function is without
a name.
Syntax: lambda arguments: expression
Ex
tables = [lambda x=x: x*10 for x in range(1, 11)]

for table in tables:


print(table())

o/p: 10 20 30 40 50 60 70 80 90 100
Q.3 Attempt any two 08
(1) Python is used in many application domains. Here's a sampling:
Web Applications
We can use Python to develop web applications. It provides libraries to handle
internet protocols such as HTML and XML, JSON, Email processing, request,
beautifulSoup, Feedparser, etc. One of Python web-framework named Django is used
on Instagram.
Desktop GUI Applications
The GUI stands for the Graphical User Interface, which provides a smooth
interaction to any application.
Software Development
Python is useful for the software development process. It works as a support
language and can be used to build control and management, testing, etc.
Scientific and Numeric

Page 2 of 5
This is the era of Artificial intelligence where the machine can perform the task the
same as the human. Python language is the most suitable language for Artificial
intelligence or machine learning. It consists of many scientific and mathematical
libraries, which makes easy to solve complex calculations.
The ides I am using: pydev, sublime text, Visual Studio Code, Spyder etc
(2)
Functions that we define ourselves to do certain specific task are referred as user-
defined functions. The way in which we define and call functions in Python are
already discussed. Functions that readily come with Python are called built-in
functions.
def fun():
print("Inside function")

# Driver's code
# Calling function
fun()

(3)
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do something
else if the condition is false. Here comes the else statement. We can use the else
statement with if statement to execute a block of code when the condition is false.
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false

num = float(input("Enter a number: "))


if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Q.4 (A) 05
In Python, there are two kinds of loop structures:

for: Iterate a predefined number of times. This is also known as a definite iteration
while: Keep on iterating until the condition is false. This is known as an indefinite
iteration

maximum = int(input(" Please Enter the Maximum Value : "))


total = 0

for number in range(1, maximum+1):


Page 3 of 5
if(number % 2 == 0):
print("{0}".format(number))
total = total + number

print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, total))

(B)
In Python, 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.

OR
(B) 05
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)

Page 4 of 5
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Page 5 of 5

You might also like