At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Basic Data Types in Python

  • By Vishnu Durga
  • June 25, 2020
  • 1884 Views

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.
In the programming world, data types play an important role. Each variable is stored in different data types and responsible for various functions. Python had two different objects. They are mutable and immutable objects.

Mutable objects

The size and declared value and its sequence of the object can able to be modified called mutable objects. 
Mutable Data Types are list, dict, set, byte array

Immutable objects

The size and declared value and its sequence of the object can able to be modified.
Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.
id() and type() is used to know the Identity and data type of the object

a=25+85j
type (a)
output : <class 'complex'>
b={1:10,2:"Pinky"}
id(b)
output : 238989244168

Built-in data types in Python

a = str("Hello python world")                                                         #str
b = int(18)	                                  		                      #int
c = float(20482.5)	                             		                      #float
d = complex(5+85j)	                            		                      #complex
e = list(("python", "fast", "growing","in",2018))                                     #list
f = tuple(("python", "easy", "learning"))          	                              #tuple
g = range(10)	                                   			              #range
h = dict(name="Vidu", age=36)	                   	  	                      #dict
i = set(("python", "fast", "growing","in",2018))     	                              #set
j = frozenset(("python","fast", "growing","in",2018))                                 #frozenset
k = bool(18)	                                  				      #bool
l = bytes(8)	                           				              #bytes
m = bytearray(8)	                    				              #bytearray
n = memoryview(bytes(18))	            			                      #memoryview

Numbers (int,Float,Complex)

Numbers are stored in numeric types. When a number is assigned to a variable, Python creates number objects.

#signed interger
age = 18
print(age)
Output : 18

Python supports 3 types of numeric data.
int (signed integers like 20, 2, 225, etc.)
float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)
complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)
A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).

String

The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling
'Hello Python'
#single (') Quoted String
"Hello Python"
# Double (") Quoted String
"""Hello Python"""
'''Hello Python'''
# triple (''') (""") Quoted String

 
In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.
The operator “+” is used to concatenate strings and “*” is used to repeat the string.

"Hello" + "python"
output : 'Hello python'
"python "*2
'Output : Python python '

Tuple

Tuple and Lists are nearly identical, Like lists, tuples can hold the collection of the different datatypes items. Items in the tuple are separated by commas (,) and should be enclosed within parentheses (). The only difference between the list and tuple are List are mutable but tuples are immutable. Tuples have read-only structure, So Size and Values in the items in the tuples are can’t be modified.“+” Operator is used to concatenate the Lists and “ * “ is used to repeat the lists.

a=(10,"pinky")
b=(20,"Sweety")
a+b
output : (10, 'pinky', 20, 'Sweety')
a*2
(10, 'pinky', 10, 'pinky')

Frozen sets

The frozen sets are similar to sets, the only difference is ‘set’ is mutable and ‘frozen set’ is immutable.assigned values and size of the frozen object can’t be changed.items in the frozenset are separated by comma(,) and declared as frozenset()

a=frozenset(("Pinky",25,"Pinky@xyz.com,0123456789))
type(a)
output : <class 'frozenset'>

List

Lists in python are similar to arrays in C.the list can hold different types of data. In lists, items are stored in sequence with comma (,) Separator and should be enclosed within square brackets [].
“+” Operator is used to concatenate the Lists and “ * “ is used to repeat the lists

List = ["python","fast","growing","in",2018]
List1 =["python","Learning","is","easy"]
List+List1
output : ['python', 'fast', 'growing', 'in', 2018, 'python', 'Learning', 'is', 'easy']
List1*2
['python', 'Learning', 'is', 'easy', 'python', 'Learning', 'is', 'easy']

Dictionary

An ordered set of items with a key-value is called a dictionary. It is similar to an associative array,i.e) each value in the dictionary is stored in separate keys. Keys can contain any primitive data type. Items in the dictionary are separated by Comma(,) and enclosed within curly braces { } key id and Values are separated using “:”.Dictionary is a mutable data type, we can able to change the size and value of the dictionary.

Dict = print({1:"apple",2:"basket",3:"cart",4:"den"})
output : {1: 'apple', 2: 'basket', 3: 'cart', 4: 'den'}

Sets

In Python, There are two types of sets,1.sets and 2. Frozen sets. Set data type contains unordered items collection. The set is a mutable & iterated data type and it has no duplicate values. using set we can do some mathematical operations such as union, intersection, difference, and so on. The set is directly opposite to the list. Items in the sets are separated by Comma(,) and enclosed within curly braces { }.

sets = print({"Jimmy","pinky","Sweety"})
Output : {'pinky', 'Sweety', 'Jimmy'}

Range

The range function is used to generate a number sequence with a particular range and ability to perform an action for a specific number of times.it is declared as a range().

range(100)
Output : range(0, 100)

Bool

The boolean data type has two constant objects true and false. It used to represent the declared values as true or false. If the condition satisfies the result will be True else False.boolean data type is declared as bool()

a="Jimmy"
b="pinky"
c=a
print(bool(a!=b))
Output: True
print(bool(a==c))
Output: False

Bytes

Bytes is an immutable sequence of integers in the range of 0 to 256, It prints the ASCII value of that integers. The immutable version of bytearray is bytes.the indexing and slicing behavior is the same in bytes and bytes array.it is declared as bytes().

bytes(10)
Output : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Byte Array

The mutable sequence of integers from 0 to 256. It is a usual method of mutable sequence.it is declared as bytearray().

bytearray(5)
Output : bytearray(b'\x00\x00\x00\x00\x00')

Memory View

The memory view of the given argument is returned by Memoryview function.A memory view is used to expose the buffer protocol in Safeway. A way to access the object’s internal data is called the buffer protocol. The internal data of an object is called a memory array or a buffer. It is declared as memoryview()

memoryview(bytes(6))
Output : <memory at 0x000001BC911EAF48>

If you have a business idea in your mind or looking for a reliable web development company, you are in the right place. Hire the best Python developers in the industry from Agira technologies.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions

Vishnu Durga

Vishnu Durga is a passionate GIS analyst at Agira Technologies. Enabling her strategic insights in GIS, and data intelligence she has assisted numerous businesses in planning and to make informed business decisions. Apart from data collection and management, she loves to do some art in her leisure moments.