-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_lists.py
More file actions
35 lines (29 loc) · 928 Bytes
/
03_lists.py
File metadata and controls
35 lines (29 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
def lists():
"""09_lists.py - Lists examples"""
print("=== LISTS ===")
# Basic list operations
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.insert(1, "mango")
print("After append/insert:", fruits)
# List slicing
print("First two: ", fruits[:2])
print("Last two: ", fruits[-2:])
print("Every other item: ", fruits[::2])
# Common list methods
numbers = [5, 2, 8, 1, 9]
numbers.sort()
print("Sorted: ", numbers)
numbers.reverse()
print("Reversed: ", numbers)
numbers.remove(8)
print("After remove(8): ", numbers)
print("Count of 2: ", numbers.count(2))
# List comprehension (very Pythonic)
squares = [x ** 2 for x in range(1, 6)]
print("Squares (comp): ", squares)
def main():
lists()
if __name__ == '__main__':
main()