Python Data Types
Every variable value has a particular data type. The interpreter attaches the value to a data type to perform relative operations on it. There are many data types in Python and you can check the Python variable data type using the type() function.
Example
Data type represents the type of data that is stored in the variables. A variable can contain a number, name of a city or a combination of numbers and texts.
Here are the main data types in Python: –
- Strings
- Numbers
- Dictionary
- Tuple
- List
- Boolean
Let’s have a brief look at each data type –
Strings
A string is a combination of characters which are represented in quotation mark. Single and double quotes (‘ ‘ or “ ”) can be used to declare a single line string while triple quotes (‘’’ ‘’’) can be used to declare a multi-line string. An already declared string cannot be updated.
Example
Numbers
Python contains numbers types like int, float and complex. Every type of numbers can be found out individually using the type() function.
Floats are represented by separating two integers by a decimal. The decimal number can be represented up to 15 decimal values.
On the other hand, complex numbers are written in the form x + yj where x is a real part and y is imaginary just like the way we represent them in mathematics.
Example
Lists
Lists are the ordered collection of information. Information means different data types ( in data analytics, information is used to represent anything which an object contains in any programming language ). All the items in a list can be of different data type.
A list is declared by enclosing the information in square brackets [ ].
Input: –
L = [ “List item”, 55, 61.09 ]
To access an individual in a list, slicing operator [ ] is used.
Example
Example
Tuple
Tuples are similar to Lists but unlike lists they are immutable I.e. once created; tuples cannot be modified.
Unlike Lists, the information that we have to store in a tuple is enclosed in small brackets ( ).
To extract information from a tuple, you need to follow the similar operations as that of a List.
Example
Dictionary
Dictionaries are used to retrieve data easily. When the amount of data is very high, dictionaries are used to access the value. Dictionary is an unarranged set of key-value pairs.
Dictionaries can be defined in Python within square braces [ ] with each item being in form of key:value.