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

CT-1 - Paper (Python-BCC302) - Solution

The document provides information about a Python programming class test for students. It includes details like the course name, instructors, learning outcomes and sections with multiple choice and long answer questions testing Python fundamentals and control flow statements.

Uploaded by

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

CT-1 - Paper (Python-BCC302) - Solution

The document provides information about a Python programming class test for students. It includes details like the course name, instructors, learning outcomes and sections with multiple choice and long answer questions testing Python fundamentals and control flow statements.

Uploaded by

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

Roll Number : ................................................

Allenhouse Institute of Technology


176, Kulgaon Road, Rooma, Kanpur – 208008 U.P.
Class Test – I : Odd Semester 2023-24
Program : B. Tech. CS/AIML/Core Sem : III Course Code : BCC-302

Course Name : Python Programming Max. Time : 120 Minutes

Course assigned to : Mr. Gaurav Tiwari Max. Marks : 30

CO-1 : Interpret the fundamental Python syntax and semantics of python.


CO-2 : To be fluent in the use of Python control flow statements.
Section – A (CO - 1 ) # Attempt both the questions # 15 Marks
Q.1 : Attempt any Four questions (Short Answer Type). Each question is of 1.5 marks.
(1.5 x 4 = 6 Marks)
a) What is Python IDE? List some Python IDE Names.
b) What do you mean by python comments?
c) Explain Arithmetic Operators with suitable example.
d) Define Python Program life cycle with block diagram.
e) What are the applications of python Programming?
Q.2 : Attempt any THREE questions (Medium Answer Type). Each question is of 3 marks.
(3 x 3 = 9 Marks)
a) Explain type conversion in Python with the help of example.
b) What are local and global variables in Python? Explain with example.
c) What are Membership operators? Explain with example.
d) Write a Program to Swap Two Numbers (entered by user) without using third variable.
e) Write a Program to calculate simple interest.
Simple Interest = (principle x time x rate)/100

Section – B (CO - 2 ) # Attempt both the questions # 15 Marks


Q.3 : Attempt any Four questions (Short Answer Type). Each question is of 1.5 marks.
(1.5 x 4 = 6 Marks)
a) Explain ‘break’ and ‘continue’ statements in python.
b) Explain range() function and its parameters.
c) What is the use of pass statement in python?
d) Write a program to take three numbers from the user and print the greatest number.
e) Write a program to print numbers from 20 to 10 in reverse using loop.
Q.4 : Attempt any THREE questions (Medium Answer Type). Each question is of 3 marks.
(3 x 3 = 9 Marks)
a) Explain all the conditional statement in Python using small code example.
b) Write a program to check whether a number entered by user is Prime or not.
c) Write a program to calculate factorial of a number entered by user.
d) Write a program to check whether a given string is a palindrome or not.
e) Write a program for Sum of squares of first n natural numbers.
Ex: if Input: N = 4
Then Output: 30
12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30
SOLUTION
Qu-1(a) What is Python IDE? List some Python IDE Names.
Sol: Integrated Development Environments (IDEs) are coding tools that make writing,
debugging, and testing your code easier. They provide helpful features like code
completion, syntax highlighting, debugging tools, variable explorers, visualization tools,
and many other features.

Some Top Python IDEs-


 IDLE (Integrated Development and Learning Environment) is a default Python IDE.
 PyCharm
 Visual Studio Code
 Sublime Text
 Atom
 Jupyter
 Spyder

Qu-1(b) What do you mean by python comments?


Sol: Comments in Python are the lines in the code that are ignored by the interpreter
during the execution of the program. Comments enhance the readability of the code.

➢ Comments start with a #, and Python will render the rest of the line as a comment:
EX:
# This is a comment.
print("Hello, World!")

➢ Triple-quoted string is also ignored by Python interpreter and can be used as a


multiline comment:
'''
This is a multiline comment.
'''

Qu-1(c) Explain Arithmetic Operators with suitable example.

Sol: Arithmetic Operators are used to perform mathematical operations like addition,
subtraction, multiplication, and division.
Operator Description
It is used to add two operands. For example, if a = 20, b = 10
+ (Addition)
=>a+b = 30

It is used to subtract the second operand from the first operand.


- (Subtraction) If the first operand is less than the second operand, the value
results negative. For example, if a = 20, b = 10 => a - b = 10
It returns the quotient after dividing the first operand by the
/ (divide)
second operand. For example, if a = 20, b = 10 => a/b = 2.0

