Python - Data Types

5.
Explain the concept of sequences in Python and give examples of sequence data types.

In Python, a sequence is a data type that represents an ordered collection of items. Sequences are iterable and support various operations like indexing, slicing, and concatenation. Some common sequence data types in Python include str (string), list, tuple, and range.

String (str): A sequence of characters.

# Example of the str data type
string_variable = "Hello, Python!"
print("String variable:", string_variable)

List (list): A mutable sequence that can contain elements of different data types.

# Example of the list data type
list_variable = [1, 'two', 3.0]
print("List variable:", list_variable)

Tuple (tuple): An immutable sequence that can contain elements of different data types.

# Example of the tuple data type
tuple_variable = (1, 'two', 3.0)
print("Tuple variable:", tuple_variable)

Range (range): A sequence representing a range of numbers.

# Example of the range data type
range_variable = range(1, 5)
print("Range variable:", list(range_variable))
String variable: Hello, Python!
List variable: [1, 'two', 3.0]
Tuple variable: (1, 'two', 3.0)
Range variable: [1, 2, 3, 4]

In this example, variables of different sequence data types are defined and printed. Sequences are versatile and widely used in Python for storing and manipulating ordered collections of data.


6.
What is the difference between a list and a tuple in Python?

In Python, both lists and tuples are used to store collections of items, but they have key differences. The main distinction lies in mutability: lists are mutable (can be modified after creation), while tuples are immutable (cannot be changed after creation).

List (list): Mutable sequence.

# Example of a list
list_variable = [1, 'two', 3.0]
print("Original list:", list_variable)

# Modifying the list
list_variable[0] = 99
print("Modified list:", list_variable)

Tuple (tuple): Immutable sequence.

# Example of a tuple
tuple_variable = (1, 'two', 3.0)
print("Original tuple:", tuple_variable)

# Attempting to modify the tuple would result in an error
# Uncommenting the next line would raise an error
# tuple_variable[0] = 99
Original list: [1, 'two', 3.0]
Modified list: [99, 'two', 3.0]
Original tuple: (1, 'two', 3.0)

In the example, the list is modified by changing its first element, while attempting to modify the tuple would result in an error. Lists are suitable for scenarios where you need a mutable collection, while tuples provide immutability for situations where data should remain constant.


7.
Discuss the use of sets in Python and provide examples.

In Python, a set is an unordered collection of unique elements. Sets are useful for tasks that require membership testing, eliminating duplicate entries, and performing mathematical set operations. Sets are defined using curly braces ({}) or the set() constructor.

Example 1: Creating and manipulating a set.

# Creating a set
set_variable = {1, 2, 3, 3, 4, 5}
print("Original set:", set_variable)

# Adding elements to the set
set_variable.add(6)
set_variable.add(4)  # Adding a duplicate element (no effect)
print("Modified set:", set_variable)

# Removing an element from the set
set_variable.remove(2)
print("Set after removal:", set_variable)

Example 2: Performing set operations (union, intersection, difference).

# Creating two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Union of sets
union_result = set1 | set2
print("Union of sets:", union_result)

# Intersection of sets
intersection_result = set1 & set2
print("Intersection of sets:", intersection_result)

# Difference of sets
difference_result = set1 - set2
print("Difference of sets:", difference_result)
Original set: {1, 2, 3, 4, 5}
Modified set: {1, 2, 3, 4, 5, 6}
Set after removal: {1, 3, 4, 5, 6}

Union of sets: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection of sets: {4, 5}
Difference of sets: {1, 2, 3}

In these examples, sets are created, modified, and used to perform set operations. Sets provide a convenient way to work with unique elements and perform set-based operations efficiently.


8.
Explain the characteristics of the str data type in Python.

The str data type in Python represents a sequence of characters and is commonly used to store textual data. Strings are immutable, meaning their values cannot be changed after creation. They support various operations, including indexing, slicing, and string concatenation.

Example: Demonstrating characteristics of the str data type.

# Creating a string
string_variable = "Hello, Python!"

# Accessing individual characters
first_char = string_variable[0]
last_char = string_variable[-1]

# Slicing the string
substring = string_variable[7:13]

# Concatenating strings
new_string = string_variable + " Welcome!"

print("Original string:", string_variable)
print("First character:", first_char)
print("Last character:", last_char)
print("Substring:", substring)
print("Concatenated string:", new_string)
Original string: Hello, Python!
First character: H
Last character: !
Substring: Python
Concatenated string: Hello, Python! Welcome!

In this example, a string is created and various operations are performed. The string is indexed to access individual characters, sliced to obtain a substring, and concatenated with another string. The immutability of strings ensures that their values remain constant.