-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathlists.py
More file actions
50 lines (44 loc) · 1.03 KB
/
lists.py
File metadata and controls
50 lines (44 loc) · 1.03 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Define an empty list
empty = []
print(empty)
# Define a list of numbers
numbers = [1, 2, 3, 100]
print(numbers)
# Modify the list in place
numbers[3] = 200
print(numbers)
# Define a list of strings
print(["batman", "superman", "spiderman"])
# Define a list of objects with different data types
print(["Hello World", [4, 5, 6], False])
# Indexing
numbers = [1, 2, 3, 4]
print(numbers[0])
print(numbers[1])
superheroes = ["batman", "superman", "spiderman"]
print(superheroes[-1])
print(superheroes[-2])
# Slicing
numbers = [1, 2, 3, 4]
new_list = numbers[0:3]
print(new_list)
# Nested lists
mixed_types = ["Hello World", [4, 5, 6], False]
print(mixed_types[0][6])
print(mixed_types[1][2])
# Concatenation
fruits = ["apples", "grapes", "oranges"]
veggies = ["corn", "kale", "spinach"]
print(fruits + veggies)
# Built-in functions
numbers = [1, 2, 3, 4]
print(len(numbers))
# List methods
fruits = ["apples", "grapes", "oranges"]
fruits.append("blueberries")
print(fruits)
fruits.sort()
print(fruits)
numbers = [1, 2, 3, 4]
numbers.pop(2)
print(numbers)