It is used to multiply one operand with the other. For example, if


* (Multiplication)
a = 20, b = 10 => a * b = 200

It returns the reminder after dividing the first operand by the


% (reminder)
second operand. For example, if a = 20, b = 10 =>a%b = 0

It is an exponent operator represented as it calculates the first


** (Exponent)
operand power to the second operand.

It gives the floor value of the quotient produced by dividing the


// (Floor division)
two operands.

Qu-1(d) Define Python Program life cycle with block diagram.


Sol:
 The development cycle of Python is considerably shorter than that of traditional
tools. There are no compilation or linking steps in Python.
 Python programs simply import modules at runtime and use the objects they
contain. Because of this, Python programs run immediately after changes are
made.
 Also because Python is interpreted, there’s a rapid turnaround after program
changes. And because Python’s parser is embedded in Python-based systems, it’s
easy to modify programs at runtime.
Qu-1(e) What are the applications of python Programming?
Sol:
Python can be used for:
 web development (server-side)
 software development
 system scripting
 handle database and big data
 complex mathematics
 rapid prototyping

====================================================================

Qu-2(a) Explain type conversion in Python with the help of example.


Sol:
The process of converting the value of one data type (integer, string, float, etc.) to
another data type is called type conversion. Python has two types of type conversion.

1. Implicit Type Conversion


2. Explicit Type Conversion

Implicit Type Conversion:


 In Implicit type conversion, Python automatically converts one data type to
another data type. This process doesn't need any user involvement.
 Python promotes the conversion of the lower data type (integer) to the
higher data type (float) to avoid data loss.

Example: Converting integer to float

num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of um_int:",type(num_int))
print("datatype of um_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of um_new:",type(num_new))
Explicit Type Conversion:
 In Explicit Type Conversion, users convert the data type of an object to
required data type.
 We use the predefined functions (Constructors) like int(), float(), str(), etc to
perform explicit type conversion.
 This type of conversion is also called typecasting because the user casts
(changes) the data type of the objects.

Example 3: Addition of string and integer using explicit conversion


num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))

Qu-2(b) What are local and global variables in Python? Explain with
example.
Sol:
Python Global variables are those which are not defined inside any function and have
a global scope whereas Python local variables are those which are defined inside a
function and their scope is limited to that function only.

Python Local Variables: Local variables in Python are those which are initialized
inside a function and belong only to that particular function. It cannot be accessed
anywhere outside the function. Let’s see how to create a local variable.
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)

f()

Python Global Variables: These are those which are defined outside any function and
which are accessible throughout the program, i.e., inside and outside of every function.
Let’s see how to create a Python global variable.
# This function uses global variable s
def f():
print("Inside Function", s)

# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)

Qu-2(c) What are Membership operators? Explain with example.


Sol:
Membership Operators are Used to check the membership of value inside a Python data
structure(list, tuple, or dictionary).Itteststhat a sequence is presented in an object or
not.

Operator Description Example

Returns True if a sequence with the specified


in x in y
value is present in the object

Returns True if a sequence with the specified


not in x not in y
value is not present in the object

Ex:

x = ["Java", "Python"]

print("Python" in x) # True

print("PHP" in x) # False

Qu-2(d) Write a Program to Swap Two Numbers (entered by user)


without using third variable.
Sol:

x = int(input("Enter First number"))


y = int(input("Enter Second number"))
print("Before Swap : x =",x,"y =",y)
x = x+y
y = x-y
x = x-y
print("After Swap : x =",x,"y =",y)
Qu-2(e) Write a Program to calculate simple interest.
Simple Interest = (principle x time x rate)/100
Sol:

p = int(input("Enter Principle Amount : "))


t = int(input("Enter Time : "))
r = int(input("Enter Interest Rate : "))
si = (p*t*r)/100
print("Simple Interest =", si)

====================================================================

Qu-3(a) Explain ‘break’ and ‘continue’ statements in python.


Sol:
 The break statement in Python is used to terminate the loop or statement in which
it is present. After that, the control will pass to the statements that are present after
the break statement, if available.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)

O/P: apple

 The continue statement is opposite to that of the break statement, instead of


