Data Types
Data type refers to the type of data we are storing in the variable. Python is a versatile and dynamically typed programming language and offers a range of built-in data types.
Numeric Types
- int represents integers or whole numbers without decimals e.g, 10, 20, 30 etc.
- float represents numbers with decimal point e.g, 1.2, -4.3, 5.0 etc.
- complex represents complex numbers with j in imaginary part.
Sequence Types
- str represents text e.g, "john", "smith" etc.
- list represents ordered, mutable collection of items.
- tuple represents ordered, immutable collection of items.
Mapping Type
- dict represents dictionaries which store key value pairing for efficient retrieval of data.
Set Types
- set represents unordered collection of unique items.
- frozenset represents immutable set.
Boolean Type
- bool represents boolean values e.g, True or False.
Binary Type
- bytes represents immutable sequences of bytes.
- bytearray represents mutable sequences of bytes.
- memoryview represents memory view objects allowing access to internal buffers.
Range Type
- range represents an immutable sequence of numbers.
None Type
- none represents a null value.
Dynamic Typing
Python is a dynamically typed language. It means that you do not need to specify the type of data storing in a variable. Python interpreter automatically detects the type of data which is stored in the variable and assign it specific data type.
Type Casting
When we take the input, and store it in a variable, its data type is string by default. When we have to perform mathematical operations, we can not do operations on string. So, we have to convert it into integer type. This is called type casting. Let see an example code:
In this way we can convert any variable to any other data type. We can also do type casting at the time of taking input.
We can also set the data type of a variable at the time of declaration.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP