Variable Types
In Python, variables can store data of various types. Python is a dynamically typed language, so you don't need to declare the type of a variable explicitly—it is inferred based on the value assigned to the variable. Understanding variable types is crucial for effectively working with data in Python.
Here’s a breakdown of the common variable types in Python:
1. Integer (int)
- Represents whole numbers, both positive and negative, without a fractional part.
- Example:
age = 25
score = -10
Type checking:
print(type(age)) # Output: <class 'int'>
2. Float (float)
- Represents real numbers with a decimal point, including fractional parts.
- Example:
price = 19.99
temperature = -4.5
Type checking:
print(type(price)) # Output: <class 'float'>
3. String (str)
- Represents sequences of characters (text).
- Strings are enclosed in single quotes (') or double quotes (").
- Example:
name = "Alice"
greeting = 'Hello, World!'
Type checking:
print(type(name)) # Output: <class 'str'>
4. Boolean (bool)
- Represents one of two values: True or False.
- Often used in conditions and comparisons.
- Example:
is_active = True
has_passed = False
Type checking:
print(type(is_active)) # Output: <class 'bool'>
5. List (list)
- Represents an ordered collection of items, which can be of different types.
- Lists are mutable, meaning their content can be changed after creation.
- Example:
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "apple", 3.5, True]
Type checking:
print(type(numbers)) # Output: <class 'list'>
6. Tuple (tuple)
- Similar to lists, but tuples are immutable, meaning their content cannot be changed after creation.
- Tuples are useful for storing fixed collections of items.
- Example:
coordinates = (10, 20)
colors = ("red", "green", "blue")
Type checking:
print(type(coordinates)) # Output: <class 'tuple'>
7. Dictionary (dict)
- Represents a collection of key-value pairs, where each key is unique.
- Useful for mapping or associating data.
- Example:
student = {"name": "Alice", "age": 25, "grade": "A"}
Type checking:
print(type(student)) # Output: <class 'dict'>
Set (set)
- Represents an unordered collection of unique items.
- Useful for storing distinct elements and performing set operations like union, intersection, etc.
- Example:
unique_numbers = {1, 2, 3, 4, 4, 5} # Duplicate '4' will be removed
Type checking:
print(type(unique_numbers)) # Output: <class 'set'>
9. NoneType (None)
- Represents the absence of a value or a null value.
- Often used to initialize variables or to indicate no value.
- Example:
result = None
Type checking:
print(type(result)) # Output: <class 'NoneType'>
Example: Working with Different Variable Types
# Integer
age = 30
print(type(age)) # <class 'int'>
# Float
height = 5.9
print(type(height)) # <class 'float'>
# String
name = "John Doe"
print(type(name)) # <class 'str'>
# Boolean
is_member = True
print(type(is_member)) # <class 'bool'>
# List
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>
# Tuple
coordinates = (10, 20)
print(type(coordinates)) # <class 'tuple'>
# Dictionary
person = {"name": "John", "age": 30}
print(type(person)) # <class 'dict'>
# Set
unique_ids = {101, 102, 103, 101}
print(type(unique_ids)) # <class 'set'>
# NoneType
data = None
print(type(data)) # <class 'NoneType'>
Summary
Understanding variable types in Python allows you to effectively manage and manipulate data in your programs. Each type serves different purposes and comes with its own methods and operations that can be utilized for specific tasks.