Python Data Types
Data types define the type of data stored inside a variable. Python automatically identifies the datatype based on the assigned value.
Why Data Types are Important?
Data types help Python understand:
- What kind of data is stored
- What operations can be performed
- How much memory is required
- How the data should behave
Main Python Data Types
x = 10 # Integer price = 99.99 # Float name = "Rahul" # String is_active = True # Boolean
Integer (int)
Integer data type stores whole numbers without decimal values.
age = 25 marks = 100 temperature = -5 print(age) print(marks) print(temperature)
Float (float)
Float data type stores decimal numbers.
price = 99.99 height = 5.8 percentage = 87.5 print(price) print(height) print(percentage)
String (str)
String data type stores text values. Strings are written inside single or double quotes.
name = "Python" city = 'Delhi' message = "Welcome to CodeVyro" print(name) print(city) print(message)
Boolean (bool)
Boolean datatype stores only two values: True or False.
is_logged_in = True is_admin = False print(is_logged_in) print(is_admin)
List Data Type
Lists store multiple values in a single variable. Lists are mutable, meaning values can be changed.
numbers = [10, 20, 30, 40] names = ["Ali", "John", "Sara"] print(numbers) print(names)
Tuple Data Type
Tuples are similar to lists, but values cannot be changed after creation.
colors = ("Red", "Blue", "Green")
print(colors)
Dictionary Data Type
Dictionary stores data in key-value pairs.
student = {
"name": "Ali",
"age": 20,
"marks": 90
}
print(student)
Checking Data Type using type()
Use type() function to check the datatype of a variable.
name = "Python" age = 25 price = 99.99 print(type(name)) print(type(age)) print(type(price))
Output
<class 'str'> <class 'int'> <class 'float'>
Type Conversion
Python allows converting one datatype into another datatype.
age = "25" converted_age = int(age) print(converted_age) print(type(converted_age))
Output
25 <class 'int'>
Real Life Example
student_name = "Ali" student_age = 20 student_percentage = 88.5 passed = True print(student_name) print(student_age) print(student_percentage) print(passed)
Summary
- Data types define the type of stored data.
- Python automatically identifies datatypes.
- Common datatypes are int, float, str, and bool.
- Lists store multiple values.
- Dictionaries store key-value pairs.
- type() function checks datatype.
- Type conversion changes one datatype into another.