Python data types
Different types are available in python. For example, if you want to store an employee salary then the data type will be integer or float, if you want to store a person address then it will be stored as alphanumeric character respectively.
Following are the data types in Python language:
1. Binary types : bytes, bytearray, memoryview.
The byte and bytearrays are generally used to operate the binary data in Python language. The byte and bytearrays are supported via a buffer protocol called as memoryview.
Example Code
#byte to hexa and hexa to byte
Strhexa = bytes.fromhex('A2f7 4509')
print(Strhexa)
Stringbyte = b'xa2xf7Et'
print(Stringtype.hex())
#replace value
b1 = b"XYZ"
b2 = b1.replace(b"X", b"P")
print(b2)
#count character
Array1A = b'ABBACACBBACA'
print(Array1A.count(b'AC'))
#bytearray
myByteArray = bytearray('String', 'UTF-8')
memView = memoryview(myByteArray)
2. Set types : set, frozenset
The sets are actually an unordered collection of the clear hash table object. Generally is used for a mathematical operation like difference, intersection, and union. The frozenset is immutable on the other hand set type is mutable in python. In frozenset it is not possible to perform methods like add() and remove() but in a set this is possible.
Example Code
Setno1 = {1, 2, 3, 4}
Setno2 = {5, 6, 7, 8,3}
Setno3 = set(range(15)) # all elements from 0 to 14 in the set
Setno4 = {10, 20, 30, 40}
print(set(Setno1.union(Setno2)))
print(set(Setno1.intersection(Setno2)))
print(set(Setno1.difference(Setno2)))
print(Setno3.issuperset(Setno1))
print(Setno1.isdisjoint(Setno4))
Setno4.add(45)
print(Setno4)
Setno4.discard(40)
print(Setno4)
3. Boolean type : bool
Generally Boolean represents only two values respectively:
True
or False
Example
#if number is greater then if will work
num1 = 200
num2 = 33
if num1 > num2:
print("num1 is greater than num2")
else:
print("num1 is not greater than num2")
4. Mapping type : Dict
Mapping type in python is mutable and is used to map hash table values to random objects. Dictionary is the mapping type in python
Example
#Mapping type using dictionary
myDict = {'fifty' : 50, 'sixty' : 60, 'seventy' : 70, 'eighty' : 80}
print(myDict)
print(list(myDict.keys()))
print(list(myDict.values()))
#create items from the key-value pairs
print(list(myDict.items()))
myDict.update({'fifty' : 50})
print(myDict)
5. Text type : Str
A sequence of characters is generally termed as Strings and called str in python. It is written within single quotes or double-quotes. Output can be displayed using the print() function in the program.
Example
#String datatype evaluation
strr = 'Hello World flocks'
print strr # Prints complete string
print strr[0] # Prints first character of the string
print strr[2:5] # Prints characters starting from 3rd to 5th
print strr[2:] # Prints string starting from 3rd character
print strr * 3 # Prints string three times
print strr + "department" # Prints concatenated string
6. Numeric types : int, float, complex
Numeric data type in python is immutable that means the value of a number in data type results in a newly allocated object with a fresh address.
Different Numeric types are:
1. int
These are positive or negative whole numbers that don’t contain any decimal point.
#int
xnum = 1634
ynum = 3563487711
znum = -56556652
print(type(xnum))
print(type(ynum))
print(type(znum))
2. float
These are the number which contains decimal point within it. Float numbers generally divide integer and fractional parts of the digit.
#float
xnum = 8.10
ynum = 9.5
znum = -97.533
print(type(xnum))
print(type(ynum))
print(type(znum))
3. complex
These are represented in the form of a + bj in which both a and b are numbers and j generally represents an imaginary number which is a square root of -1.
Example
#complex
xnum = 1+54j
ynum = 8j
znum = -9j
print(type(xnum))
print(type(ynum))
print(type(znum))
7. Sequence types : list, tuple, range
The famous sequence type in python is list, tuple, and range. In python, there is some other sequence type object which is some text string and binary data.
Example
List1 = [1, 2, 3, 4, 5]
List2 = [56, 42, 85, 96, 23]
if 5 in List1:
print('30 is present')
if 120 not in List1:
print('120 is not present')
print(List1 + List2) #Concatinate lists
print(List1 * 2) #Add myList1 two times with itself
print(max(List2))
print(List2.count(42)) #42 has two times in the list
print(List2[2:7])
print(List2[2:7:2])
List1.append(60)
print(List1)
List2.insert(5, 17)
print(List2)
List2.pop(3)
print(List2)
List1.reverse()
print(List1)
