codingstreets
Search
Close this search box.
exploring-the-fundamentals-an-introduction-to-python-data-types

Exploring the Fundamentals: An Introduction to Python Data Types

Python Data Types – Python provides a range of built-in data types. A Data type defines what type of value variable stores. It guides the Python interpreter in dealing with the variables stored in various kinds of data types. Python holds different types of Data types such as int, float, str, and boolean, etc.,     

Following are the Data Types Python holds-

Name

Value-type

Text Type

str

Numeric Type

int, float, complex

Sequence Type

list, tuple, range

Mapping Type

dict

Set Type

set, frozenset

Boolean Type

Bool

Binary Type

Bytes, bytearray, memoryview

Let’s get started with an example.

Text Type – A data type that represents sequences of characters enclosed in single quotes (”) or double quotes (“”). Used for representing text data, such as names, sentences, or any other collection of characters. E.g., “codingstreets”, ‘It is Five’, ‘A’.

				
					String = 'Hello world' # quotation mark can be both single or double
print(String)
				
			

Numeric – A data type that represents numeric value known as numeric data type. It includes data types of int, float, and complex. E.g., 5, -10, or 0, 1+2j.

				
					int = 1
float = 1.2              #(Decimal number in mathematical terms)
Complex = 5+8j    #(syntax = a+bj. Where, a, b = integer and j = alphabet) 

				
			

Int or integer – An integer is a data type that includes the number of some range of mathematical Integers. Integers support both negative and positive types of numbers. E.g., 10, 15, -18.

				
					x = 10
z = -7

print(x)
print(z)

#output:
10
-7
				
			

Float – In Python or computer language decimal numbers calls float. It is as same as a decimal number in mathematics. Float can be positive or negative. E.g., 1.9, -1.2.

				
					x = 5.2
y = -7.8

print(x)
print(y) 

#output:
5.2
-7.8
				
			

Float can also be scientific numbers with an “e” to indicate the power of 10.

				
					x = 1e7
y = 2E10
z = -5.7e6

print(x)
print(y)
print(z)

#output:

10000000.0
20000000000.0
-5700000.0
				
			

Complex – A number that includes real and imaginary components represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number). E.g., 1+2j.

				
					z = 1+1j
y = 2j

print(z)
print(y)

#output:

(1+1j)
2j
				
			

Python Object Data Type

				
					x = 10 #int
y = 1.1 #float
z = 1+1j #complex

print(type(x))
print(type(y))
print(type(z))

#output:

10
1.1
1+1j
				
			

Sequence Type – A data type that represents how to manage a list of things (numbers, alpha-numeric) that are in order or not.

				
					List = list(['red', 'yellow', 'green'])
Tuple = tuple(['red', 'yellow', 'green'])
Range = range(6)
  
print(List)
print(Tuple)
print(Range)  

#output:

['red', 'yellow', 'green']
('red', 'yellow', 'green')
range(0, 6)

				
			

Mapping Type A data type that represents storing data values in key : value pairs. The values stored in the Dict type always come in curly braces ({}).

				
					Dict = {'name' : 'John', 'age' : 20, 'country' : 'NYC'}
print(Dict)

#output:
{'name': 'John', 'age': 20, 'country': 'NYC'}
				
			

Set Type Represents an unordered collection of unique elements enclosed in curly braces ({}). Sets are useful for operations like membership testing and eliminating duplicate values.

				
					Set = {'red', 'yellow', 'green'}
print(Set)

#output:
{'yellow', 'green', 'red'}
				
			
				
					x = frozenset(('red', 'yellow', 'green'))
print(x)   

#output:
frozenset({'yellow', 'green', 'red'})
				
			

Bytes – A method that represents the byte size of the given integer.

				
					x = bytes(5)
print(x)

#output:
b'\x00\x00\x00\x00\x00'
				
			

bytearray – A method that represents a bytearray object which is an array of the given object.

				
					x = bytearray(6)
print(x)

#output:
bytearray(b'\x00\x00\x00\x00\x00\x00')
				
			

memoryview – A method that represents Python code to access the internal data of an object that supports the buffer protocol without copying.

				
					x = memoryview(bytes(8))
print(x)

#output:
<memory at 0x1deca28>
				
			

Boolean – Boolean returns True and False as output. It is used when one of the two conditions is either true or false. Used for logical operations and conditional statements.

				
					x = 10
if x > 0:
     print(True)
else:
     print(False)

#output:
True
				
			
				
					name = 'codingstreets'
if type(name) == str:
     print(True)
else:
     print(False) 

#output:
True
				
			

Type Conversion – Type Conversion is defined as the conversion of one type of data into another type of data with the help of int(), float(), and complex().

				
					x = 12
y = 12.1
z = 5j
 
print(x)
print(y)
print(z) 

#output:
12
12.1
5j
				
			

Conversion of int into float

				
					x = float(10)
print(x)

#output:
10.0
				
			

conversion of float into int

				
					Float = 10.1
y = int(Float)
print(y)

#output:
10
				
			

conversion of int into complex

				
					x = 12
z = complex(x)
print(z)

#output:
(12+0j)
				
			

Note: Complex data type can’t be converted into another data type.

In Python, the specific data type can be set to variables by using the following constructor functions.

				
					a = str('I like to read articles on codingstreets')
b = int(1)
c = float(1.2)
d = complex(1)
e = list(('red','yellow','green'))
f = tuple(('red','yellow','green'))
g = range(6)
h = dict(name = 'John', Add = 'NYC')
i = set(('red','yellow','green'))
j = frozenset(('red','yellow','green'))
k = bool(1)
l = bytes(2)
m = bytearray(6)
n = memoryview(bytes(8))

print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
print(j)
print(k)
print(l)
print(m)
print(n)

				
			
				
					#output:

I like to read articles on codingstreets
1
1.2
(1+0j)
['red', 'yellow', 'green']
('red', 'yellow', 'green')
range(0, 6)
{'name': 'John', 'Add': 'NYC'}
{'yellow', 'green', 'red'}
frozenset({'yellow', 'green', 'red'})
True
b'\x00\x00'
bytearray(b'\x00\x00\x00\x00\x00\x00')
<memory at 0x24eab30>
				
			

Conclusion

These data types form the foundation for working with different kinds of data in Python. They can be combined and manipulated to create complex data structures and solve various programming problems. Python also provides powerful built-in functions and methods to operate on these data types, making data manipulation and analysis more efficient.

 

Understanding Python’s data types and their characteristics is crucial for writing clean, efficient, and bug-free code. It allows programmers to choose the appropriate data structure for their specific requirements and ensures data integrity and consistency throughout the program.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on