Professional Documents
Culture Documents
D
ESSAY QUESTIONS WITH SOLUTIONS
1.1 PYTHON BASICS
Python? Explain the features of
Python language
1.3 NUMBERS
Introduction to Numbers, Integers, Floating Point Real Numbers, Complex Numbers
scuss different types of numbers in python.
wer : 020
umbers
Numbers are of immutable type i.e., when the value of a number is changed a new object is allocated also
storage and direct access.
tingNumber
A number is created by assigning a value to a variable.
PYTHON PROG
1.16 RAMMING
4. Complex Number (JNTu
Syntax
var value A complex number
Example real number and imaginary is an
nurnl%frred Dao
aFloat •e2.3497 Syntax
Upd•ting a Number real + imagj
Existingnumber can be updated by reassigning a Here j •t imaginary
can be a different
variable to another number. Il)e new value number
number or related to previous value.
Example
Example 24.37j, 22 + 3.4j
aComplex aComplex + 2
'I .3.2 Operators,
Deleting a Number Bunt-in
Generally a number can not be deleted but, its usage can Q26:Zbescribe different
bc stopped. However, it can be explicitly deleted by using del by Python with antypes of o
example
statement once the variable name is removed, it is no longer Answer: for each.
used. In case, if it is used then it results an "NameError"
There are three different
exception. types of
by python. They are listed
Example below, operatotl
1. Arithmetic or
del aComplex numeric
2. Comparison operators
Number Types.
3. Boolean operators.
Python supports the following number types,
1. Arithmetic or Numeric
l. Plain integer Operators
The numeric type arithmetic
2. Long integer
by python are listed below, operators
3. Floating point real numbers ate
(i) Division (l; V)
4. Complex numbers.
(ii) Modulus (%)
1. Plain Integers
(iii) Exponentiation (**)
In Python, plain integers correspond to universal numeric
type. The range of integers is —2,147, 483, 698 to 2, 147, 483, (iv) Addition (binary (+)
operator)
647 i.e., -2" to 2 31- 1. (v) Subtraction (binary
(—)operator)
Example (vi) No change (Unary(+)
operator)
89, -- 169,-o x 43, 017, 0101 (vii) Negation (Unary (—)
operator).
2. Long Integer (i) Division(/, //)
Long integersare representedas (L) or (l). They are There are three different types
superset of integers and are used when the range of number of division
exceed —231range. (a) Classic division
Those can be expressed in octal, decimal or hexadecimal (b) True division (l)
notations. (c) Floor division (//).
Example (a) Classic Division
312L, 249L In the Classic Division Operation,the
quotientn
3. Floating Point Real Numbers obtained will have the same type as thatofthecp
Floating point real numbers are represented in straight involved. That is, if integer type operands
areimi
forward decimal or scientific notations. In decimal notation, then the classic division operationretumsan•
decimal point is used while in scientific notation, 'E' or 'e' is by truncating the fraction. Similarly,forfloating
used. operands, it returns the actual floatingpointqa
Positive or negative signs betweenthe "e" and the Example
exponentrepresentsthe sign of the exponent.If sign is not 25/2
present, then it refers positive exponent.
12
Example
>>> 25.0/2.0
1.28, -2.204e -i9
12.5
'
(0
' Division
Dis type
€søøple
200. poc
•ereaat•• or tot
rte.* tutore
asn
i2.s
as.0/2.o
ta.s
Division (Il)
smallest
rounded to the
IntheFloorDivision Operation, the fractions obtained in the quotient are always truncated and
on its immediate left on the number line.
wholenumber which is
Esgmple
Fie Shea
txsc bit
python 3.7.4 (tags/v3.1.4:e093S9i12e, cui e 2019,
on Win32
type "heip•, •copyright•, •czedizs• o: •license O • tor ure intonation.
2S//2
as.0//2.o
12.0
(i) Modulus(%)
Thecomputationof modulus is different for different numeric types.
+ The modulo of two integer numbers is the remainder of their integer division.
Example
170/03-2
+ The modulo oftwo floating point numbers is obtained using the following formula.
dividend
dividend— math.floor * divisor
divisor
+ The modulo of two complex numbers is obtained using the following formula.
dividend
dividend— math. floor real* divisor
divisor
Exponentiation
numeric type. The binding between the exponentiation operat
TheExponentiationOperation, determines the power ofa
andits left operand is more tighter than that with unary operators (+, —).Whereas, the binding between the exponentia
that with unary operators (+,
Operator and its right operand is less tighter than
JOURN8L FOR ENGINEERINGSTUDENTS
8LL-1N-ONE SIA GROUP
PYTHON PROGRAMMING
1.18 tJNTu
Example
S. 3.4 (NSC
binding VS th •e than
-32
•ore
list of all numeric arithmetic operators in the highest-to-lowest order of precedence is given below.
S.No. Operator Syntax Function
2. Comparison Operators
These operators compare only the data values of the participating objects. The comparisons performedarebased
sign and magnitude for numeric values, whereas a lexicography (i.e.,) alphabetical order is followed, in writing
strings. The comparisons result in either Boolean True or False values.
The list of standard type value comparison operators in highest-to-lowest precedence order is givenbelow,
Operator Syntax Determines whether
expl < exp2 expl is less than exp2
Look for the SIA GROUP LOGO on •the TITLE COVER beforeyou buy
1 pytttor uoncos
1.19
a-on,
•i
True
t rue
true
BJßtple
EM Debug Options
Window Help
python
t 2019,
"help", •copvnght•, €4 •
or
tor nor.
True
Operator
Boolean
Theseoperators negate or link two or more expressions.
Thelistof standard type boolean operators in highest to lowest precedence order is given below:
Operator Syntax
Determines
not not exp negation or logical NOT
of exp
and expl and exp2 conjunction or logical
AND of expl and exp2
or expl or exp2 disjunctionor logical OR
of expl and exp2
tumples
Shea
Debug Help
3 • 1.4 (HSC 64 •
on
type *hexp•, •credit'" or for intorn•eson.
n.n—s.S9,
03.02
True
not
True
(0<300)
True
(03.02) and (0>300)
true
nov€n O)
Python's triple quotes are generally helpful while writing a large block of HTML
or SQL.
Example
#HTML
strings
back =
Examf
Home Page
HI ! USER
•OX2D9'
'OX2d9•
'QC"". 92585942
•SS. 92ses9'
'642 •'56.92ses942
•S6.92S8S9'
•"6.92ses942
•u •esc. 92525942
•s. 692se6E+01
'S6.92S9'
scored id
'rose scored 90 marks'
ers and
Sequences
string Templates 1.29
provides string Templates using
into the string module, wh,ch.
the two methods string substituting is very
substitute( ) and much simplified. When the Template module
of the substitution and safe_substitutecan be used with templateobjects.
leaves the
substitution of missing
gomple
key as it is.
Fie Edi
python
(tag'/
Jul e 2019,
.tvpe •help s , tysc 7.1916 64 bit •
•copytxght•.
•eredit.• or
tot more
mathot SVhiChzcnth
has
•the month ot ,ho•any•• d"" •)
has 31 day" • •)
'I.SUb'titute
Traceback recent c.ii :
line 1, In
•substitute
(Which—onth•
•nareh•
return
•eit.pattetn.sub(convert,
seit.teqiate)
return
hoe
(nappingtnaedl)
ReyEtxor: •ho.any•
si.sate substitute
•the ot Hatch hag )
$ho•any days•
Syntax
r' any_string'
(or)
R'any_string'
Example
• txsc •
••eopvrafie• tor •ntor—eaen.
r•.ene
tErrno •ucb or
Pyc.bon\
S . •
L2•t11, t'vv•,
pnnt (L2)
•t•vv', 'dd'J, 3.5)
list (•sure')
•wed'
Lito:2J
t •nn% •cue')
Alt0)c'xxx'
S» pl.append( id')
pi
Odel•ce•entire
esas'cs, Numbers
and Sequences
1.37
1.5 MAPPING
AND SET TYPES
Whatare dictionaries? Explain with
examples.
picn00 ModelPaper-I,Q3(b)
Dictionariesrefer to python's mapping
type. are
entry is key : value. Keys correspond
python object. to any python similar to mssociativearrays, or hashes in perl. The syntax of a
arbitrary Dictionaries are type and it is usually
numbers or strings. Whereas, values correspond
enclosed by curly
Dictionariesare created by simply
assigning a dictionary
or not. The syntax is as follows.
elements to a variable, without considering whether dictionary contains
: 'CYR' , 'port'
:100}
dicta, dittb
('nee': 'Jack', ipo:t•:
100))
{dict
ddict
edict—I}
(' , 'bar'))
edict
('too': Jone, 'bat': None)
00
running 90,
tv dieuonacy two items, The keys dictb ore 'onme' and •port'
onci'90' and Its
if through Key is not in the dictionary, then e
rror occurs.
Whichi'
tine).
: Sni
in the jibove example. key 'SOC ih not present in the dictionary 'dictb', Ilence error
key( ) jnethod, which whether has key or not.
and not in operators ore boolean. that i", they return true it' key exists in the dictionary
otherwjsc returns
in diet // or key('nojnc')
•Ratnnna'
•rhe 10110wingexample shows the key os combination of numbers and strings.
»•>djeic121 •s 'Shiva'
*diete14.91'abc'
:rue
true
Eaiae
DictionaryKeys
Thedictionary values does not have restrictions imposed on them. Any python object ranging from standard objects to user
Objectscan be dictionary value. But its not same for dictionary keys. The properties of dictionary keys are as follows,
&fined
Morethan one entry per key not allowed one major constraint is for each dictionary key only one value is allowed rather
thanmultiple values for same key. If there are any key collisions, the assignment that is done at last will be fixed.
3.7A
•Debug
Python 3.7.' cysc v.
Peopyzaqht•,•credit'"or O• ere
I •nexxø•: •abe•i
dit
Pythonwill not check for any key collisions because it needs memory •forall the key-value pain
2. Keysmust be hashable
The python objects can be used as keys and they must be hashable objects. The mutable types like lists and dictionaries
are supported by Python because they cannot be hashed. All the immutable types can be used as keys because they are
hashable. Certain mutable objects are hashable and they can be used as keys.
The key need to be hashable. The hash function that is used by interpreterfor computing the iocation to store the data
dependsupon key value. The value is modifiable when key is mutable object. The hash function can map to a different
locationif the key is modified. Therefore, the hash function can never reliably store or retrieve the associated value. The
hashable keys can be selected because their values cannot be modified.
RLL-IN-ONE JOURN8L FOR ENGINEERING STUDENTS
SPECTRUM SIA GROUP