Hello dear, today we will all read about the data types given in Python, so what are we waiting for, let’s start..
What is Data type in Python | Python Data Types example
Data types
Data types denote the type of data stored into variables. Like other programming languages Python has a set of built-in data types. Since everything is object in Python, every data type in Python is a class and each variable is an object of a class.
Data types classification:
Text | str |
Numeric | int, float, complex |
Sequence | list, tuple, range |
Mapping | dictionary |
Set | set, frozenset |
Boolean | bool |
Binary types | bytes, bytearray, memoryview |
Text
Text is a collection of characters otherwise called as string.String is represented using str data type. A set of characters enclosed within a set of single quotes(‘) or set of double quotes(“) is called string literal.Multiline strings are enclosed within triple quotes (”’ or “””)
1 2 | s='Hello world' print(s) |
Hello world
1 2 | s="abc*xyz#" print(s) |
abc*xyz#
1 2 3 | s="""Hi friends. Happy programming.""" print(s) |
Hi friends. Happy programming.
Strings are immutable
String is an object that is not changeable after creation. So it is called immutable object. The contents of string objects cannot be overwritten.
1 2 3 | s="""Hi friends. Happy programming.""" s[3]='p' |
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> s[3]='p' TypeError: 'str' object does not support item assignment
Slicing the strings
Slicing is the process of extracting a range of characters or substring from the given string.
string_name[starting index:ending index(not included)]
1 2 | s="Happy programming" print(s[6:13]) |
program
Numeric data types represented using int, float and complex and are called as Python Numbers.
The numbers without fractional part (a decimal point and a set of digits after the decimal point) are called integers. The numbers 10,-100,234 are the examples of integers. Integer values can be of any length.
The limit on the length of integers is dependent on the limit of the memory in the computer.
1
2 | a = 1234567890123456789
print(a)
|
1234567890123456789
Floating point numbers are the numbers with fractional part (a decimal point and a set of digits after the decimal point), represented by ‘float’ data type and are 64-bit double precision values in all platforms. The maximum floating point value a variable can have is 1.8*10308 .
1
2 | pi=3.14
print(pi)
|
3.14
Complex numbers are represented in the form a+bj, where a is real part and b is the imaginary part.
1
2 | c=1+2j
print(c)
|
(1+2j)
1
2
3
4 | a=1+2j
b=3+4j
c=a+b
print(c)
|
(4+6j)