Python Quick Guide - Step 1 Basic Syntax and Data Types (7) - Lists (3)
Series - Python Quick Guide
Contents
Info
- This course is designed to help you learn the basics of Python programming as quickly as possible through hands-on practice.
- The “Style Guide” sections primarily cover guidelines from PEP8 for writing clean Python code.
- You can run and see the results of each code example.
Feel free to experiment with the code - reloading the page will reset the content.
This is a continuation of “Step 1: Basic Syntax and Data Types”.
1.5. Organizing Data
1.5.1. Lists
Functions Applicable to Numeric Lists
sum
Function
- The
sum
function 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
sorted
function returns a list with elements sorted in ascending order.- By specifying
reverse=True
as a parameter, you can get a list in descending order. - The original list is not modified; a new list is returned.
- By specifying
- The
sorted
function 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
all
function returnsTrue
if all values in the list areTrue
, otherwise it returnsFalse
.- It checks elements from the beginning and returns
False
as soon as it finds aFalse
value.
(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
bool
function).
any
Function
- The
any
function returnsTrue
if any value in the list isTrue
, otherwise it returnsFalse
.- It checks elements from the beginning and returns
True
as soon as it finds aTrue
value.
(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
bool
function).
Behavior of Functions with Empty Lists
sum
Function- For an empty list (
[]
), thesum
function returns0
.
- For an empty list (
min
andmax
Functions- For an empty list (
[]
), bothmin
andmax
functions raise aValueError
.
- For an empty list (
all
Function- For an empty list (
[]
), theall
function 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 (
any
Function- For an empty list (
[]
), theany
function 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