terminating the loop, it forces to execute the next iteration of the loop. As the name
suggests the continue statement forces the loop to continue or execute the next
iteration. When the continue statement is executed in the loop, the code inside the
loop following the continue statement will be skipped and the next iteration of the
loop will begin.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
O/P: apple
cherry
Qu-3(b) Explain range() function and its parameters.
Sol:
The range() function is used to generate the sequence of the numbers. If we pass the
range(10), it will generate the numbers from 0 to 9.
Syntax:
range (start, stop, step_size)
 The start represents the beginning of the iteration. It is optional.
 The stop represents that the loop will iterate till stop. The range(1,5) will
generate numbers 1 to 4 iterations.
 The step_size is used to skip the specific numbers from the iteration. It is optional
to use. By default, the step size is 1. It is optional.

Ex: Program to print numbers in sequence.

for i in range(10): ``
print(i,end = ' ')

O/P: 0 1 2 3 4 5 6 7 8 9

Qu-3(c) What is the use of pass statement in python?


Sol: Loops cannot be empty, but if for some reason you have a loop with no content,
put in the pass statement to avoid getting an error.
for x in [0, 1, 2]: ``
pass

The pass statement is a null statement. But the difference between pass and comment
is that comment is ignored by the interpreter whereas pass is not ignored.
The pass statement is generally used as a placeholder i.e. when the user does not know
what code to write. So, user simply places pass there as empty code is not allowed in
loops, function definitions, class definitions, or in if statements. So, using pass
statement user avoids this error.
Ex:
a = 10
b = 20
if(a<b):
``
pass
else:
print("b<a")
Qu-3(d) Write a program to take three numbers from the user and
print the greatest number.
Sol:
x = int(input("Enter First number"))
y = int(input("Enter Second number"))
z = int(input("Enter Third number"))
``
a = x if x>y else y
b = a if a>z else z
print("Largest Number is :", b)

Qu-3(e) Write a program to print numbers from 20 to 10 in reverse


using loop.
Sol:
for i in range(20,9,-1): ``
print(i)

====================================================================

Qu-4(a) Explain all the conditional statement in Python using small


code example.
Sol: Decision making statements/ Conditional Stat ements (if, elif, else):
Decision making is required when we want to execute a code only if a certain condition
is satisfied. The if…elif…else statement is used in Python for decision making. All the
statements of one block are intended at the same level indentation.

 Python if statement:
The if statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not.

Syntax:
``
if expression:
statement

num = 3
if num > 0: ``
print(num, "is a positive number.")
print("This is always printed.")
 Python If-Else Statement:
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 if we want to do something else if
the condition is false, we can use the else statement with if statement to execute a block
of code when the if condition is false.

Syntax:
if condition:
#block of statements``
else:
#another block of statements (else-block)

Ex:

num = int(input("enter the number?"))


if num%2 == 0:
``
print("Number is even")
else:
print("Number is odd")

 Python if-elif-else Ladder:


Here, a user can decide among multiple options. The if statements are executed from
the top down. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed.

Syntax:
if expression 1:
# block of statements

elif expression 2:
# block of statements ``

elif expression 3:
# block of statements

else:
# block of statements
Ex:

number = int(input("Enter the number?"))


if number==10:
print("number is equals to 10")
elif number==50:
``
print("number is equal to 50");
elif number==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100");

Qu-4(b) Write a program to check whether a number entered by user


is Prime or not.
Sol:
x = int(input("Enter a Number : "))
f=0
if x<2:
print("Not a Prime Number")
else:
for i in range(2,x):
if x%i==0:
f=1
break
if f==0:
print("Prime Number")
else:
print("Not a Prime Number")

Qu-4(c) Write a program to calculate factorial of a number entered by


user.
Sol:
x = int(input("Enter a Number : "))
f=1
if x<0:
print("Factorial not exist for negative numbers")
elif x==0:
print("Factorial of 0 is : 1")
else:
for i in range(2,x+1):
f=f*i
print("Factorial is : ",f)

Qu-4(d) Write a program to check whether a given string is a


palindrome or not.
Sol:
x = input("Enter a String : ")
rev = ''
for i in range(len(x)-1, -1, -1):
rev = rev + x[i]

if x==rev:
print("Palindrome")
else:
print("Not a Palindrome")

Qu-4(e) Write a program for Sum of squares of first n natural numbers.


if Input: N = 4
Then Output: 30
12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30
Sol:

n = int(input("Enter Number of Terms: "))


s=0
for i in range(1, n+1):
s = s + i**2
print("Sum of squares of first", n, "natural numbers :", s)

You might also like