There are some fundamental or built-in data types in Python. .
We can use type () function to get the data type of an object, and we can use id () function to get the identity (address) of the object.
Numeric types - int, float, complex
There are three distinct numeric types: integers, floating point numbers, and complex numbers.
Every type is an object in python, and it uses automatic memory management using reference counting.
>>> a = 10
>>> print(type(a))
<class 'int'>
>>> b = 1.23
>>> print(type(b))
<class 'float'>
>>> c = 11j
>>> print(type(c))
<class 'complex'>
>>> print(hex(id(a)))
0x7ff906bfd448
>>> print(hex(id(b)))
0x1f5f2bcbdf0
Booleans are subtype of integers, The boolean type has only two values, True or False.
Arithmetical operators: +, - *, /, % //, **
Sequence types: list, tuple, range, str
list, and tuple both can contain instances of different types, the main difference is list is a mutable sequence, whereas tuple is the immutable sequence.
list is denoted by the one pair of square brackets []
tuple is denoted by the one pair of parentheses ()
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
Example # list
fruits = []
print(type(fruits))
#### list #######
fruits = ['apples', 'grapes', 'mangoes']
fruits.append('bananas')
print(fruits)
## output
<class 'list'>
['apples', 'grapes', 'mangoes', 'bananas']
Example # tuple
fruits = ('apples', 'grapes', 'mangoes')
print(type(fruits))
print(fruits)
fruits[0] = 'bananas' # error, tuple doesnot support item assignment
## OUTPUT
<class 'tuple'>
('apples', 'grapes', 'mangoes')
TypeError: 'tuple' object does not support item assignment
Example # range
for x in range(5):
print(x, end=' ')
## OUTPUT
0 1 2 3 4
Text Sequence Type: str
Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points. String literals are written in a variety of ways:
single quotes: 'Learner Landmark'
double quotes: "Learner Landmark"
triple quoted: '''Learner Landmark'''
name = 'Learner Landmark'
print(type(name))
print(name)
## output
<class 'str'>
Learner Landmark
Set Types: set, frozenset
A set object is an unordered collection of distinct hashable objects.
The set type is mutable — the contents can be changed using methods like add() and remove().
The frozenset type is immutable and hashable — its contents cannot be altered after it is created;
set will not allow the duplicates:
fruits = {'apples', 'grapes', 'mangoes', 'bananas'}
print(fruits)
fruits.add('grapes') # this has no effect, if item already exist
print(fruits)
## output
{'grapes', 'apples', 'mangoes', 'bananas'}
{'grapes', 'apples', 'mangoes', 'bananas'}
Empty set created by using set () constructor.
empty_set = {} # this is not a set, it is dict
print(type(empty_set))
empty_set2 = set()
print(type(empty_set2))
## OUTPUT
<class 'dict'>
<class 'set'>
frozenset is an immutable set object:
countries = frozenset(['India', 'USA', 'China', 'Russia', 'India'])
print(type(countries))
print(countries)
## OUTPUT
<class 'frozenset'>
frozenset({'Russia', 'China', 'India', 'USA'})
Mapping Types: dict
Dictionary in python is a key : value pair collection, keys are unique and they can be different types of objects. Keys are immutable they cannot be changed. Values are mutable, they can be changed.
An empty dictionary created with one pair of curly braces {}
A dictionary can be created with dict () constructor
mydict1 = {}
print(type(mydict1))
## output
<class 'dict'>
mydict2 = dict([(101, "Satish"), (102, "Nagesh")])
print(mydict2)
## output
{101: 'Satish', 102: 'Nagesh'}
loop through all key,value pairs:
Comments