Python Interactive Guide - Step 1 Basic Syntax and Data Types (7) - Lists (3)
Series - Python Interactive Guide
Contents
Info
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
This is a continuation of “Step 1: Basic Syntax and Data Types”.
1.5. Grouping Data
1.5.1. Lists
Functions Applicable to Numeric Lists
sum Function
- The
sumfunction calculates the total of all numbers in a list.
min and max Functions
- The
min(max) function calculates the minimum (maximum) value in a list.
sorted Function
- The
sortedfunction returns a list with elements sorted in ascending order.- By specifying
reverse=Trueas a parameter, you can get a list in descending order. - The original list is not modified; a new list is returned.
- By specifying
- The
sortedfunction can be applied not only to numeric lists but also to lists of types that have a defined order.
Functions Applicable to Boolean Lists
all Function
- The
allfunction returnsTrueif all values in the list areTrue, otherwise it returnsFalse.- It checks elements from the beginning and returns
Falseas soon as it finds aFalsevalue.
(The remaining elements are not checked)
- It checks elements from the beginning and returns
- When applied to a non-boolean list, elements are interpreted as boolean values (by applying the
boolfunction).
any Function
- The
anyfunction returnsTrueif any value in the list isTrue, otherwise it returnsFalse.- It checks elements from the beginning and returns
Trueas soon as it finds aTruevalue.
(The remaining elements are not checked)
- It checks elements from the beginning and returns
- When applied to a non-boolean list, elements are interpreted as boolean values (by applying the
boolfunction).
Behavior of Functions with Empty Lists
sumFunction- For an empty list (
[]), thesumfunction returns0.
- For an empty list (
minandmaxFunctions- For an empty list (
[]), bothminandmaxfunctions raise aValueError.
- For an empty list (
allFunction- For an empty list (
[]), theallfunction returnsTrue.- This is because the proposition “all elements in an empty list are true” is considered true. (There are no counter-examples)
- For an empty list (
anyFunction- For an empty list (
[]), theanyfunction returnsFalse.- This is because the proposition “at least one element in an empty list is true” is considered false.
- For an empty list (
List Summary
Let’s solve some exercises about the list operations we’ve covered.
📚Exercise 1: Shopping List Management
You are creating a program to manage a shopping list. Perform the following operations in order and display the final shopping list.
- Initial shopping list:
["milk", "bread", "eggs", "cheese", "butter"]
- Check if “eggs” is already in the list and display the result
- Add “coffee” to the end of the list
- Insert “rice” at the position of “bread” and remove “bread”
- Take out the first and last items from the list, display them, and create a new list with the remaining items
- Add
["apples", "bananas"]to the remaining list - Display the number of items in the final shopping list and the alphabetically sorted shopping list
Sample Solution
📚Exercise 2: Grade Data Operations
You are working with math grades for a class. Perform the following operations in order:
- Test scores:
[85, 92, 78, 65, 88, 72, 90, 81]
- Create and display a new list with these scores sorted in ascending order
- Display the highest and lowest scores
- Display a list of the 3 lowest scores
- Calculate and display the sum of the 3 highest scores
- Calculate and display the average score
Sample Solution
📚Exercise 3: Inventory Management System
For the inventory management system below where product names, stock quantities, and prices are stored in separate lists, perform the specified operations in order:
- Product name list:
["Phone", "Laptop", "Tablet", "Headphones", "Charger"] - Stock quantity list:
[15, 8, 12, 35, 40] - Price list:
[800, 1200, 600, 150, 25]
- Increase the stock quantity of “Tablet” by 5 (Get the corresponding index and update the value)
- Add a new product called “Smartwatch” with a stock quantity of 10 and a price of 350
- Apply a 20% discount to the price of “Headphones” and display the new price
- Display the name and total value (price * stock quantity) of the product with the highest unit price