Python Crash Course 3 - Lists
I’m YouKoutaku. These codes are from the book python_Crash_Course, 2nd edition. I learned python and took these notes for this book.
3.Lists
3.1 lists
1
2
3
#3.1 create a list
bicycles = ['trek','canon','redline','specialized']
print(bicycles)
1
['trek', 'cannondale', 'redline', 'specialized']
1
2
3
4
5
#3.2 access the list
print(bicycles[0])
print(bicycles[0].title())
1
2
trek
Trek
index of list is begin to 0.
1
2
3
4
5
6
#3.1.2 access the list
print(bicycles[0])
print(bicycles[1])
print(bicycles[2])
print(bicycles[3])
print(bicycles[-1])
1
2
3
4
5
trek
cannondale
redline
specialized
specialized
1
2
3
#3.1.3 using list index in a string
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)
1
My first bicycle was a Trek.
1
2
3
4
5
6
#E-3-1
names = ['yang','mo','yamada']
print(names[0])
print(names[1])
print(names[2])
1
2
3
yang
moli
yamada
1
2
3
#E-3-2
for name in names:
print(f'{name.title()}くん,明けましておめでとう~今年もよろしくお願いします.')
1
2
3
Yangくん,明けましておめでとう~今年もよろしくお願いします.
Moliくん,明けましておめでとう~今年もよろしくお願いします.
Yamadaくん,明けましておめでとう~今年もよろしくお願いします.
1
2
3
#E-3-3
ways = ['foot','car','subway']
print(f'I would like to go school by {ways[0]}')
1
I would like to go school by foot
3.2 element’s change , add and delete
1
2
3
4
5
6
#3.2.1 change the element of the list
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'toyota'
print(motorcycles)
1
2
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
1
2
3
4
#3.2.2 add a new element to the list
motorcycles.append('honda')
print(motorcycles)
#.append used to add an element to the end of the list.
1
['ducati', 'yamaha', 'suzuki', 'honda']
1
2
3
4
5
6
7
#using append
motorcycles = []
motorcycles.append('honda')
motorcycles.append('honda1')
motorcycles.append('honda2')
print(motorcycles)
1
['honda', 'honda1', 'honda2']
1
2
3
4
5
#add a new element to the middle of the list
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0,'toyota')
print(motorcycles)
#.insert need the index to insert the element.
1
['ducati', 'honda', 'yamaha', 'suzuki']
1
2
3
4
5
#3.2.3 delete an element from the list
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
#del used to delete an element by the index
1
['yamaha', 'suzuki']
1
2
3
4
5
6
7
#pop() used to delete the last element of the list and return it.
#this is a stack operation. first in, last out.
motorcycles = ['honda', 'yamaha', 'suzuki']
mine = motorcycles.pop()
print(mine)
print(motorcycles)
1
2
suzuki
['honda', 'yamaha']
1
2
3
4
#pop() also used to delete the element of the list by the index and return it.
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(1)
print(f"The first motorcycle I owned was a {first_owned.title()}.")
1
The first motorcycle I owned was a Yamaha.
del or pop()?
if you want to delete a data from the list and don’t use it in any way, use the ‘del’ if you want to continue to use it after deleting the date use the ‘pop’
1
2
3
4
5
#remove() used to delete an element by the value
motorcycles = ['honda', 'yamaha', 'suzuki', 'toyota']
motorcycles.remove('yamaha')
print(motorcycles)
1
['honda', 'suzuki', 'ducati']
1
2
3
4
5
6
7
#remove() also used to delete the element of the list by the value and return it.
motorcycles = ['honda', 'yamaha', 'suzuki', 'toyota']
too_expensive = 'yamaha'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f'\nA {too_expensive.title()} is too expensive for me.')
1
2
3
['honda', 'suzuki', 'ducati']
A Yamaha is too expensive for me.
Note: ‘remove’ can only delete the first data. If you want to delete the same value multiple times in the list, you need to use a loop that each value can be removed.
1
2
3
4
#E-3-4
names = ['mori yuto', 'yamada yatsuya', 'aizawa kaori']
for name in names:
print(f'{name.title()},May I have dinner with you?')
1
2
3
Mori Yuto,May I have dinner with you?
Yamada Yatsuya,May I have dinner with you?
Aizawa Kaori,May I have dinner with you?
1
2
3
4
5
6
7
8
9
10
#E-3-5
names = ['mori yuto', 'yamada yatsuya', 'aizawa kaori']
er = names.pop()
print(f"{er.title()} can't come. ")
names.append('murasaki haruka')
for name in names:
print(f'{name.title()},May I have dinner with you?')
1
2
3
4
Aizawa Kaori can't come.
Mori Yuto,May I have dinner with you?
Yamada Yatsuya,May I have dinner with you?
Murasaki Haruka,May I have dinner with you?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#E-3-6
names = ['mori yuto', 'yamada yatsuya', 'aizawa kaori']
er = names.pop()
print(f"{er.title()} can't come. ")
names.append('murasaki haruka')
print('I find a bigger table for dinner.')
names.insert(0,'yang zhengda')
names.insert(2,'itakura haru')
names.append('horie shin')
for name in names:
print(f'{name.title()},May I have dinner with you?')
1
2
3
4
5
6
7
8
Aizawa Kaori can't come.
I find a bigger table for dinner.
Yang Zhengda,May I have dinner with you?
Mori Yuto,May I have dinner with you?
Itakura Haru,May I have dinner with you?
Yamada Yatsuya,May I have dinner with you?
Murasaki Haruka,May I have dinner with you?
Horie Shin,May I have dinner with you?
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
#E-3-7
names = ['mori yuto', 'yamada yatsuya', 'aizawa kaori']
er = names.pop()
print(f"{er.title()} can't come. ")
names.append('murasaki haruka')
print('I find a bigger table for dinner.')
names.insert(0,'yang zhengda')
names.insert(2,'itakura haru')
names.append('horie shin')
print("I'm sorry for that the new dining table not be delivered in time, Only 2 guests can be invited.")
while len(names) > 2 :
Not_inviter = names.pop()
print(f"I sorry. {Not_inviter.title()},we can't have dinner together.")
for name in names:
print(f'{name.title()},May I have dinner with you?')
while len(names)!=0 :
del names[0]
print(names)
1
2
3
4
5
6
7
8
9
10
Aizawa Kaori can't come.
I find a bigger table for dinner.
I'm sorry for that the new dining table not be delivered in time, Only 2 guests can be invited.
I sorry. Horie Shin,we can't have dinner together.
I sorry. Murasaki Haruka,we can't have dinner together.
I sorry. Yamada Yatsuya,we can't have dinner together.
I sorry. Itakura Haru,we can't have dinner together.
Yang Zhengda,May I have dinner with you?
Mori Yuto,May I have dinner with you?
[]
3.3 list sort
1
2
3
4
5
#3.3.1 sort() used to sort the list.
cars = ["bmw", "audi", "toyota","subaru"]
cars.sort()
print(cars)
#actually change the order of the list, can't go back.
1
['audi', 'bmw', 'subaru', 'toyota']
1
2
3
4
#sort list in reverse alphabetical order.
cars = ["bmw", "audi", "toyota","subaru"]
cars.sort(reverse=True)
print(cars)
1
['toyota', 'subaru', 'bmw', 'audi']
1
2
3
4
5
6
7
8
#3.3.2 sorted() used to sort the list, but the list is not changed.
cars = ["bmw", "audi", "toyota","subaru"]
print("original:")
print(cars)
print("\nthe sorted:")
print( sorted(cars) )
print("\noriginal:")
print(cars)
1
2
3
4
5
6
7
8
original:
['bmw', 'audi', 'toyota', 'subaru']
the sorted:
['audi', 'bmw', 'subaru', 'toyota']
original:
['bmw', 'audi', 'toyota', 'subaru']
** 注意 ** 大文字と小文字が同時ある時,複雑になる.
1
2
3
cars = ["bmw", "audi", "toyota","subaru","Biyadi"]
cars.sort()
print(cars)
1
['Biyadi', 'audi', 'bmw', 'subaru', 'toyota']
1
2
3
4
5
6
7
#3.3.3 reverse() used to sort the list in reverse order.
cars = ["bmw", "audi", "toyota","subaru"]
print(cars)
cars.reverse()
print(cars)
#it is just to reverse the order of original list.
#list is changed in reverse
1
2
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
1
2
3
#3.3.4 len() used to get the length of the list.
cars = ["bmw", "audi", "toyota","subaru"]
print(len(cars))
1
4
1
2
cars = []
print(len(cars))
1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#E-3-8
place = ["h北海道", "k鎌倉", "t京都", "y横浜", "o沖縄"]
print(sorted(place))
print(place)
print(sorted(place,reverse=True))
print(place)
place.reverse()
print(place)
place.reverse()
print(place)
place.sort()
print(place)
place.sort(reverse=True)
print(place)
1
2
3
4
5
6
7
8
['h北海道', 'k鎌倉', 'o沖縄', 't京都', 'y横浜']
['h北海道', 'k鎌倉', 't京都', 'y横浜', 'o沖縄']
['y横浜', 't京都', 'o沖縄', 'k鎌倉', 'h北海道']
['h北海道', 'k鎌倉', 't京都', 'y横浜', 'o沖縄']
['o沖縄', 'y横浜', 't京都', 'k鎌倉', 'h北海道']
['h北海道', 'k鎌倉', 't京都', 'y横浜', 'o沖縄']
['h北海道', 'k鎌倉', 'o沖縄', 't京都', 'y横浜']
['y横浜', 't京都', 'o沖縄', 'k鎌倉', 'h北海道']
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
#E-3-9
names = ['mori yuto', 'yamada yatsuya', 'aizawa kaori']
er = names.pop()
print(f"{er.title()} can't come. ")
names.append('murasaki haruka')
print('I find a bigger table for dinner.')
names.insert(0,'yang zhengda')
names.insert(2,'itakura haru')
names.append('horie shin')
print("I'm sorry for that the new dining table not be delivered in time, Only 2 guests can be invited.")
while len(names) > 2 :
Not_inviter = names.pop()
print(f"I sorry. {Not_inviter.title()},we can't have dinner together.")
for name in names:
print(f'{name.title()},May I have dinner with you?')
print(f"Today I will invite {len(names)} guests.")
while len(names)!=0 :
del names[0]
print(names)
1
2
3
4
5
6
7
8
9
10
11
Aizawa Kaori can't come.
I find a bigger table for dinner.
I'm sorry for that the new dining table not be delivered in time, Only 2 guests can be invited.
I sorry. Horie Shin,we can't have dinner together.
I sorry. Murasaki Haruka,we can't have dinner together.
I sorry. Yamada Yatsuya,we can't have dinner together.
I sorry. Itakura Haru,we can't have dinner together.
Yang Zhengda,May I have dinner with you?
Mori Yuto,May I have dinner with you?
Today I will invit 2 guests.
[]
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
#E-3—10
Country = ["Japan", "United States", "France", "Russia", "India"]
My_Country = "China"
Country.append(My_Country)#リストの最後に加える
print(Country)
Country.insert(1, "England")#リストの中間に加える
print(Country)
del Country[-1]#リストの要素を削除
print(Country)
now1 = Country.pop()#スタックstack削除
print(now1)
print(Country)
now2 = Country.pop(0)#要素を削除しながら,関数のreturn値として使う
print(now2)
print(Country)
Country.remove("England")#値を指定し削除する
print(Country)
print(len(Country))#リストの長さ
print(sorted(Country))#臨時的に並べ替え
print(Country)
print(sorted(Country,reverse=True))#臨時的に逆並べ替え
print(Country)
Country.reverse()#リストの反転
print(Country)
Country.sort()#永久的に並べ替え
print(Country)
Country.sort(reverse=True)#永久的に逆順並べ替え
print(Country)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
['Japan', 'United States', 'France', 'Russia', 'India', 'China']
['Japan', 'Engliand', 'United States', 'France', 'Russia', 'India', 'China']
['Japan', 'Engliand', 'United States', 'France', 'Russia', 'India']
India
['Japan', 'Engliand', 'United States', 'France', 'Russia']
Japan
['Engliand', 'United States', 'France', 'Russia']
['United States', 'France', 'Russia']
['United States', 'France', 'Russia']
3
['France', 'Russia', 'United States']
['United States', 'France', 'Russia']
['United States', 'Russia', 'France']
['United States', 'France', 'Russia']
['Russia', 'France', 'United States']
['France', 'Russia', 'United States']
['United States', 'Russia', 'France']
1
2
3
4
5
#3.4 avoid the error of index out of range.
#E-3-11
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
#list index out of range
1
2
3
4
5
6
7
8
9
10
11
12
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [2], in <cell line: 4>()
1 #3.4 avoid the error of index out of range.
2 #E-3-11
3 motorcycles = ['honda', 'yamaha', 'suzuki']
----> 4 print(motorcycles[3])
IndexError: list index out of range
index[-1]is the last element of list
1
2
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1])
1
suzuki
This post is licensed under CC BY 4.0 by the author.