-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_list.py
More file actions
68 lines (46 loc) · 1.69 KB
/
python_list.py
File metadata and controls
68 lines (46 loc) · 1.69 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
# Hello World program in Python
print("Example of Python Lists\n")
# In Python lists are written with square brackets.
thislist = ["apple", "banana", "cherry"]
print(thislist)
# You access the list items by referring to the index number:
print(thislist[1])
# To change the value of a specific item, refer to the index number:
thislist[1] = "blackcurrant"
print(thislist)
# You can loop through the list items by using a for loop:
# Print all items in the list, one by one:
for x in thislist:
print(x)
# To determine if a specified item is present in a list use the in keyword:
# Check if "apple" is present in the list:
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
print(x)
# To determine how many items a list have, use the len() method:
# Print the number of items in the list:
print(len(thislist))
print(x)
# To add an item to the end of the list, use the append() method:
# Using the append() method to append an item:
thislist.append("orange")
print(thislist)
# To add an item at the specified index, use the insert() method:
# Insert an item as the second position:
thislist.insert(1, "orange")
print(thislist)
# The remove() method removes the specified item:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
# The pop() method removes the specified index, (or the last item if index is not specified):
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
# The del keyword removes the specified index:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
# Using the list() constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)