13  Data types in Python

13.1 Fundamental Data Types in Python

13.1.1 Numeric Types:

Integers (int):

Integers represent whole numbers, both positive and negative, without a fractional component.

Code
python
x=12
type(x)
<class 'int'>

Floating Point Numbers (float):

Used for decimal or floating-point numbers.

Code
python
x=12.5
type(x)
<class 'float'>

13.1.2 Boolean (bool):

  • Represents two values: True or False. Booleans are integral in control flow and decision-making structures in Python.

Boolean Example

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.

Code
python
# Boolean example

# Assigning boolean values
light_on = True
is_raining = False

# 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.
Code
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.

13.1.3 Sequences:

Strings (str):

A sequence of characters, enclosed in single, double, or triple quotes.

Code
python
my_string = "Hello, Python!"
print(type(my_string))  
<class 'str'>

Lists:

Ordered, mutable collections of items of mixed data types.

Code
python
my_list = [1, 2.5, "word"]
print(type(my_list))  
<class 'list'>

Lists are mutable: You can change the contents in the list as shows below

Code
python
my_list = [1, 2.5, "word"]
print("Original List:", my_list)
Original List: [1, 2.5, 'word']
Code
my_list[0] = 20
print("Modified List:", my_list)
Modified List: [20, 2.5, 'word']

Tuples:

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.

Code
python
my_tuple = (1, 2.5, "word")
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.

13.1.4 Sets:

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 {}.

Code
python
my_set = {2.5, 1, "word"} 
print(type(my_set))
<class 'set'>
Code
my_set # the values in the set are 
{1, 2.5, 'word'}

13.1.5 Dictionaries (dict):

  • Key-value pairs that are unordered, mutable, and indexed. Dictionaries are essential for data storage and retrieval operations where relationship mapping is crucial.
Code
python
my_dict = {'name': 'Alice', 'age': 25}
print(type(my_dict))  
<class 'dict'>
Code
# 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'])
Code
# 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])