-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.py
More file actions
92 lines (75 loc) · 2.25 KB
/
Copy patharrays.py
File metadata and controls
92 lines (75 loc) · 2.25 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Get the value of the first array item.
cars = ["Ford", "Volvo", "BMW"]
x = cars[1]
print(x)
# Modify the value of the first array item.
cars = ["Ford", "Volvo", "BMW"]
cars[0] = "Toyota"
# The length of an array can be found with the len() function.
cars = ["Ford", "Volvo", "BMW"]
x = len(cars)
print(x)
# Looping through an array.
cars = ["Ford", "Volvo", "BMW"]
for x in cars:
print(x)
# Adding an array item using the append() method.
cars = ["Ford", "Volvo", "BMW"]
cars.append("Honda")
print(cars)
# Removing an array item using the remove() method.
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Volvo")
print(cars)
# Removing an array item using the pop() method.
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
print(cars)
# Array methods()
# append() - Adds an element at the end of the list
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits)
# Add list to the list
fruits = ['apple', 'banana', 'cherry']
more_fruits = ['orange', 'mango', 'grapes']
fruits.extend(more_fruits)
print(fruits)
# clear() - Removes all the elements from the list
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)
# copy() - Returns a copy of the list
fruits = ['apple', 'banana', 'cherry']
x = fruits.copy()
print(x)
# count() - Returns the number of elements with the specified value
fruits = ['apple', 'banana', 'cherry', 'apple']
x = fruits.count('apple')
print(x)
# Return the number of times the value 9 appears in the list.
points= [1,4,2,9,7,8,9,3,1]
x = points.count(9)
print(x)
# extend() - Add the elements of a list (or any iterable), to the end of the current list
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'Volvo', 'BMW']
fruits.extend(cars)
print(fruits)
# Add a tuple to the fruits list.
fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)
print(fruits)
# index() - Returns the index of the first element with the specified value
fruits = ['apple', 'banana', 'cherry']
x = fruits.index('cherry')
print(x)
# What is the position of the value 32.
points = [4,55,64,32,16,32]
x = points.index(32)
print(x)
# Find the position of 'cherry', but start the search at position 4.
fruits = ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango','cherry']
x = fruits.index('cherry', 4)
print(x)