✏️

Python Programming: Lists and Tuples

Jul 1, 2024

Python Programming: Lists and Tuples

Introduction

  • Discussing Python data structures: Lists and Tuples
  • Lists and tuples are equivalent to arrays in other programming languages.
  • These are built-in data types in Python that store sets of values.

Lists in Python

Concept

  • Built-in data type that stores multiple values.
  • Example: Storing student marks.
    • Without list: Create multiple variables for each student mark.
    • With list: Store all marks in a single list variable.

Basic Operations

  • Create a list using square brackets [].
    • Example: marks = [94.4, 87.0, 95.0, 66.0, 45.0]
  • Print a list and its type to verify.
    • Example: print(marks), print(type(marks)) prints class 'list'.

Properties

  • Lists allow indexing and slicing similar to strings.
  • Example of indexing: marks[0], marks[1]
  • Print list length using len() function.
    • Example: print(len(marks)) prints 5.

Flexibility

  • Lists can store elements of different data types.
    • Example: student = ['Karan', 85, 'Delhi', 92.5]

Mutability

  • Lists are mutable, meaning they can be changed.
    • Example: Changing list element value: student[0] = 'Arjun'

Example Operations

  1. Appending: marks.append(50.0) adds 50.0 to the end of the list.
  2. Sorting: marks.sort() sorts the list in ascending order.
  • For descending: marks.sort(reverse=True)
  1. Reversing: marks.reverse() reverses the list order.
  2. Inserting: marks.insert(index, value) inserts a value at specified index.
  3. Removing: marks.remove(value) removes the first occurrence of the value.
  4. Popping: marks.pop(index) removes element at the specified index.

Tuples in Python

Concept

  • Built-in data type similar to lists but immutable.
  • Tuples use parentheses () instead of square brackets.
    • Example: empty_tuple = (), single_tuple = (1,), multi_tuple = (1, 2, 3)
  • Tuples can store elements of different data types.

Basic Operations

  • Indexing and slicing are allowed in tuples just like lists.
    • Example: tuple[0], tuple[1:3]

Methods

  1. Index: tuple.index(value) finds the index of the first occurrence of the value.
  2. Count: tuple.count(value) counts occurrences of the value in the tuple.

Practice Questions

Question 1

  • Task: Ask the user to enter names of their three favorite movies and store them in a list.
    • Example solution: movies = [] movies.append(input('Enter first movie: ')) movies.append(input('Enter second movie: ')) movies.append(input('Enter third movie: ')) print(movies)

Question 2

  • Task: Check if a list is a palindrome.
    • Concept: A list is a palindrome if it reads the same forwards and backwards.
    • Example solution: def is_palindrome(lst): reverse_lst = lst.copy() reverse_lst.reverse() return lst == reverse_lst

Question 3

  • Task: Count the number of students with grade 'A' in a given tuple and store values in a list, then sort them.
    • Example solution: grades = ('A', 'B', 'C', 'A', 'B', 'D', 'A') print(grades.count('A')) # Outputs: 3 grades_list = list(grades) grades_list.sort() print(grades_list) # Outputs: ['A', 'A', 'A', 'B', 'B', 'C', 'D']