python
=12
xtype(x)
<class 'int'>
Integers represent whole numbers, both positive and negative, without a fractional component.
Used for decimal or floating-point numbers.
Booleans in Python represent one of two values: True
or False
. They are typically used in conditional statements. Below is a simple example demonstrating the use of Booleans.
A sequence of characters, enclosed in single, double, or triple quotes.
Ordered, mutable collections of items of mixed data types.
Lists are mutable: You can change the contents in the list as shows below
Ordered collections like lists, but immutable collections of items of mixed data types.
- Immutable: Once created, the elements of a tuple cannot be changed, added, or removed.
- Ordered: Tuples maintain the order of elements as they were added.
- Indexing and Duplication: Tuples support indexing (you can access elements using their index) and can contain duplicate elements.
- Syntax: Defined by enclosing elements in parentheses (), although parentheses are optional.
Tuples are immutable.my_tuple[0] = 20
Executing the above line will raise an error because changing an element in a tuple is not allowed.
Unordered collections of unique elements. They are mutable and are useful for operations like union, intersection, and difference.
- Mutable: You can add or remove elements from a set after its creation.
Unordered: Sets do not maintain any order of elements, and thus they don’t support indexing.
No Duplication: Sets cannot contain duplicate elements. Adding a duplicate element will not change the set.
Syntax: Defined by enclosing elements in curly braces {}
.
<class 'dict'>
# Using the .keys() method to view keys
keys = my_dict.keys()
print("keys in the dictionary:", keys)
keys in the dictionary: dict_keys(['name', 'age'])
# Using the .values() method to view keys
values = my_dict.values()
print("values in the dictionary:", values)
values in the dictionary: dict_values(['Alice', 25])