Learn Code Quiz: Data Science | Looping and Selecting Elements in Lists, Dictionaries, and Tuples

preview_player
Показать описание
Description:

For you to try in Jupyter notebook and practice

my_list = [1, 2, 3, 4, 5]
# List demonstration
my_list = [1, 2, 3, 4, 5]
# Looping through the list using a for loop
print("Looping through the list:")
for item in my_list:
print(item)

Output: Looping through the list:
1
2
3
4
5

my_list = [1, 2, 3, 4, 5]
# Selecting elements from the list
print("\nSelecting elements from the list:")
# Select the element at index 0
print("Element at index 0:", my_list[0])

Output: Selecting elements from the list:
Output: Element at index 0: 1

my_list = [1, 2, 3, 4, 5]
# Select the element at index 2
print("Element at index 2:", my_list[2])

Output: Element at index 2: 3

my_list = [1, 2, 3, 4, 5]
# Selecting a range of elements using slicing
print("\nSlicing the list:")
# Select elements from index 1 to 3 (exclusive)
print("Elements from index 1 to 3:", my_list[1:3])

Output: Slicing the list:

Elements from index 1 to 3: [2, 3]
my_list = [1, 2, 3, 4, 5]
# Select all elements starting from index 2 till the end
print("Elements from index 2 to end:", my_list[2:])

Output: Elements from index 2 to end: [3, 4, 5]

my_list = [1, 2, 3, 4, 5]
# Select all elements from the beginning to index 3 (exclusive)
print("Elements from beginning to index 3:", my_list[:3])

Output: Elements from beginning to index 3: [1, 2, 3]

Looping and Selecting Elements in Dictionaries:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Dictionary demonstration
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Looping through the dictionary using a for loop
print("Looping through the dictionary:")
print(key, ":", value)

Output: Looping through the dictionary:
Output: name : John
Output: age : 30
Output: city : New York

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Selecting elements from the dictionary
print("\nSelecting elements from the dictionary:")
# Select the value for the 'name' key
print("Name:", my_dict['name'])

Output: Selecting elements from the dictionary:
Output: Name: John

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Select the value for the 'age' key
print("Age:", my_dict['age'])

Output: Age: 30

Note: There's no concept of slicing for dictionaries as they are not ordered.
Looping and Selecting Elements in Tuples:

my_tuple = (10, 20, 30, 40, 50)
# Tuple demonstration
my_tuple = (10, 20, 30, 40, 50)
# Looping through the tuple using a for loop
print("Looping through the tuple:")
for item in my_tuple:
print(item)

Output: Looping through the tuple:
10
20
30
40
50

my_tuple = (10, 20, 30, 40, 50)
# Selecting elements from the tuple
print("\nSelecting elements from the tuple:")
# Select the element at index 0
print("Element at index 0:", my_tuple[0])

Output: Selecting elements from the tuple:
Output: Element at index 0: 10

# Select the element at index 2
print("Element at index 2:", my_tuple[2])

Output: Element at index 2: 30

my_tuple = (10, 20, 30, 40, 50)
# Selecting a range of elements using slicing
print("\nSlicing the tuple:")
# Select elements from index 1 to 3 (exclusive)
print("Elements from index 1 to 3:", my_tuple[1:3])

Output: Slicing the tuple:
Output: Elements from index 1 to 3: (20, 30)

my_tuple = (10, 20, 30, 40, 50)
# Select all elements starting from index 2 till the end
print("Elements from index 2 to end:", my_tuple[2:])

Output: Elements from index 2 to end: (30, 40, 50)

my_tuple = (10, 20, 30, 40, 50)
# Select all elements from the beginning to index 3 (exclusive)
print("Elements from beginning to index 3:", my_tuple[:3])

Output: Elements from beginning to index 3: (10, 20, 30)

Рекомендации по теме
welcome to shbcf.ru