Code
python
=12
xtype(x)
<class 'int'>
Integers represent whole numbers, both positive and negative, without a fractional component.
python
=12
xtype(x)
<class 'int'>
Used for decimal or floating-point numbers.
python
=12.5
xtype(x)
<class 'float'>
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.
python
# Boolean example
# Assigning boolean values
= True
light_on = False
is_raining
# Using the boolean values in conditional statements
if light_on:
print("The light is on.")
else:
print("The light is off.")
The light is on.
if is_raining:
print("It's raining. Take an umbrella.")
else:
print("It's not raining. No need for an umbrella.")
It's not raining. No need for an umbrella.
A sequence of characters, enclosed in single, double, or triple quotes.
python
= "Hello, Python!"
my_string print(type(my_string))
<class 'str'>
Ordered, mutable collections of items of mixed data types.
python
= [1, 2.5, "word"]
my_list print(type(my_list))
<class 'list'>
Lists are mutable: You can change the contents in the list as shows below
python
= [1, 2.5, "word"]
my_list print("Original List:", my_list)
Original List: [1, 2.5, 'word']
0] = 20
my_list[print("Modified List:", my_list)
Modified List: [20, 2.5, 'word']
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.
python
= (1, 2.5, "word")
my_tuple print(type(my_tuple))
<class 'tuple'>
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 {}
.
python
= {2.5, 1, "word"}
my_set print(type(my_set))
<class 'set'>
# the values in the set are my_set
{1, 2.5, 'word'}
python
= {'name': 'Alice', 'age': 25}
my_dict print(type(my_dict))
<class 'dict'>
# Using the .keys() method to view keys
= my_dict.keys()
keys print("keys in the dictionary:", keys)
keys in the dictionary: dict_keys(['name', 'age'])
# Using the .values() method to view keys
= my_dict.values()
values print("values in the dictionary:", values)
values in the dictionary: dict_values(['Alice', 25])