Master zero-based list indexing in Python to retrieve the first element of a list collection.
Learn how to convert minutes into seconds in Python using arithmetic multiplication and basic unit conversions.
Create a function get_first_value(number_list) that takes a list containing numbers and returns the first element.
Lists are Python's primary ordered collection data type. Accessing elements by 0-based index is an essential skill.
Use zero-based index [0]:
def get_first_value(number_list):
return number_list[0]
# Testing our function
print(get_first_value([1, 2, 3])) # Output: 1
print(get_first_value([80, 5, 100])) # Output: 80
print(get_first_value([-500, 0, 50])) # Output: -500
list[0]: First element.list[1]: Second element.list[-1]: Last element (negative indexing counts from the end!).list[0] on an empty list [] raises an uncaught IndexError: list index out of range. Handle empty lists safely: if not number_list: return None.Use [0] for the first element and [-1] for the last element. Next, let's convert strings to integers and vice versa!