Lesson · 40 min · Free
Python Lists Essentials
Python Lists Essentials Python Lists Essentials Welcome to the "Python Lists Essentials" lesson, a crucial component of your "Python Programming - Basics" course. For students in pharmacy and biotechnology, understanding
Python Lists Essentials
Welcome to the "Python Lists Essentials" lesson, a crucial component of your "Python Programming - Basics" course. For students in pharmacy and biotechnology, understanding how to effectively manage and manipulate collections of data is paramount. Python lists offer a versatile and intuitive way to store ordered sequences of items, making them indispensable for tasks ranging from managing patient drug regimens to analyzing genomic sequences or experimental results. A Python list is an ordered, mutable, and heterogeneous collection of items. "Ordered" means that the elements maintain a specific sequence, and their position (index) can be used to access them. "Mutable" implies that you can change the contents of a list after it has been created – adding, removing, or modifying elements. "Heterogeneous" signifies that a list can contain items of different data types (e.g., integers, strings, floats, even other lists) within the same collection. This flexibility is incredibly powerful for scientific and clinical data handling.
Creating and Accessing Lists
Creating a list is straightforward; you enclose a comma-separated sequence of items within square brackets [] . Accessing individual elements or slices of a list is done using indexing. Python uses zero-based indexing, meaning the first element is at index 0, the second at index 1, and so on. Negative indices can be used to access elements from the end of the list, where -1 refers to the last element. # Creating a list of drug names drug_names = ["Aspirin", "Paracetamol", "Insulin", "Metformin"] print(f"Original drug list: {drug_names}") # Accessing elements by index first_drug = drug_names[0] print(f"First drug: {first_drug}") # Output: First drug: Aspirin third_drug = drug_names[2] print(f"Third drug: {third_drug}") # Output: Third drug: Insulin last_drug = drug_names[-1] print(f"Last drug: {last_drug}") # Output: Last drug: Metformin # Creating a list with mixed data types (e.g., patient data) patient_record = ["John Doe", 34, 78.5, ["Hypertension", "Diabetes"]] print(f"Patient record: {patient_record}") print(f"Patient name: {patient_record[0]}") print(f"Patient age: {patient_record[1]}") Lists also support slicing, which allows you to extract a sub-sequence of elements. The syntax for slicing is list[start:end:step] . The start index is inclusive, while the end index is exclusive. The step parameter (optional) determines the increment between elements. # List of experimental readings over time temperature_readings = [25.1, 25.3, 25.2, 25.5, 25.4, 25.6, 25.3, 25.7, 25.8, 25.6] print(f"All temperature readings: {temperature_readings}") # Get the first three readings early_readings = temperature_readings[0:3] # or temperature_readings[:3] print(f"Early readings (first 3): {early_readings}") # Output: [25.1, 25.3, 25.2] # Get readings from index 4 to the end later_readings = temperature_readings[4:] print(f"Later readings (from index 4): {later_readings}") # Output: [25.4, 25.6, 25.3, 25.7, 25.8, 25.6] # Get readings with a step of 2 every_other_reading = temperature_readings[::2] print(f"Every other reading: {every_other_reading}") # Output: [25.1, 25.2, 25.4, 25.3, 25.8] # Reverse the list reversed_readings = temperature_readings[::-1] print(f"Reversed readings: {reversed_readings}") The mutability of lists is a powerful feature. You can modify elements by assigning new values to specific indices, add elements using methods like append() or insert() , and remove elements with remove() , pop() , or the del statement. Understanding these operations is crucial for dynamically managing data in your applications.
Key Takeaways
Python lists are ordered, mutable, and can store heterogeneous data types. Lists are created using square brackets [] . Elements are accessed using zero-based indexing (e.g., my_list[0] ). Negative indexing accesses elements from the end (e.g., my_list[-1] ). Slicing allows extraction of sub-sequences (e.g., my_list[start:end:step] ). Lists are mutable, meaning their contents can be changed after creation.
Practice Exercise
Imagine you are tracking the molecular weights (in Da) of several proteins identified in a preliminary proteomics experiment. Create a Python list named protein_molecular_weights containing at least five different floating-point molecular weights. Then, write Python code to: Print the entire list. Access and print the molecular weight of the third protein identified. Extract and print a sub-list containing the molecular weights of the first two proteins. Update the molecular weight of the last protein to a new value (e.g., simulate a re-measurement). Print the modified list.
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →