CHAPTER 8 Data Handling
CHAPTER 8 Data Handling
HANDLI
Chapter -
NG
3
int
float
complex
Example:
w=1 # int
y = 2.8 # float
z = 1j # complex
integer : There are two types of integers in python:
⮚ int
⮚ Boolean
Example:
x=1
y = 35656222554887711
z = -3255522
⮚ Boolean: It has two values: True and False. True has the value 1 and False has the
value 0.
Example:
>>>bool(0)
False
>>>bool(1)
True
>>>bool(‘ ‘)
False
>>>bool(-34)
True
>>>bool(34)
True
float : float or "floating point number" is a number, positive or negative, containing one
or more decimals. Float can also be scientific numbers with an "e" to indicate the power
of 10.
Example:
x = 1.10
y = 1.0
z = -35.59
a = 35e3
b = 12E4
c = -87.7e100
complex : Complex numbers are written with a "j" as the imaginary part.
Example:
>>>x = 3+5j
>>>y = 2+4j
>>>z=x+y
>>>print(z)
5+9j
>>>z.real
5.0
>>>z.imag
9.0
Real and imaginary part of a number can be accessed through the attributes real and imag.
▪ Python allows for either pairs of single or double quotes. Example: 'hello' is the same as
"hello" .
▪ Python does not have a character data type, a single character is simply a string with a
length of 1.
▪ String is immutable data type means it can never change its value in place.
⮚ List
⮚ Tuple
⮚ Set
⮚ Dictionary
3.2 MUTABLE & IMMUTABLE Data Type:
These are changeable. In the same memory address, new value can be stored.
Example: List, Set, Dictionary
These are unchangeable. In the same memory address new value cannot be stored.
Example: integer, float, Boolean, string and tuple.
RESULT
OPERATOR NAME SYNTAX
(X=14, Y=4)
+ Addition x+y 18
_ Subtraction x–y 10
* Multiplication x*y 56
// Division (floor) x // y 3
% Modulus x%y 2
RESULT
OPERATOR NAME SYNTAX
(IF X=16,
Y=42)
False
> Greater than x>y
True
< Less than x<y
False
== Equal to x == y
True
!= Not equal to x != y
False
>= Greater than or equal to x >= y
True
<= Less than or equal to x <= y
X Y X and Y
False False False
False True False
True False False
True True True
X Y X and Y
false false X
false true X
true false Y
true true Y
In an expression X and Y, if first operand has false value, then return first operand X as a
result, otherwise returns Y.
>>>0 and 0
0
>>>0 and 6
0
>>>‘a’ and ‘n’
’n’
>>>6>9 and ‘c’+9>5 # and operator will test the second operand only if the first operand
False # is true, otherwise ignores it, even if the second operand is wrong
X Y X or Y
false false Y
false true Y
true false X
true true X
>>>0 or 0
0
>>>0 or 6
6
>>>‘a’ or
‘n’ ’a’
>>>6<9 or ‘c’+9>5 # or operator will test the second operand only if the first operand
True # is false, otherwise ignores it, even if the second operand is wrong
iv. Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
Examples:
Let
a = 10
b=4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
v. Assignment operators: Assignment operators are used to assign values to the variables.
OPERA
TOR DESCRIPTION SYNTAX
a. Identity operators- is and is not are the identity operators both are used to check if
two values are located on the same part of the memory. Two variables that are equal
does not imply that they are identical.
Example:
Let
a1 =
3
b1 = 3
a2 = 'PythonProgramming'
b2 =
'PythonProgramming' a3 =
[1,2,3]
b3 = [1,2,3]
Output:
False
True
False
Example
:
>>>str1= “Hello”
b. Membership operators- in and not in are the membership operators; used to test
whether a value or variable is in a sequence.
Example:
Let
x = 'Digital India'
y = {3:'a',4:'b'}
print('D' in x)
print('digital' not in x)
print('Digital' not in x)
print(3 in y)
print('b' in y)
Output:
T
r
u
e
T
r
u
e
F
a
l
s
e
T
r
u
e
F
a
l
s
e