Numeric Types
Numeric Python types refer to the fundamental data types in Python that handle numerical values. These types play a crucial role in mathematical and scientific computations, making Python a powerful language for data analysis, machine learning, and scientific research.
Integer (int)
The integer class represents this value, encompassing both positive and negative whole numbers without fractions or decimals. In Python, there exists no restriction on the length of an integer value, allowing for limitless magnitude.
Float (float)
The float class represents this value, denoting a real number with a floating-point representation characterised by a decimal point. Optionally, one can append the character ‘e’ or ‘E’ followed by a positive or negative integer to indicate scientific notation.
-
Complex (complex)
The complex class represents a complex number specified in the form of (real part) + (imaginary part)j. An illustrative example is 2 + 3j.
For example:
This code demonstrates how to determine the data type of variables in Python using the type() function. It prints the data types of three variables: a (integer), b (float), and c (complex). The output shows the respective data types for each variable.
Python
a = 5
print(“Type of a: “, type(a))
b = 5.0
print(“nType of b: “, type(b))
c = 2 + 4j
print(“nType of c: “, type(c))
Output:
Type of a: <class ‘int’>
Type of b: <class ‘float’>
Type of c: <class ‘complex’>
Sequence Types
The sequence data type in Python refers to an ordered collection of either similar or different data types. Sequences provide a structured and efficient means of storing multiple values.
-
List (list)
Lists are mutable sequences, allowing the modification of elements. Here’s an example that demonstrates the use of a list to store and manipulate a sequence of numbers:
# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]
# Accessing elements in the list
first_element = numbers[0]
second_element = numbers[1]
# Modifying elements in the list
numbers[2] = 10
# Adding elements to the list
numbers.append(6)
# Removing elements from the list
removed_element = numbers.pop(3)
# Iterating through the list
for num in numbers:
print(num)
# Checking if an element is in the list
is_present = 7 in numbers
# Finding the length of the list
list_length = len(numbers)
# Slicing the list
subset = numbers[1:4]
# Concatenating two lists
other_numbers = [7, 8, 9]
combined_list = numbers + other_numbers
# Sorting the list
numbers.sort()
# Reversing the list
numbers.reverse()
# Clearing all elements from the list
numbers.clear()
# Printing the results
print(“List after modifications:”, numbers)
print(“Removed element:”, removed_element)
print(“Is 7 present in the list?”, is_present)
print(“Length of the list:”, list_length)
print(“Subset of the list:”, subset)
print(“Combined list:”, combined_list)
This example covers various operations on a list, including accessing elements, modifying the list, adding and removing elements, iterating through the list, checking membership, finding the length, slicing, concatenating, sorting, reversing, and clearing the list.
-
Tuple (tuple)
Tuples are immutable sequences, and once created, their elements cannot be changed. Here’s an example demonstrating the use of a tuple:
Examples:
# Creating a tuple
fruits_tuple = (‘apple’, ‘banana’, ‘orange’, ‘grape’)
# Accessing elements in the tuple
first_fruit = fruits_tuple[0]
second_fruit = fruits_tuple[1]
# Iterating through the tuple
for fruit in fruits_tuple:
print(fruit)
# Checking if an element is in the tuple
is_present = ‘kiwi’ in fruits_tuple
# Finding the length of the tuple
tuple_length = len(fruits_tuple)
# Slicing the tuple
subset = fruits_tuple[1:3]
# Concatenating two tuples
other_fruits_tuple = (‘pear’, ‘melon’)
combined_tuple = fruits_tuple + other_fruits_tuple
# Multiplying the tuple
repeated_tuple = fruits_tuple * 2
# Nested tuple
nested_tuple = (‘red’, (‘green’, ‘blue’), ‘yellow’)
# Printing the results
print(“First fruit:”, first_fruit)
print(“Is ‘kiwi’ present in the tuple?”, is_present)
print(“Length of the tuple:”, tuple_length)
print(“Subset of the tuple:”, subset)
print(“Combined tuple:”, combined_tuple)
print(“Repeated tuple:”, repeated_tuple)
print(“Nested tuple:”, nested_tuple)
This example covers various operations on a tuple, including accessing elements, iterating through the tuple, checking membership, finding the length, slicing, concatenating, multiplying, and using nested tuples. Note that unlike lists, tuples are immutable, meaning their elements cannot be modified after creation.
-
String (str)
Strings are not only a sequence type but also serve as the primary text type in Python.
Examples:
# Creating a string
message = “Hello, Python!”
# Accessing characters in the string
first_char = message[0]
second_char = message[7]
# Iterating through the string
for char in message:
print(char, end=’ ‘)
# Concatenating strings
greeting = “Hello, “
name = “Alice”
full_greeting = greeting + name + “!”
# Finding the length of the string
message_length = len(message)
# Checking if a substring is present in the string
is_present = “Python” in message
# String methods
uppercase_message = message.upper()
lowercase_message = message.lower()
capitalized_message = message.capitalize()
# String formatting
formatted_message = “My name is {} and I am {} years old.”.format(“Bob”, 25)
# String slicing
substring = message[7:13]
# Repeating a string
repeated_message = message * 3
# Escaping special characters
escaped_string = “This is a newline character:nSee?”
# Printing the results
print(“nFirst character:”, first_char)
print(“Concatenated greeting:”, full_greeting)
print(“Length of the string:”, message_length)
print(“Is ‘Python’ present in the string?”, is_present)
print(“Uppercase:”, uppercase_message)
print(“Lowercase:”, lowercase_message)
print(“Capitalized:”, capitalized_message)
print(“Formatted string:”, formatted_message)
print(“Substring:”, substring)
print(“Repeated string:”, repeated_message)
print(“Escaped string:”, escaped_string)
# Creating a string
message = “Hello, Python!”
# Accessing characters in the string
first_char = message[0]
second_char = message[7]
# Iterating through the string
for char in message:
print(char, end=’ ‘)
# Concatenating strings
greeting = “Hello, “
name = “Alice”
full_greeting = greeting + name + “!”
# Finding the length of the string
message_length = len(message)
# Checking if a substring is present in the string
is_present = “Python” in message
# String methods
uppercase_message = message.upper()
lowercase_message = message.lower()
capitalized_message = message.capitalize()
# String formatting
formatted_message = “My name is {} and I am {} years old.”.format(“Bob”, 25)
# String slicing
substring = message[7:13]
# Repeating a string
repeated_message = message * 3
# Escaping special characters
escaped_string = “This is a newline character:nSee?”
# Printing the results
print(“nFirst character:”, first_char)
print(“Concatenated greeting:”, full_greeting)
print(“Length of the string:”, message_length)
print(“Is ‘Python’ present in the string?”, is_present)
print(“Uppercase:”, uppercase_message)
print(“Lowercase:”, lowercase_message)
print(“Capitalized:”, capitalized_message)
print(“Formatted string:”, formatted_message)
print(“Substring:”, substring)
print(“Repeated string:”, repeated_message)
print(“Escaped string:”, escaped_string)
-
Set Types
In Python, a set is a versatile data type representing an unordered collection that is both iterable and mutable while also ensuring the absence of duplicate elements. Sets provide a flexible and efficient way to manage unique values without concern for their order, as the arrangement of elements within a set is undefined. A set can encompass various data types, making it a dynamic container for distinct elements.
Example:
# create a set named student_id
student_id = {112, 114, 116, 118, 115}
# display student_id elements
print(student_id)
# display type of student_id
print(type(student_id))
Run Code
Output
{112, 114, 115, 116, 118}
<class ‘set’>
Dictionary Types
In Python, a dictionary is a dynamic and versatile data type that serves as an ordered collection of items. Unlike other sequential data structures, a Python dictionary stores elements in key/value pairs, providing a flexible and efficient means of organising and retrieving information
Example:
# create a dictionary named capital_city
capital_city = {‘Nepal’: ‘Kathmandu’, ‘Italy’: ‘Rome’, ‘England’: ‘London’}
print(capital_city)
Output
{‘Nepal’: ‘Kathmandu’, ‘Italy’: ‘Rome’, ‘England’: ‘London’}
In the above example, we have created a dictionary named capital_city. Here,
- Keys are ‘Nepal’, ‘Italy’, ‘England’
- Values are ‘Kathmandu’, ‘Rome’, ‘London’
-
Boolean
The Boolean type in Python has two default values: True and False. These values play a crucial role in evaluating the truthfulness or falsity of a given statement. This principle is illustrated in the programming language’s documentation. The value False can be denoted by either 0 or the letter “F,” whereas True can be represented by any non-zero value.
For example:
# Python program to check the boolean type
print(type(True))
print(type(False))
print(false)
Output:
<class ‘bool’>
<class ‘bool’>
NameError: name ‘false’ is not defined