Quick Overview
`lst[0]` raises `IndexError` on an empty list, where `lst[:1]` quietly returns an empty list. Negative indices count from the end, making `lst[-1]` the last element.
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!
A function with no `return` hands back `None`, so a forgotten one surfaces as a `TypeError` at the call site rather than at the line that actually caused it.
`minutes * 60` returns an `int` when given an `int`, while `/` always produces a float regardless of the operands. Multiplication preserves the type; division does not.
`base * height / 2` always yields a float in Python 3 because `/` never returns an int, even for whole numbers. `//` floors instead, which is almost never what an area wants.