DATA TYPES
Variables are storage containers which can be referred by user defined name. Variable value can be changed during anytime of the program depending upon the condition or information passed.
For ex. a=5, here a is a variable holding 5 as value.
Data Type specifies the type of data a variable is going to hold. Data can be of many types eg. integer, decimal numbers, alphanumeric, sequence of values etc.
Let us learn how to process different types of data in Python.
In Python, variable are not required to be associated with a particular data type when declared. Rather it can also change the data type as and when required.
For example:
>>>x=10 #Variable x declared of integer type
>>>type(x)
>>><class 'int'>
>>>x="India" # Variable x data type is changed to string
>>>type(x)
>>><class 'str'>
Types of Data Types
To write a good program you must know how to choose a perfect data type to make program efficient. Let us learn these Data types in detail
Numeric Data Type
-
Integer: It consists of whole numbers, means non fractional numbers. It can be signed or unsigned both ex. marks=20, Rno=12, x=-4
-
float: float is used to store numbers with decimals. Even if you store integer 9 in float type then it will be stored as 9.0 ex. weight=56.8, y=-10.5
-
complex: Complex number system has two parts a real and an imaginary number. You might study this concept in higher classes. ex. distance=2+1j
Boolean Data Type
-
Boolean data type represents truth value i.e. True or False. 1 and 0 can also be used in place of True and False respectively. ex. state=False
Dictionary Data Type
-
Dictionary is an unordered set of comma separated key : value pairs. Dictionary is declared in {} and each key should be a unique though values can repeat.
ex. medals={'TeamA':25, 'TeamB':32, 'TeamC':14}
Sequence Data Type
-
String: A string can hold any type of data such as letters, numbers, special characters. Strings are enclosed in single quotes (' ') or double quotes (" ").
ex. Houseno='B-19', name='Neer'
-
List: List is a compound data type, which holds group of values separated by comma (,). List is declared using square brackets [ ] and can contain value of any data type.
ex. student=[12,'Niyasa','60.5'], vowels=['a','e','i','o','u'], marks=[21, 20, 17, 24]
-
Tuple: Tuple is also like list with a difference that they cannot be changed/modified. Tuple is declared using round brackets ( )
ex. days=('Mon', 'Tue', 'Wed')
Indexing
We just talked about the sequence data type, but did you wonder, how are you going to access individual element from them. In sequence data types (string, list or tuple), each element is assigned with a serial number called index. In python, index begins with 0 in case of forward indexing and -1 in backward indexing.
Reading string1 from the given example:
>>> print(string1)
>>>PYTHONFORALL
>>>print(string1[4])
>>>'O'
>>>print(string1[-4])
>>>'R'
Reading list1 from the given example:
>>> print(list1)
>>>[15, 20, 4.5, 'H']
>>>print(list1[0])
>>>15
>>>print(list1[-1])
>>>'H'
Data Type Conversions
-
int() function is used convert the string input into integer so that arithmetic operations can be performed.
-
float() method is used convert the object input into float.
-
str() method is used to convert the object into string type.
-
The chr() method converts an integer to its Unicode character and returns it.
-
The bool() method converts the given value into True or False i.e. Boolean.
-
The type() method displays the data type of the object passed.