chapter4
chapter4
Data types
Data types are means to identify the type of data and associated operations of
handling it.
Datatype can be many types eg character, integer, real, string etc.
Built- in Core Data types
o Numbers (int , float, complex)
o String
o List
o Tuple
o Dictionary
Data types
Data types are means to identify the type of data and associated operations of
handling it.
Integers
o Integers (Signed)
o Booleans
o
Integers :-
Integers are whole numbers such as 5,39,1917, 0 etc
They have no fractional parts.
Integers are represented in Python by numeric values with no decimal point.
Integers can be positive or negative e,g +12,-15,3000(missing + or-symbol
means it is positive number).
Type of integers
Integers (signed)---
False False
>>>bool(1) >>>Str(True)
True True
Eg 0.000000123
>>>print(c) -1.0
Strings
A python strings is a sequence of characters and each character can be
individually accessed using its index
A string data type lets us to hold string data i.e any number of valid
characters into a set of quotation marks.
In Python 3.x each character stored in string is a Unicode character i.e all
strings in Python 3.x are sequences of pure Unicode characters .
Unicode is a system designed to represent every character from every
language.
A strings can hold any type of known characters i.e letters , numbers and
special characters of any known scripted language.
Eg of some legal strings
“abcd”, “1234”, ‘$%^&’,’????’
0 1 2 3 4 5 6
Subject 7 8
C O M P U T E R S
-9 -8 -7 -6 -5 -4 -3 -2
-1
Thus,
Note :--The index (also called subscript sometimes) is the numbered position of a
letter in the string. In Python, indices begin 0 onwards in the forward rirection and -1,-
2 …in the backward direction.
List
IMMUTABLE TYPES
The immutable types are those that can never change their value in place.
Eg integers , floating point numbers , Booleans , strings, tuples
Immutability means that in the same memory address, new value cannot be stored
as and when we want.
Eg p=5
q=p
r=5
.. # will give 5,5,5
5 6 7 8 9 10
p q r
>>> id(5) >>> id(p) >>> id(q) >>> id(r)
1457662208 1457662208 1457662208 1457662208
p=10
r=7
q=r
5 6 7 8 9 10
p q r
Now If we
>>>t=5
>>>id(t)
1457662208 here variables t has reference memory address same as initial
reference memory address of variable p when it has value 5.
Note :---
The mutable types are those whose values can be changed in place.
Only three types are mutable in python .
Lists
Dictionaries
Sets ( Not in syllabus)
>>>Chk=[2,4,6]
id(chk)
150195536
Variable Internals
Objects :-
Python is an object oriented language. Python calls every entity that stores
any values or any type of data as an object.
An object is an entity that has certain properties and that exhibit a certain
type of behaviour eg integer values are objects – they hold whole numbers
only and they have infinite precision (properties); they support all
arithmetic operations(behaviour)
All values are referred to as object in python. Similarly , we can say that a
variable is also an object that refers to a value.
>>>a=4
>>>print(4)
>>>print(a)
The id of an object
The id of an object is generally the memory location of the object.
Id returns the memory location of the object.
Built –in function id( ) returns the id of an object eg
>>>id(4)
30899132
>>>a=4
>>>id(a)
30899132
30899132
a=4
; a 4
B=5
30899120
;
B=b-1 b
5
Variable Internals
Objects :-
Python is an object oriented language. Python calls every entity that stores
any values or any type of data as an object.
An object is an entity that has certain properties and that exhibit a certain
type of behaviour eg integer values are objects – they hold whole numbers
only and they have infinite precision (properties); they support all
arithmetic operations(behaviour)
All values are referred to as object in python. Similarly , we can say that a
variable is also an object that refers to a value.
>>>a=4
>>>print(4)
>>>print(a)
The id of an object
The id of an object is generally the memory location of the object.
Id returns the memory location of the object.
Built –in function id( ) returns the id of an object eg
>>>id(4)
30899132
>>>a=4
>>>id(a)
30899132
30899132
a=4
a 4
;
B=5
30899120
;
B=b-1 b 5
Operators
“The symbols that trigger the operation / action on data are called Operators,
and the data on which operation is being carried out i.e the objects of
operation(s) are referred to as operands.
Arithmetic Operators
Python use arithmetic operators for basic calculations like
+ addition
-subtraction
*multiplication
/ division
// floor division
% remainder
** exponentiation
Unary Operators
The operators that act on one operand are referred to as Unary Operators.
Unary + The operators unary ‘+’precedes an operand. The operand (the value
on which the
Operator operates) eg a=5 than +a means 5
Unary- The operators unary ‘-’precedes an operand. The operand of the unary –
operator must have arithmetic type and the result is the negation of its operand’s
value eg if a=5 then –a means -5
Binary Operators
“ Operators that act upon two operands are referred to as Binary Operators.
We know that in Python has an assignment operator =which assigns the value
specified on RHS to the variable/object on the LHS of =.
Python also offers augmented assignment arithmetic operators , which combine
the impact of an arithmetic operator with an assignment operator,
Eg a=a+b
We may write
a+=b
In the term relational operator , relational refers to the relationships that values (or
operands) can have with one another.
The relational operators determine the relation among different operands.
Python provides six (06) relational operators for comparing values(thus also
calledcomparison operators).
If the comparison is true, the relational expression results into the Boolean value
True and to Boolean value False, if the comparison is false.
< less than , <= less than or equal to , == equal to
> greater than ,>= greater than or equal to , != not equal to
a=3 , b=13
a<b will return True
a+5>c-2
Identity Operators
>>>a=235
For eg
>>>b=240
>>>c=235
>>>a is b
False
>>>a is c
True
Equality (==) and Identity (is)---important Relation
When the is operator returns True for two variables, it implicitly means that
the equality operator will also return True.
That is , expression a is b as True means that a==b will also be True, always.
>>> a,b=235,235
print(a,b)
>>>a is b
True
>>>a==b
>>>True
Exception
>>>s1=’abc’
The reson behind this behaviour is that
>>>s2=input(“Enter a string:”)
there are a few cases where Python creates
Enter a string :abc
two different objects that both store the
>>>s1==s2
same value.
True
>>>s1 is s2
False
>>>s3=’abc’
Logical Relational Operators
s1 is s3
Python provides three logical operators to combine existing expressions
True NOT
OR, AND,
OR –Operator evaluates to True if either of its (relational) operands evaluates
to True;
False if both operands evaluate to false.
AND operators evaluates to True if both of its (relational)operands evaluate to
True; False if either or both operands evaluate to false.
NOT operator returns always a Boolean True or False.
Expressions- An expression in Python is any valid combination of Atoms and
operators.
Atom- An atom is something that has a value. Identifiers , literals, strings, list,
tuples , sets, dictionaries.
Expression is composed of one or more operations.
Module:-
>>>import math
>>>math.pow(3,4)
81.0
Working with random module
The statistics module of the Python Standard Library provides many statistics
functions such as mean( ), median( ) , mode( ) etc.