Python Data Types
Data types are categories of data that define the type of value a variable can hold. Python data types are classes and variables are instances (objects) of these classes. Python is dynamically typed, meaning you don't need to declare the type explicitly.
| Data Types | |
|---|---|
| Numeric | Integer , float , complex |
| Sequences | list , tuple , range |
| Boolean | True or False |
| Mapping | Dict |
| Set Type | set,frozenset |
1.Numeric Type
The Numeric data types are used to store numerical values. There are three primary numeric data types:
Integer - An integer is a whole number without a decimal point that can be positive, negative, or zero
Float - Float is used to store single-precision floating point numbers.
Complex - Complex number is a combination of a real number and an imaginary number.
x = 20 y = 12.8 z = 2+3j print(x) #int print(y) #float print(z) #complex print(type(x)) print(type(y)) print(type(z))
20 12.8 (2+3j) <class 'int'> <class 'float'> <class 'complex'>
2. Sequence Type
The sequence Data type is the allow storing of multiple values in an oraganized fashion.Its Contains List,tuple and strings.
Strings - A String is textual data using single quotes,double quotes or enclosed in quotes
List - A list is used to store the multiple items in one item .It is mutable or changeable and ordered sequence of elements.
Tuples - Tuples is Collection of objects separated by commas.
x = "Hello World" y = ["Hii","Hello"] z = ("How","Are","You") print(x) print(y) print(z) print("\n") print(type(x)) print(type(y)) print(type(z))
Hello World ['Hii', 'Hello'] ('How', 'Are', 'You') <class 'str'> <class 'list'> <class 'tuple'>
3. Boolean Type
Boolean expressions are the expressions that evaluate a condition and result in a Boolean value.Its contains True or False
print(40 > 30) # if 30 is greater than 40 print(1 == 0) #if 1 value is equal to 0
True False
4. Mapping Type
Dict - Key-value pairs for mapping unique keys to values
dict = { "Student": "Kumar", "Standard": "10TH", "Section": "A", } print(dict)
{'Student': 'Kumar', 'Standard': '10TH', 'Section': 'A'}
5. Set Type
Sets are used to store the several items in a single variable. It is one of the four built-in data types (List, Dictionary, Tuple, and Set) having qualities and usage different from the other three
cars = {"Scorpio", "kia", "Innova "} print(cars)
{'kia', 'Innova ', 'Scorpio'}