5 Session Python Fundamentals Part 2
5 Session Python Fundamentals Part 2
STRING LITERALS
Q. Find the length of the following strings:-
S1=‘\\’
S2=‘abc’
S3=‘Sita\’s pen’
S4=“Anita’s book”
S5=‘a\
b\
c‘
S6=“A\
B\
C\
D”
S7=‘’’A
B
C
D’’’
PYTHON FUNDAMENTALS
OUTPUT
PYTHON FUNDAMENTALS
STRING LITERALS
Note:-
•Triple quoted strings also count EOL characters in the size of the string.
•Single /double quoted strings typed in multiple lines with \ at the end
of each intermediate line do not count \ in the size of the string.
NUMERIC LITERALS
>>> x=46
>>> y=0o56
>>> x
46
>>> y
46
>>> x+y
92
>>> z=0O14
>>> x+y+z
104
>>>
PYTHON FUNDAMENTALS
NUMERIC LITERALS
CONVERSION
NUMBER REMAINDER
46/16 14-(represented as E)
Ans:-
2 2
(46)10 = (2E)16
0
PYTHON FUNDAMENTALS
NUMERIC LITERALS
CONVERSION
NUMBER REMAINDER
58/16 10-(represented as A)
3 3 Ans:-
0 (58)10 = (3A)16
OUTPUT:- OUTPUT:-
>>> a=58
>>> x=46 >>> b=0X3a
>>> y=0x2E >>> a==b
>>> x==y True
True >>> a
>>> x 58
46 >>> b
>>> y 58
46 >>> a+b
>>> 116
>>>
PYTHON FUNDAMENTALS
NUMERIC LITERALS
a. Fractional Form:-
A real constant in fractional form having at least one
digit with the decimal point either before or after.
Eg:- 2.0, -13.5, .3(0.3), 7.(7.0)
.
b. Exponent form:-
It consists of two parts: mantissa and exponent.
eg:- 5.8 can be written as 0.58X101 = 0.58E01 (where
0.58 is the mantissa and exponent part is 1)
0.058X102 = 0.058E02
0.0058X103 = 0.0058E03
PYTHON FUNDAMENTALS
NUMERIC LITERALS
Floating point literals: representation:--
b. Exponent form:-
OUTPUT:-
>>> x=5.8
>>> y=0.58E01
>>> x==y
True
>>> z=0.058E02
>>> x==z
True
>>> x
5.8
>>> y
5.8
>>> z
5.8
>>>
PYTHON FUNDAMENTALS
NUMERIC LITERALS
Floating point literals: representation:--
b. Exponent form:-
OUTPUT:-
>>> 152E05
15200000.0
>>> 1.52E07
15200000.0
>>>
NOTE:-
In Numeric values commas are not allowed. (It will be considered a tuple)
PYTHON FUNDAMENTALS
BOOLEAN LITERALS
Eg:-
>>> x=True
>>> y=False
NOTE:- In PYTHON True means 1
>>> x and False means 0
True
>>> y
False
>>> x==1
True
>>> x==0
False
>>>
PYTHON FUNDAMENTALS
None LITERAL
It’s a special literal of PYTHON which means that the variable has no value.
Eg:-
>>> x=None
>>> x
>>> print(x)
None
PYTHON FUNDAMENTALS
OPERATORS
Operator are tokens that trigger some computation/ action when applied on
variables or other objects in an expression.
CLASSIFICATION:-
1. Arithemetic operators: -
+
-
*
/
%
**
//
PYTHON FUNDAMENTALS
OPERATORS
1. Arithemetic operators: -
Examples:-
>>> x=10
>>> y=20
>>> z=30
>>> x+y # addition
30
>>> x-y # subtraction
-10
>>> x*y #multiplication
200
>>> x/y #division( answer is always float)
0.5
>>> y/x #division
2.0
>>> 5%2 # Modulas
1
>>> 15%4 # Remainder
3
PYTHON FUNDAMENTALS
OPERATORS
1. Arithemetic operators: -
Examples:-
>>> 5//2 # floor division quotient (both operands are of type int)
2
>>> 5.0//2 #floor division (one of the operand is float)
2.0
>>> 5//2.0 #floor division (one of the operand is float)
2.0
>>> 3**4 # exponent (raise to power)
81
>>> x=2
>>> y=5
>>> x**y
32
PYTHON FUNDAMENTALS
OPERATORS
2. Relational operators:
>
<
>=
<=
==
!=
3. Logical Operators:-
and
or
PYTHON FUNDAMENTALS
OPERATORS
2. Relational operators(Examples:-)
>>> x=1
>>> y=2
>>> z=3
>>> x==1 #equal to
True
>>> x==y
False
>>> y>z # greater than
False
>>> y>=2 # greater than or equal to
True
>>> y!=x # not equal to
True
>>> >>> z<=y #less than or equal to
False
>>> z<2 # less than
False
>>> 5==5 # equal to
True
PYTHON FUNDAMENTALS
OPERATORS
3. Logical operators( Examples:-)
>>> x,y,z=10,20,30
>>> x>=y and y<=z #logical and
False
>>> x>=y or y<=z # logical or
True
>>> x==10 or y!=20 #logical or
True
>>>x<=y and y<=z and z<=x #logical and
False
>>>x<=y and y<=z # logical and
True
>>> not(x<=y and y<=z) #logical not
False
>>>
PYTHON FUNDAMENTALS
OPERATORS
4. Assignment operators
= Assignment
+= Assign Sum
-= Assign difference
*= Assign product
/= Assign quotient
%= Assign remainder
//= Assign floor division
**= Assign exponent
PYTHON FUNDAMENTALS
OPERATORS
4. Assignment operators( Examples:-)
>>> x=10
>>> y=2
>>> z=3
>>> a=40 # assignment operator
>>> x+=5 # assign sum x=x+5
>>> x
15
>>> x-=3 #assign difference x=x-3
>>> x
12
>>> x*=2 # assign product x=x*2
>>> x
24
>>> x/=y #assign quotient x=x/y
>>> x
12.0