Data Handing in Python
Introduction:
- In this chapter we will learn data types, variables, operators and expression in detail.
- Python has a predefined set of data types to handle the data in a program.
- We can store any type of data in Python.
DATA TYPES
- Data can be of any type like- character, integer, real, string.
- Anything enclosed in “ “ is considered as string in Python.
- Any whole value is an integer value.
- Any value with fraction part is a real value.
- True or False value specifies boolean value.
- Python supports following core data types :-
- Numbers
- int like 10, 5
- float like 3.5, 302.24
- complex 3+5i
- String ( “pankaj”, ‘pankaj’, ‘a’, “a” )
- List like [3,4,5,”pankaj”] its elements are Mutable.
- Tuple like(3,4,5,”pankaj”) its elements are immutable.
- Dictionary like {‘a’:1, ‘e’:2, ‘I’:3, ‘o’:4, ‘u’:5} where a,e,i,o,u are keys and 1,2,3,4,5 are their values.
CORE DATA TYPES
Mutable and Immutable Types
In Python, Data Objects are categorized in two types :-
- Mutable (Changeable)
- Immutable (Non-Changeable)
Look at following statements carefully
p = 10
q = p
r =10 they will represent 10, 10, 10
Now, try to change the values
p = 17
r = 7
q =9
did the values actually change?
Answer is NO.
Because here values are objects and p, q, r are their reference name.
Variables and Values
An important fact to know is-
- In Python, values are actually objects.
- And their variable names are actually their reference names.
Suppose we assign 10 to a variable A.
- A = 10 Here, value 10 is an object and A is its reference name.
If we assign 10 to a variable B, B will refer to same object.
- Here, we have two variables, but with same location.
- Now, if we change value of B like B=20
- Then a new object will be created with a new location 20 and this object will be referenced by B.
Mutable and Immutable Types
Following data types comes under mutable and immutable types-
- Mutable (Changeable): –
- lists and dictionaries
- Immutable (Non-Changeable): –
- integers, floats, Booleans, strings , tuples and sets.
Variable Internals
The Type of an Object
Pay attention to the following command:-
>>>a=4
>>>type(4)
<class 'int'> # here 4 is an object and its class is int (integer)
>>>type(a)
<class 'int'> # here a is referring to the object which is of int class.
The Value of an Object
Pay attention to the following command:-
>>>print (4)
4 # here value output is coming via print() function.
>>>print (a)
4
The ID of an Object
>>>id (4)
1817668720 Here value 4 and variable a are showing same id which means 4 is an object being referenced by a that’s why they are keeping same id.
>>>id (a)
1817668720
Operators
The symbols that shows a special behavior or action when applied to operands are called operators. For ex- + , - , > , < etc.
Python supports following operators:-
- Arithmetic Operator
- Relation Operator
- Logical Operators
- Identity Operators
- Bitwise Operators
- Membership Operators
Arithmetic Operators
- Python has following binary arithmetic operator -
- addition + (for ex- 2+3 will result in to 5)
- subtraction – (for ex- 2-3 will result in to -1)
- multiplication * (for ex- 2*3 will result in to 6 )
- division / its result comes in fraction. (for ex- 13/2 will result in to 6.5)
- quotient (Floor) // its result comes as a whole number (for ex- 13//2 will result into 6.)
- remnant (Remainder) % its result comes as a whole remnant number.(For ex-13%2 will result into 1. )
- exponent ** (power of) it will come as per exponent value. (For ex- 2 **3 will result into 8)
Assignment Operators and shorthand
Python has following assignment operator and shorthand -
- (=)
- a=10 , 10 will be assigned to a.
- (+=)
- a+=5 is equal to a=a+5.
- (-=)
- a-=5 is equal to a=a-5.
- (*=)
- a*=5 is equal to a=a*5.
- (/=)
- a/=5 is equal to a=a/5.
- (//=)
- a//=5 is equal to a=a//5.
- (%=)
- a%=5 is equal to a=a%5.
- (**=)
- a**=5 is equal to a=a**5
Relational Operators
Python uses Relational operators to check for equality. These results into true or false. Relational Operator are of following types:-
- < Less Than like a<b
- > Greater Than like a>b
- <= Less Than and Equal to like a<=b
- >= Greater Than and Equal to like a>=b
- == Equal to like a==b
- != not Equal to like a!=b
Logical Operators
Python has two binary logical operators -
- or operator
- if a = True and b = False then a or b will return True.
- and operator
- If a = True and b = False then a and b will return False.
- not operator
- if a = True then not a will return False.
Identity Operators
Identity operator is also used to check for equality. These expression also results into True or False.
Identity Operators are of following types-
- “is” operator if a=5 and b=5 then a is b will come to True
- “is not” operator if a=5 and b=5 then a is not b will come to False
Relational Operator ( == ) and Identity operator (is) differs in case of strings that we will see later.
Bitwise operators
Used to manipulate bit values.
Operator Meaning Example
& Bitwise AND x& y
| Bitwise OR x | y
~ Bitwise NOT ~x
^ Bitwise XOR x ^ y
>> Bitwise right shift x>> 2
<< Bitwise left shift x<< 2
Python Membership Operators
Test for membership in a sequence or list.
Operator
- in
- Evaluates to true if it finds a variable in the specified sequence and false otherwise.
- not in
- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
e.g.
a = 5
b = 10
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 -a is available in the given list")
else:
print ("Line 1 -a is not available in the given list")
if ( b not in list ):
print ("Line 2 -b is not available in the given list")
else:
print ("Line 2 -b is available in the given list")
output
Line 1 -a is available in the given list
Line 2 -b is not available in the given list
Operators Precedence :
highest precedence to lowest precedence table
Type conversion/ Type Casting
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion:
In Implicit type conversion,Python automatically converts one datatype to another datatype.This process doesn't need any user involvement.
Explicit Type Conversion:
In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int(),float(),str() etc.
Working with math Module of Python
Python provides math module to work for all mathematical works. We need to write following statement in our program import math output of this program will be 5.0
import math
a=25
print(math.sqrt(a))
Taking Input in Python
In Python, input () function is used to take input which takes input in the form of string. Then it will be type-casted as per requirement.
For ex- to calculate area of a circle, program will be as-
#program to find out area of a circle
radius=int(input("Enter radius"))
area= 3.14*radius*radius
print("The area of circle is ",area)
Its output will be as-
Enter radius : 10
It is so helpful for us
ReplyDeleteThnx sir ji