Data and Data Types in Python
What is Data?
Data is any piece of information that a computer can store, process, and manipulate. In Python, data can be a number, text, true/false value, or a collection of values.
Examples of Data
10→ a number"Python"→ textTrue→ boolean value[1, 2, 3]→ collection of numbers
What are Data Types?
A data type tells Python what kind of data a value is and how it should be handled.
Python is a dynamically typed language, meaning:
- You do not need to declare the data type.
- Python automatically detects the data type at runtime.
Example
x = 10
Here, Python automatically understands that x is of type int.
Why Data Types Are Important?
Data types help Python:
- Decide how much memory to allocate
- Know which operations are allowed
- Prevent errors during execution
Built-in Data Types in Python
Python has several built-in data types. The most commonly used ones are:
Category Data Type Example
Numeric int, float, complex 10, 3.5, 2+3j
Text str "Hello"
Boolean bool True, False
Sequence list, tuple, range [1,2], (1,2), range(5)
Set set {1,2,3}
Mapping dict {"a":1}
None NoneType None
Numeric Data Types
int (Integer)
Definition: int stores whole numbers without decimals.
variable_name = integer_value
Example
age = 25
Explanation: 25 is a whole number, so Python assigns it the int data type.
float (Floating Point)
Definition: float stores numbers with decimal points.
price = 99.99
Explanation: Since 99.99 contains a decimal, Python treats it as a float.
complex
Definition: complex stores numbers with a real and imaginary part.
num = 3 + 4j
Explanation: 3 is the real part and 4j is the imaginary part.
Text Data Type (str)
Definition: str stores text or characters enclosed in quotes.
name = "Python"
Explanation: Text is written inside quotes, so Python identifies it as a string.
Boolean Data Type (bool)
Definition: bool represents True or False values.
is_active = True
Explanation: Boolean values are commonly used in conditions and decisions.
Sequence Data Types
list
Definition: A list is an ordered, mutable collection of items.
numbers = [1, 2, 3, 4]
Lists can be modified (add, remove, change elements).
tuple
Definition: A tuple is an ordered, immutable collection.
coordinates = (10, 20)
Tuples cannot be changed after creation.
range
Definition: Generates a sequence of numbers.
numbers = range(1, 6)
Generates numbers from 1 to 5.
Set, Dictionary, and None Data Types
set
unique_numbers = {1, 2, 3, 3}
Duplicate values are automatically removed.
dict
student = {"name": "John", "age": 20}
Stores data in key-value pairs.
NoneType
result = None
Represents absence of a value.
Checking Data Type in Python
x = 10
print(type(x))
Output:
<class 'int'>
type() tells us the data type of a variable.
Conclusion
- Data is information.
- Data types define how data is stored and used.
- Python automatically assigns data types.
- Understanding data types is fundamental to writing correct Python programs.