Data Types in Python
Data Types in Python
Variables can hold data of different types. Data types determine the type and size of data associated with variables.
Following are the standard or built-in data type of Python:
1. String
2. Numeric :-integers, floating point, complex numbers
3. Boolean
4. Collections:-List, Tuple, Dictionary
String type
The text enclosed in quotes forms a String in Python. Strings are sequences of character data. String literals may be delimited using either single or double quotes. For example,str=”abc”
creates a string
other examples are
str=’abc12’
str=”hello world”
str=”” //empty string
str=”12”
str=”@hello”
But str=’Hello” is an invalid string as opening and closing quotes are different.
Numeric data type
Number data types are used to store numeric values. There are three distinct numeric types:
- Integers
- floating point numbers
- complex numbers
Integers
Integers are whole numbers without any fractional part. Integer may contain a (+) or (-) sign. An integer with no sign is assumed to be positive.
Python allows four types of integer:-
Decimal Integers:
It represents an integer consisting of a sequence of digits. The digits may be from 0 to 9.For example,
344,8452,-19
Binary numbers are usually used by computers for representation of information. Binary number can contain only two bits i.e., 0 and 1. Any binary number is a sequence of 0s and 1s
A binary number is represented by using 0b before the desired number.
For example, 0b11011
Try this
num=0b1101
print(num)
output:
13
Decimal equivalent of 1101 is printed.
Octal numbers are sequence of digits starting with 0o(zero followed by o).
An octal number can contain only digits 0to 7. So 0o18 will be an invalid example of octal number.
Some valid examples are:-
Binary integers
Binary numbers are usually used by computers for representation of information. Binary number can contain only two bits i.e., 0 and 1. Any binary number is a sequence of 0s and 1s
A binary number is represented by using 0b before the desired number.
For example, 0b11011
Try this
num=0b1101
print(num)
output:
13
Decimal equivalent of 1101 is printed.
Octal Integers
Octal numbers are sequence of digits starting with 0o(zero followed by o).
An octal number can contain only digits 0to 7. So 0o18 will be an invalid example of octal number.
Some valid examples are:-
0o112,0o675 etc.
Try this
num=23
print(oct(num))
output:
0o27
oct() function returns octal equivalent of a decimal number.
Hexadecimal integers
Hexadecimal numbers can contain digits 0 to 9 and letters A,B,C,D,E and F. A to F represents numbers 10 to 15.A hexadecimal number is represented by using 0x before the desired number.
For example,
0x34,0xC etc.
Try this
num=0xA
print(num)
num=0xA
print(num)
output:
10
Fractional form
Fractional form consist of signed or unsigned digits including a decimal point between digits.
The following are valid real numbers in fractional form:
-12.45,89.66,0.00098,.5
Note:- comma cannot be there in a number so 12,000.89 will be invalid.
Exponent form
10
Floating point numbers
Also known as real numbers. Floating point numbers are the numbers having fractional part. Fractional numbers can be written in two forms
- Fractional form
- Exponent form
Fractional form
Fractional form consist of signed or unsigned digits including a decimal point between digits.
The following are valid real numbers in fractional form:
-12.45,89.66,0.00098,.5
Note:- comma cannot be there in a number so 12,000.89 will be invalid.
Exponent form
This form is derived from scientific notation. It consists of two parts: mantissa and exponent
Consider a number 4500.0
Here the uppercase or lowercase letter ‘e’ signifies the 10th
Other examples are
Consider a number 4500.0
In scientific notation it can be written as 4.5X103
In exponent form it can be written as 4.5e3 or 4.5E3Here the uppercase or lowercase letter ‘e’ signifies the 10th
Other examples are
>>>0.152e04 #type in interactive python
1520.0 #output
>>> 456.0e08
45600000000.0
>>> 454567e-3
454.567
A negative number after “e” shift decimal point to left side.
Complex numbers
A complex number is a number of the form A+Bi, where A and B are the real numbers and i is imaginary. I is equal to the square root of -1 i.e., √-1
An example of complex number is
X = 3+5j
Python uses j in place of i.Here 3 is the real component and 5j is the imaginary component.
Now consider following examples performed in interactive python.
>>> a=7+9.4j
>>> a
(7+9.4j)
>>> b=0+7j
>>> b
7j
>>> a.real
7 .0
real() function returns the real part of a complex number.
>>> a.imag
9.4
imag() function returns the imaginary part of a complex number.
Boolean data type
A Boolean data type has one of two possible values i.e., True or False.When you run a conditional statement, Python returns True or False
Example
>>> a=50
>>> b=90
>>> a>b
False
Collections:
List
A list is a data type that can be used to store any type and number of variables and information.
You can define and assign items to a list with the expression:
my_list = [item_1, item_2, item_3]
An empty list can be created as:
my_list = []
for example,
colors=[‘RED’,’GREEN’,’BLUE’]
vowels=[‘a’,’e’,’i’,’o’,’u’]
nums=[1,4,5,6]
Items of a list can be changed. Suppose we want to store the number 7 in place of 1 at first location of above list nums then we can do this as:
>>>nums[0]=7
>>> nums
[7, 4, 5, 6]
Some examples
Marks=(33,35,32,39)
Vowels=(‘a’,’i’,’o’,’u’)
We cannot change items of a tuple. Therefore a statement like
Marks(1)=99
Will generate an error.
Syntax:
d = {key_1 : a, key_2 : 2, key_3 : ab}
An empty dictionary will have this format:
d = {}
Example
>>> colors={1:'red',2:'white'}
>>> colors
{1: 'red', 2: 'white'}
You can define and assign items to a list with the expression:
my_list = [item_1, item_2, item_3]
An empty list can be created as:
my_list = []
for example,
colors=[‘RED’,’GREEN’,’BLUE’]
vowels=[‘a’,’e’,’i’,’o’,’u’]
nums=[1,4,5,6]
Items of a list can be changed. Suppose we want to store the number 7 in place of 1 at first location of above list nums then we can do this as:
>>>nums[0]=7
>>> nums
[7, 4, 5, 6]
Tuples
Tuples are also like lists but unlike lists, contents of tuples cannot be changed i.e., tuples are not modifiable. Parentheses are used for creation of tuples.Some examples
Marks=(33,35,32,39)
Vowels=(‘a’,’i’,’o’,’u’)
We cannot change items of a tuple. Therefore a statement like
Marks(1)=99
Will generate an error.
Dictionary
The dictionary is an unordered set of comma separated key:value pairs within {}. Here key must be unique i.e., within a dictionary, no two keys can be same and it can be a number, string or tuple. A colon separates a key from its value and all the items are enclosed in curly braces.Syntax:
d = {key_1 : a, key_2 : 2, key_3 : ab}
An empty dictionary will have this format:
d = {}
Example
>>> colors={1:'red',2:'white'}
>>> colors
{1: 'red', 2: 'white'}
Comments
Post a Comment