Data Types in Python: In Python, understanding data types is crucial for every programmer. A data type indicates the kind of value a variable can hold. Since Python is a dynamically typed language, you don’t need to explicitly declare the type of a variable; the interpreter infers the type automatically based on the value assigned to it.
Grasping the concept of data types can help you write more efficient and effective code. Let’s explore the built-in data types available in Python.
Primitive Data Type:
Primitive data types in Python are the most basic types of data. They are the building blocks for data manipulation in Python and are typically immutable, meaning their value cannot change after they are created. Here are the main primitive data types in Python:
Examples of Primitive Data Types
- Integer (int) :
- Represents whole numbers.
- Example: x = 10
- Float (float) :
- Represents floating-point numbers (decimal values).
- Example: y = 10.5
- Boolean (bool) :
- Represents True or False values.
- Example: is_active = True
- String (str) :
- Represents a sequence of characters.
- Example: name = “Alice”
Read Also: Cybersecurity in the Age of AI: How AI Is Reinventing Digital Defence
Non-Primitive Data-Type:
Non-primitive data types, also known as complex or composite data types, are data types that are derived from primitive data types. They can store multiple values or more complex structures of data. Unlike primitive types, non-primitive data types are mutable, meaning their contents can be changed.
Examples of Non-Primitive Data Types
- List (list) :
- An ordered collection of items, which can be of different types.
- Example: numbers = [1, 2, 3, 4]
- Tuple (tuple) :
- Similar to lists, but immutable.
- Example: coordinates = (10.0, 20.0)
- Dictionary (dict) :
- Collection of key-value pairs.
- Example: person = {“name”: “Alice”, “age”: 30}
- Set (set) :
- An unordered collection of unique items.
- Example: unique_numbers = {1, 2, 3, 4}
- String (str) :
- While strings are technically primitive in Python, they can also be considered non-primitive when used to store complex data or collections of characters
Get Tech News on Our Telegram & WhatsApp Community” |