Iot Unit 3 Decode
Iot Unit 3 Decode
'
. Introduction to Python
~-1 : Introduction to Python I • Python supports the use of modules and packages,
which means that programs can be designed in a
1 WJaat .. P,tbon ? modular style and code can be reused across a
Q. • Python is an object - oriented, high - level
variety of ·projects.
~ ~age with integrated dynamic
,einantic:s primarily for web and app development. • Python can also be used to process text, display
· numbers or images, solve scientific equations, and
Q.2 u,t and aplaln featma of Python. save data.
AJ1J- : Featmes : • Python is an interpreted language i.e. interpreter
1. Python is a simple and_minimalistic language.
2. Easy to Leam.
. .
executes the code line by line at a time.
• Due to its open- source nature, Python has been
3. Free and Open Source. ported to many platforms. .All your Python
4. Python supports procedure - oriented programs can work on any of these platforms
programming as well as object- oriented without requiring any changes at all if you are
programming. careful enough to avoid any system - dependent
5. Extensive Libraries : The Python Standard features.
Library is huge indeed. • ~ou can use Python on GNU/Llnux, Windows,
6. Embeddable : You can embed Python within FreeBSD, Macintosh, Solaris.
your CIC++ programs to give scripting • Python uses garbage colledion, the ~ e r
r,
capabilities for your program's users. . doesn't have to worry about memory leaks (failure
Q.3 List the beneflu of Python. to delete) or dangling references.
Ans. : • Python can be used to develop prototypes. · • The Python language environment is reasonably
• Python allows for a more productive coding secure from tampering. Pre- compiled python
environment than massive languages like C# and modules can be distributed to prevent altering the
source code.
Java.
• Python powers Django, a complete and open source Features of Python :
web application framework. 1. Cross-platform language
• Most automation, data mining, and big data 2. Python is a open source programming language
platforms rely on Python. 3. Object - Oriented langpage
• Python supports modules and packages, which 4. Extensive Libraries
encourages program modularity and code reuse.
5. Python provides interface to all commercial
Q.4 Write ehort note on Python. databases.
Ans. : • Python is an object - oriented, high - level
rogramming language. Python is a simple and
alistic language.
(3 - 1)
,,,_ , of'11dl«r
If - •llll!'Nalon :
,
I
e container
....
Body of If
:
: • Pyth on data type s are as follows : • Python dictionary is an 8SSOCiativ Body of alae
An,. r O:.h ich
1. Num bers : it stores num
eric valu e. In Pyth on # list of Integers permits access base d on a key, rathe an • The ii.,eJse statement evaluates test expression and
have integ er mdex. will
body of ii only when test cond
ition .
prog ramm ing lang uage, we my_list "' (1, 2, 3) Tirue.execute is
poin t num bers , and complex t, a dicti
numbers, float ing • A dictionary is a muta ble objec 50
# list with mixe d data type s anot her dicti onary objectonary .
num bers. cann ot use itseU or • U the condition is False, body of else is executed
eric objects : = (1, 'Hello', 3.4) y may be any v~: Inde n s. .
• Pyth on supp orts four type s of num my_list key. A valu e with in a dictionar . tation is used to separate the block
Jong integ ers, float ing poin t num bers , and , imm utab le or muta ble· .
c. For : The for loop in Python
integ ers, of .._P ie. Pyth on type is used
complex num bers. Q.8 Deecrlbe D~ and tupl e In tam e ies : over a
to iterate
exam ple : . • To create Pyth on dicti onar string) or other
2. Strings : it is list of characte
rs in orde r. Strin g Ans. : List and tupl e prog ram iterable ~ (list,_ tuple ,
.
emp ty string. # Mate 8 llat #l/u8 r/bln /pyth on3 -,.,.. .... Iterating over a sequence 19
with zero characters is calle d an my_list = (1, 2. 3, 4, 6, 6, 7)
called trave rsal .
gene ral mec hani sm for (1, 2, 3, 4, 6, 6, 7) # 11,fua 8 tu; la # crea te_d lct.p y
3. Lists : Lists prov ide a my_tuple-=
inde xed by a # Print tbe !lat • Syntax of for Loop.
stori ng a collection of objects print('The list: ', my_list) ': 'Mo nday '}
on. To crea te an emp ty list, use # Print tbe tuple wee kend = { 'Sun ': "Sumiay', 'Mon for Val In aeq ue,- :
num ber in pyth prin t('lb e tupl e 1, my:-tup le)
squa re brackets with no elem ents in betw een
ent In the list :', my_list(Oi) - val8 • dict( one = 1, -two = 2) Body of for
prlnt('The first elem
the a1u ·
them(□). #A cce aaa ne~ • Here, val is the variable that takes v e of the
h like lists, exce pt capJtal& • {} item insid"
4. Tuples : Tupl es are veey muc prlnt('The first elem ent In the tupl e:', my_tuple(OI) _· e the sequence on each iteration.
renc e. capltal8['svk'J • 'Bratl&lava'
# Acc ess 11D ~ismant
for one impo rtan t diffe
t.
its elem ents can' t be :', end =·• ') capltalllj"deu'J = "Beilin' Q.11 WiHa •boat bnal r/co atma a atatamen
• Onc e a tupl e is created, print('All the elem ents in the list . be used in eith
that a tupl e is caplt al8l' dnk'J • 'Copenhagen• Ana. : • The continue statement can to
mod ified in plac e. Kno wing for elem in my_list : s to instruct pyth . er
le, pyth on can be mor e efficient in a !lat for or while loop. on continue on
imm utab # Itera te over the elem ents of to the .
d • { i: object() for t in range(4)
} executing the
man ipul ating tupl es than lists. next iteration of the loop without
prlnt{elem, end ='') follow the contin
• Like lists; the cont ents of tupl
es are arbit rary . The statements in the loop which ~
renc e is that lists are muta ble and tupl es prin t() print (wee kend ) state men t
only diffe e:\ end ='')
prlnt('All the elem ents in the tupl prlnt{vals) the loop variable is
are not.
tuple : • ~ a for loop, the value of
are very simi lar to for elem in my_ print{capitala} . after a cont inue state ment, so that after
5. Dictionaries : Dictionaries a tuple mcre men ted
trary objects and # Iterate over the elem ents of print(d) the loop for the
lists in that they can cont ain arbi skip ping the rema ining part of
h, but, inste ad , print(elem, end= ' ') in four different . itera tion will be
can be nest ed to any desi red dept • In the example, four dictionaries current itera tion, the · next
by integ ers,· they can be ,II prin t() Later we prin t the cont ents of these performed in the normal fashion.
of bein g inde xed ct, such as strin gs # Slice a list way s.
inde xed by any imm utab le obje print('List slice:', my_llst(2:6I) dicti onar ies to the console. be used as an
or tuple s. ) # Slice a tuple • Often a continue statement can
prln t(Tu ple slice:', my_tuple(2:6I "Mon•: "Mo nday •} e a loop, and it's
the first elem ent in the llst . . •') · weellend .= { "Sun•: "Sun day• , ~tem ative to an if statement insid
/ prlnt{'Try to mod lfy renc e as to which one
between Um and tuplea. "1st a matter of personal prefe
Q.e &plaJa cllfference
Ans. : • Lists are deno ted by
square brac kets , i
I
lllY_list(OJ = 9
print('The list:', my_llst)
# Mod tbe list
ify"
[3.3 :Control of Flow / is used.
pare nthe ses. to find the largest
whe reas tupl es are deno ted by • For example, suppose we wish
of foU ~ control of a- :
ent in the llst . . ,'),1
• .lists are mut able whe reas tupl
es are imm utab le. 1' prlnt{'Try to modify Uie first elem
,
Q.10 :-i:;a1a IYDb.tuIf........ c. for number in an array , as well as the index within the
rred.
I' my_tuple[O) = 9 . array at which the largest v$e
occu
after its crea tion # Is tupl e modlflcation possible?
• .list can be chan ged or mod ified can' t be chan ged Ans. : a. H : Pyth on if Stat eme nt Synt
ax
we could use the
acco rdin g to need s . whe reas tupl e
print('The tupl e:', my_tuple)
' • Using a continue statement,
or mod ified afte r its crea tion.
lfta etup reu 1on : following:
I -~
tion than that of tupl e.
Q tlon u,. · , •tat.ment(a)
• Lists has mor e buil t - in func Write abort note on Python clJc » X • (7, 12, 92,, 19, 18 ,44, 31)
test expression and will
size than that of list, aine r of key-value • The prog ram .eva luate s the » xmax • x(OJ
• Tup les oper atio n has smar.ller Ans. : • Pyth on dict iona ry is a cont
~ -• or~T70NS, ._
T •TECHMCAL ,..._,~ An up thrust for knowledge
• 2~!!____ ____ _____3 _-6;--_ _ _ _ _
~~'!fl11btp
_ =~=':::::uctton
~- ---~& ~
~~- · Jn~'!fTlt'!':_~
- ;otlc flow of Contro l: Program mers can also -
use
3-7
- : - •
- _
-Co m _p -
_lex - -
e _
lem_
en -
ts - - -cons
are
~toP ytlu m
~ ~
-=cted:..:::~from=~.!:other
- -tru
!!!-'of'
• These exrepti ons are process ed ll8ing five elements hierarchically, wherea s simple demen ts
• e,cceptions as a basis for implementing unusua l
contain data values. Within an XML document,,
•Examp le : stateme nts. These are : control flow. Slnre 't here is no 'go to' statement in
respect . each element needs to be marked (or tagged) in
c.... Pa,.nt : 1. try/exr ept : catch . the error and recover · &o Python so exrepti ons can help In this
some manner similar to HTML XML elementstion, are
def _1nit_( 1elf. a): exrepti ons hoist by progra mmers or Python i ~ cka made up of a start tag, an end tag and Informa
3.7: Python Pa ges • JSON, XML, HTTP
1e!La • a 2. try/finally : Whethe r exception occurs or not; 1 Ub, URL Lib, SMTP Lib or content between them.
automa tically perform s the clean-u p action. t
conditi onally in th • An XML docume nt may also be valid. A valid
assert : triggers an excepti on
def method l(nlf): 3.
code. · , e Q.23 What 11 JSON ? docume nt is · checked against either a Document
Type Definition (OTO) or a schema. The followi
ng
pr!nt{n!La*2) Ant• : JavaScript Object Notati ~ (JSON) is_ a
def method2(aelf): 4. raise : manual ly trigger s an excepti on in the code. lightweight data - interch ange format based on the is a very simple XML docum ent
prlnt(nl f.a+'lll1 5. with/as : implem ent context managers in older syntax of JavaScript objects. It is a text - _based, <'?Janl V9I8lon =·t.O' ?>
olua Chlld(P anint): version s of Python such as - Python 2.6 &: human - readabl e, languag e - indepe ndent format for · < addre11 >
Python 3.0. <name>Rakabita Dbotm </name >
representing structu red object data for easy
a. b): Q.21 Why are Eiu:eptlona lJNd ? transmisSion or saving. <email > [email protected]<ma</email>
def _lnlt_( aelf,
<pbone >020-4& :6789</ pbDne>
n!La = a Ans. : • Exceptions allow us to jump out of random
Q.2' Why to med JSON ? <birthd ay> 1993-07 -13</bl tthday>
aelf.b =b illogical large chunks of codes In case of errors. Let
Ans, : • Compa red to its predece ssor In_server - client </addr eas>
def instbod l(aelf): us take a scenario that you have given a function to ,·
communication, XML, JSON is much smaller • The first line is a pro<:essing instruction. It begins
print(ae!La*7) do a specific task. rs, and better
translating Into faster data transfe with '<r and indicates the version of xml that is
• U you go there and found those things m,issing· µtat used In the docum ent
experiences.
are require d _to do that specific task, what will you
def method3(aelf):
do? • JSON exists as a "sequen ce of bytes" which is very TFN Structll,. :
print(ae!La + aelf.b)
- useful in the case Vo{~ need to transm it (stream) data • An XML docume nt exhibits a tree structure.
It has a
p = Panmt('bi1 • Either you stop workin g or think about a solution over a networ k. single root node; <address> In ~ exampl e above.
c = Chlld('bi', 'bye') where to find those items to perform the task. -The - . The tree is a general ordered tree. There is a first
print('P arent method 1: ', p .method l()) same thing happen s here in case of Exreptions aiso. ....,,, 1,
• JSON is also extrem ely human - friendly since it is
child, a next sibling etc.
·. textual, .and simulta neously machin e - friendly .
print('P arent method 2: ', p.method2()) • Exrepti ons allow program mers jump to . an • Nodes have _parents and children. There are leaf
exrepti on handle r In a single step, abando ning all • JSON has expressive syntax for rep~t ing arrays, nodes at the bottom of the tree. The declaration at
functio n calls. You can . think exceptions to an
I
objects, numbe rs and boolean s. the top is not part of the tree, but the rest of the
print()
optimi zed quality go - to statement, In which the document is part of tree.
Q.25 write abort note on XML.
progra m error · that occurs at runtim e gets easily
managed by ·the excepti on block. Ans. : XML Document • In the tree representation, intei;µal nodes represent
print('Chlld method 1: ', c.metbo dl())
complex elements, whereas leaf nodes represent
print('Chlld method 2: ', c.metbod2()) • When the Interpr eter encoun ters an error, it lets the • XML docum ents are text based. The basi~ object in
simple elements. That is why· the XML model is
printfC hlld method 3: ', c.metbod3()) executi on go to the ·e xceptio n part to solve and XML is the XML docum ent. XML documents,
called a tree model or a hierarc hical model.
continu e the executi on instead of stoppin g: Including XH1M L ones, must be ·well - formed .
Q.20 What le Except ion lwaclliag In Python ? These documffl.t is a labeled, unrank ed, ordered · • An XML document can be modeled as an ordered,
Q.22 Explain rola of an Except ion Handle r
In tree: . labeled tree, with a document node serving as the
Ana. : Python has many built - in exreptions which
Python . root node.
forces your progra m to output an error when 1. Labeled _means that some annotation, the label,
someth ing in it goes wrong. Ana. : • Error handlin g : The exrepti ons get raised is attache d to each node. · • An XML document can be modeled as an ordered
whenev er Python detects an error In a progra m ·at labeled tree. Each node in this tree corresponds to
• When these exrepti ons occur, it causes the current 2. Unrank ed means that there is no a priori an element in the document and is labeled with the
process to stop and passes it to the calling process runtim e. bound on the numbe r of childre n of a node. element tag name. Each edge In this tree represents
to j
until it is handled . U not h1111dled, our progra m will - • Event Notification : Exceptions are also used 3. Ordere d means that there is an order between inclusion of the element corresponding to the child
suitable conditi ons &: then passing result
crash. signal . the children of each node. node under the element correponding to the parent
flags around a program &: text them .explicitly. node in the XML file.
• For exampl e, if functio n A calls function B which in • There are two main structu ring concepts that. are
• Terminate Execution : There may arise. some used to constru ct an XML doewne nt : Elements • Now <name> has two children and <birthday> has
turn calls functio n C and an exrepti on occurs in
problem s · or errors in progra ms that it needs a · · three. Most processing on the tree is done with
a
functio n C If it is not· handle d in C, the exrepti on and Attributes.
' termination. So try/finally is used that guaran tees Attribu tes in XML provide additional preorder traversa l. One way to view the tree is
passes to B and then to A. ~t ~osing - time operati on will be_perform ed. The
o
shown in Fig. Q.25.1
inform ation that describes elements.
• In Python , exrepti ons can be handle d using a try with stateme nt offers•an alterna tive for objects that
suppor t it. _
statem ent T'TECHNICAL PUBLICATIONS' ·. An up
T.
1hnJ' 1 tor knowledge
-
-
Q.1
Q.3 ·--·--·
----··
00Jl\pilation
null
unmutable
Q.2 list
Q.4 minimalistic
Q.6 associative uray
--
Q.5
Q.7 hashing Q.8 keyword
day
-
Q.9 libraries
/ Fill In the Blanks f~r Mid Term Exam I [!l JavaScript Order No~tion