0 ratings0% found this document useful (0 votes) 21 views15 pagesPYTHON - Solution - Apr 24 - NSGAcademy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
NSG ACADEMY 1* Floor, Pinaccle Pride,
Contact: 9823782121 / 7276030223 Above Maharashtra Electronics,
Sadashiv Peth, Pune-30
PYTHON PROGRAMMING TYBCS Apr 2024 Exam
Attempt any Eight of the following: [8x1=8]
a) What is indentation? (Chapter — 1)
Solution:
‘+ Indentation refers to the use of spaces or tabs at the bezinning of a Line tolllefine the
structure and hierarchy of the code. Itis crucial for defining blocks of-¢ode, sch as
those inside loops, conditionals, and functions.
Unlike many other languages that use braces °{}* to mark code blocks) Pylon uses
indeatation to indicate whieh lines belong to which block. Pyoper and cnsist@yt
indeatation is required to avoid errors like IndentationEyor.
b) List the features of Python? (Chapter — 1)
Solution
Note: I have written 6 features for 3 marks. For," iilijlswritliny two features.
Python is a popular high-level, general-purpose,programming langage known for its,
simplicity, versatility, and extensive support for vijious progrmming paradigms
Here are some of the key features of Python:
* Easy to Learn and Read: Pythoitthas a simple. efean, and readable syntax that allows
programmers to write code quickly and Withsfewer lines. Its design emphasizes
readability and reduces.tiltscomplekity of writing and maintaining code, making it a
good choice for begifiners.
«Interpreted Language: Python is ai interpreted language, meaning that Python code is
executed line-by-line iy the intespreter at runtime. This makes debugging easier and
climinates\the need for a 8epafate compilation step.
© Dynamically Tyitd: Python is dynamically typed, meaning that we don’t have to
declare the typef a variable explicitly. The interpreter determines the type at runtime.
© Object-Oriented Programming (OOP): Python supports object-oriented programming
paradigms, alloWing developers to define classes and create objects, encapsulate date,
and Usbinheritance and polymorphism.
» Extensive Standard Library: Pythoa comes with a rich standard library that includes
modolés and packages for handling common tasks like file VO, regular expressions,
networking, database interaction, and more. This reduces the need to write code for
standard operations.
4 Cross-Platform Compatibility: Python is platform-independent, meaning Python code
can run on any operating system (Windows, macOS, Linux) without modification.
©) What are break and continue statements? (Chapter ~ 2)
Solution:
i) break Statement:
The break statement is used to exit from a loop prematurely. When break is
encountered, the loop is terminated, and the program continues with the next statement
after the loop.
SURESH AGRAWAL HiNSG ACADEMY 1* Floor, Pinaccle Pride,
Contact: 9823782121 / 7276030223 Above Maharashtra Electronics,
Sadashiv Peth, Pune-30
PYTHON PROGRAMMING TYBCS — Apr 2024 Exam
Syntax:
while condition:
# some code
if condition_to_break:
break
a item in iterable:
# some code
if condition_to_break:
break
fi) continue Statement:
The continue statement is used to skip the current iteralin of a Iobp and move to the
next iteration.
Syntax:
while condition:
# some code
if condition_to_coniitinya:
continue
a # other code,
for item in it@rable:
# some code
if Condition tOleGntinue:
continue,
# othepycode
d) Lispiny two,built-in ex€eptions? (Chapter - 4)
‘Sglution:
Hee are two built-in exceptions:
1) WiexEpror: This exception raised when trying to access an index that is out of range for a
list or other sequence type.
Example:
mylist = [11, 22, 33]
print (mylist[5]) # It will raise IndexError (index 5 is out of range)
2) ValueError: This exception raised when a function receives an argument of the right type
but an inappropriate value,
Example:
a = int(‘hello')
# This will raise ValueError because ‘hello’ can't be converted to
an integer
SURESH AGRAWAL BHNSG ACADEMY 1* Floor, Pinaccle Pride,
Contact: 9823782121 / 7276030223 Above Maharashtra Electronics,
Sadashiv Peth, Pune-30
PYTHON PROGRAMMING TYBCS ~ Apr 2024 Exam
€) What is the purpose of range () function? (Chapter — 2)
Solution:
‘Note: Ihave written examples for 2-3 marks. For 1 mark write theoretical coneepts pny.
The range() function is used to generate a sequence of numbers, which can be itegéted over in loops
or converted into a list.
Syntax:
range(start, stop, step)
where,
start (optional): Specifies the starting value of the sequence (inclusi@j_iphe default value
is 0" if this parameter is not provided.
stop (required): Specifies the end value of the sequeng® (exehisive), Tlipsequence will stop
before this value (it is not included in the output).
step (optional): Specifies the step or increment ben eactilyalye’in the sequence. The
default value is “1” if this parameter is not provigéd. We can ise a b8gative step to generate a
sequence that decreases.
Example:
© for 4 in range(5):
print(i) PBMppyt’: 91234
© for i in range@S, 6):
print (i) ¥ output : 2345
© for i in range(T IRQs):
print Gh # Output: 13579
© for i in refge(10, 2, -2):
print (i) # Output : 108642
£) What does‘thelollowing function return -clock() and gintime(). (Chapter ~ 4)
Solition:
Clock
It returns a flosting point number representing the current processor time in seconds.
*) However, clock() was from the rime module, deprecated in Python 3.3 and removed in
Python 3.8 due to inconsistent behavior across different operating systems
gmtime():
© The gotime() function is a part of rine module in Python.
© Ttreturns a ‘struct_sime” object representing the time in UTC (Coordinated Universal
Time).
SURESH AGRAWAL HiNSG ACADEMY 1* Floor, Pinaccle Pride,
Contact: 9823782121 / 7276030223 Above Maharashtra Electronics,
Sadashiv Peth, Pune-30
PYTHON PROGRAMMING TYBCS ~ Apr 2024 Exam
The gmtime() fanction converts a given time (in seconds) since the Epoch (Jan 1,
1970) into a struct_time object in UTC. If no argument is provided, it uses theurrent
time.
The struct_time object is a named tuple with attributes like m_vear, tm
tm_mday, tm_hour, tm_min, m_sec, and so on.
Example:
import time
utc_time = time. gmtime()
print (ute_time)
Output:
time.struct_time(tm_year=2@24, tm_mon' ‘m_mday=15, tm_hour:
tm_min=49, tm_sec=25, tm_wday=4 dayeg20, An_isdst-0)
2) Compare for and while loop. (Chapter - 2)
Sotution:
for 7 while
‘for loop typically used for fp
‘when iterating over a seq
tuple, string oF range)
rationsipr | whife loop repeats as long as a given
. yy | condition is True.
Syntax:
for item in
Syntax:
while condition:
# code block
Example:
# This will print numbers @ to 4
i-e
while i < 5:
print (i)
itd
n) Deli
text and binary files. (Chapter —4)
lution:
There are two types of files in Python
Binary file: Binary files follows a specific format. It is encoded in the binary format,
which ean be understood only by machine. It can be opened in any text editor but we
cannot read its content though itis binary. For example, images, audios, videos ete
Text file: Text files do not have any specific format. It can be opened in any text editor
and easily readable by human being. For example, html, ess, py, java, txt, esv, ini, json
ete.
SURESH AGRAWAL oHNSG ACADEMY 1* Floor, Pinaccle Pride,
Contact: 9823782121 / 7276030223 Above Maharashtra Electronics,
Sadashiv Peth, Pune-30
PYTHON PROGRAMMING TYBCS ~ Apr 2024 Exam
i) Define list and dictionary. (Chapter — 3)
Solution:
«A listis an ordered, mutable collection of elements.
« Itcan contain items of different data types (such as integers, string of aber lists) and
allows duplicate values.
‘Lists are indexed by which we can access elements by theirsgijsition in the 118
‘Lists are defined using square brackets ‘[]', and the elements ate sepaliad by comma °,.
Dictionary:
«Dictionary is a collection of key-value pairs
* Itisa mutable, unordered, and iterable data type.
«Each key ina dictionary must be unique, aud the Wilues calbe’Or any data type,
including other dictionaries.
«Dictionaries are defined using curly braces)", and the key-value pairs are separated by
colons *:*
4) What is lambda function? (Chapter - 3)
Solution:
‘Note: I have written examples for understgnding, For 1 marks examples are not required.
Lambda function or anonymous function is a function that does not have a name. These
functions are created using th@Jambda Keyword, and they are typically used for small, one-time
tasks, where definilfig full functiifUsing the def keyword might seem unnecessary.
Syntax:
Jambda arguments: dxgFession
Where,
fambda: The keyword used to create the anonymous function.
arguments list of parameters (just like in normal functions).
taxpression: The single expression that the function evaluates and returns. Unlike normal
functions, a lambda function can only contain one expression and does not
require a retum statement.
Exanjple:
#Mambda function to find the square of a number
square = lambda x: x ** 2
print(square(4)) # Output: 16
Example:
# Lambda function to check if a number is even
is_even = lambda x: x % 2 == @
print (is_even(4)) # Output: True
print(is_even(7)) # Output: False