Contents

Python Quick Guide - Step 1 Basic Syntax and Data Types (7) - Lists (3)

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”.

  • The sum function calculates the total of all numbers in a list.
  • The min (max) function calculates the minimum (maximum) value in a list.
  • 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.
  • The sorted function can be applied not only to numeric lists but also to lists of types that have a defined order.
  • The all function returns True if all values in the list are True, otherwise it returns False.
    • It checks elements from the beginning and returns False as soon as it finds a False value.
      (The remaining elements are not checked)
  • When applied to a non-boolean list, elements are interpreted as boolean values (by applying the bool function).
  • The any function returns True if any value in the list is True, otherwise it returns False.
    • It checks elements from the beginning and returns True as soon as it finds a True value.
      (The remaining elements are not checked)
  • When applied to a non-boolean list, elements are interpreted as boolean values (by applying the bool function).
Behavior of Functions with Empty Lists
  1. sum Function
    • For an empty list ([]), the sum function returns 0.
  1. min and max Functions
    • For an empty list ([]), both min and max functions raise a ValueError.
  1. all Function
    • For an empty list ([]), the all function returns True.
      • This is because the proposition “all elements in an empty list are true” is considered true. (There are no counter-examples)
  1. any Function
    • For an empty list ([]), the any function returns False.
      • This is because the proposition “at least one element in an empty list is true” is considered false.

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"]
  1. Check if “eggs” is already in the list and display the result
  2. Add “coffee” to the end of the list
  3. Insert “rice” at the position of “bread” and remove “bread”
  4. Take out the first and last items from the list, display them, and create a new list with the remaining items
  5. Add ["apples", "bananas"] to the remaining list
  6. 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]
  1. Create and display a new list with these scores sorted in ascending order
  2. Display the highest and lowest scores
  3. Display a list of the 3 lowest scores
  4. Calculate and display the sum of the 3 highest scores
  5. 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]
  1. Increase the stock quantity of “Tablet” by 5 (Get the corresponding index and update the value)
  2. Add a new product called “Smartwatch” with a stock quantity of 10 and a price of 350
  3. Apply a 20% discount to the price of “Headphones” and display the new price
  4. Display the name and total value (price * stock quantity) of the product with the highest unit price
Sample Solution

Related Content