|
| 1 | +## Python Data Types: List, Set, Tuple, and Dictionary |
| 2 | + |
| 3 | +### 1. List |
| 4 | +Lists are used to store multiple items in a single variable.List items are ordered, changeable, and allow duplicate values. |
| 5 | +Lists are created using square brackets [] |
| 6 | + |
| 7 | +```python |
| 8 | +# Creating a list |
| 9 | +fruits = ["apple", "banana", "cherry"] |
| 10 | +print(type(fruits)) #Class List |
| 11 | +print(lent(fruits)) #2 |
| 12 | + |
| 13 | +# Accessing list items |
| 14 | +print(fruits[0]) # Output: apple |
| 15 | + |
| 16 | +# Adding an item to the list |
| 17 | +fruits.append("orange") |
| 18 | +print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange'] |
| 19 | +``` |
| 20 | + |
| 21 | +## Here are some practice questions for Python lists: |
| 22 | + |
| 23 | +*Practice Questions* |
| 24 | +## Basic Operations: |
| 25 | + |
| 26 | +Create a list of your favorite fruits. Add a new fruit to the list and then remove one fruit from the list. Print the updated list. |
| 27 | +Write a Python program to find the length of a list without using the built-in len() function. |
| 28 | + |
| 29 | +## Accessing Elements: |
| 30 | + |
| 31 | +Given the list numbers = [10, 20, 30, 40, 50], write a Python program to access the third element. |
| 32 | +Write a Python program to access the last element of a list using negative indexing. |
| 33 | + |
| 34 | +## Slicing and Modifying Lists: |
| 35 | + |
| 36 | +Write a Python program to reverse a list using slicing. |
| 37 | +Given a list colors = ["red", "green", "blue", "yellow", "purple"], write a program to replace "blue" with "orange". |
| 38 | + |
| 39 | +## List Methods: |
| 40 | + |
| 41 | +Write a Python program to count the occurrences of a specific element in a list. |
| 42 | +Create a list of numbers and write a Python program to sort the list in descending order. |
| 43 | + |
| 44 | +## Comprehensions: |
| 45 | + |
| 46 | +Write a list comprehension that creates a list of the squares of all even numbers from 0 to 20. |
| 47 | +Given a list of strings, write a list comprehension to create a new list with only the strings that have more than 3 characters. |
| 48 | + |
| 49 | +## Nested Lists: |
| 50 | + |
| 51 | +Create a nested list (a list inside another list) and write a program to access an element from the inner list. |
| 52 | +Write a Python program to flatten a nested list (convert it into a single list with no nested lists). |
| 53 | + |
| 54 | +## Advanced Operations: |
| 55 | + |
| 56 | +Write a Python program to remove all occurrences of a specific value from a list without using a loop. |
| 57 | +Given two lists, write a Python program to merge them into a dictionary where elements from the first list are keys and elements from the second list are values. |
| 58 | + |
| 59 | +These questions cover various aspects of working with lists in Python and should provide a good range of practice. |
| 60 | + |
0 commit comments