0% found this document useful (0 votes)
3 views23 pages

5 Session Python Fundamentals Part 2

The document covers Python fundamentals, focusing on string, numeric, boolean literals, and operators. It explains the different types of string representations, numeric types (integers, floats, and complex numbers), and their conversions. Additionally, it details arithmetic, relational, logical, and assignment operators with examples.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

5 Session Python Fundamentals Part 2

The document covers Python fundamentals, focusing on string, numeric, boolean literals, and operators. It explains the different types of string representations, numeric types (integers, floats, and complex numbers), and their conversions. Additionally, it details arithmetic, relational, logical, and assignment operators with examples.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PYTHON FUNDAMENTALS

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

int( signed integers)


any positive or negative whole number.
float (floating point numbers)
represents real numbers with decimal
complex (complex numbers)
a+bJ , where a is the real part of number and b is the
imaginary part.
PYTHON FUNDAMENTALS
NUMERIC LITERALS

int( signed integers)


any positive or negative whole number.

float (floating point numbers)


represents real numbers with decimal

complex (complex numbers)


a+bj , where a is the real part of number and b is the
imaginary part.
PYTHON FUNDAMENTALS
NUMERIC LITERALS

int literals representation:-

a. Decimal integer representation


eg:- 123, -45,9,-607, -12,2,4,0

b. Octal integer representation


A sequence of digits starting with 0o(zero then letter o) is taken
as an octal integer.
eg:- decimal integer 8 can be written as 0o10 as octal integer
decimal integer 12 can be written as 0o14 as octal integer
PYTHON FUNDAMENTALS
NUMERIC LITERALS

Integer literals representation:-

a. Decimal integer representation(0 -9)


eg:- 123, -45,9,-607, -12,2,4,0
b. Octal integer representation(0-7)
A sequence of digits starting with 0o/0O(zero then letter o/O)
is taken as an octal integer.
eg:- decimal integer 46 can be written as 0o56 as octal integer
decimal integer 12 can be written as 0O14 as octal integer
CONVERSION
NUMBER REMAINDER
46/8 6
5/8 5 Ans:-
0 (46)10 = (56)8
PYTHON FUNDAMENTALS
NUMERIC LITERALS
CONVERSION
NUMBER REMAINDER
12/8 4
1/8 1 Ans:-
0 (12)10 = (14)8

>>> x=46
>>> y=0o56
>>> x
46
>>> y
46
>>> x+y
92
>>> z=0O14
>>> x+y+z
104
>>>
PYTHON FUNDAMENTALS
NUMERIC LITERALS

c. Hexadecimal integer representation(0-9,A,B,C,D,E,F)


A sequence of digits starting with 0x/ 0X(zero then letter x/X) is
taken as an hexadecimal integer.
eg:- decimal integer 46 can be written as 0x as hexadecimal integer
decimal integer 12 can be written as 0X14 as hexadecimal integer

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

Floating point literals: representation:--

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

A boolean literals in PYTHON is used to represent one of the two values


i.e. True or False

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

You might also like