W3school_pythontutorial(5)-Tuples
W3school에서 python 내실 다지기 목록
- W3school_pythontutorial(1) Syntax ~ Strings
- W3school_pythontutorial(2)-String_Methods
- W3school_pythontutorial(3)-Lists
- W3school_pythontutorial(4) Booleans ~ Arrays
- W3school_pythontutorial(5)-Tuples
W3school_pythontutorial(5)-Tuples
내실 다지기 : w3school 사이트에서 python tutorial을 처음부터 끝까지 하는 목표로 시작하는 포스팅으로 제공해주는 목차 순서대로 진행한다. 간단한 내용은 제공해준 예제만 실행해본다. 활용해볼 예제는 추가로 간단한 예제를 만들어 실행하는 코드까지 작성하며 내실을 다진다.
Lists
mylist = ["apple", "banana", "cherry"]
print(mylist)
['apple', 'banana', 'cherry']
# Allow Duplicates
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
['apple', 'banana', 'cherry', 'apple', 'cherry']
# List Length
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
3
# List Items - Data Types
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
print(list1)
print(list2)
print(list3)
['apple', 'banana', 'cherry']
[1, 5, 7, 9, 3]
[True, False, False]
list1 = ["abc", 34, True, 40, "male"]
print(list1)
['abc', 34, True, 40, 'male']
# type()
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
<class 'list'>
# The list() Constructor
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
['apple', 'banana', 'cherry']
Access List Items
# Access Items
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
banana
# Negative Indexing
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
cherry
# Range of Indexes
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
['cherry', 'orange', 'kiwi']
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
['apple', 'banana', 'cherry', 'orange']
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
['cherry', 'orange', 'kiwi', 'melon', 'mango']
# Range of Negative Indexes
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
['orange', 'kiwi', 'melon']
# Check if Item Exists
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
Yes, 'apple' is in the fruits list
Change List Items
# Change Item Value
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
['apple', 'blackcurrant', 'cherry']
# Change a Range of Item Values
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)
['apple', 'blackcurrant', 'watermelon', 'cherry']
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
['apple', 'watermelon']
# Insert Items
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
['apple', 'banana', 'watermelon', 'cherry']
Add List Items
# Append Items
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
['apple', 'banana', 'cherry', 'orange']
# Insert Items
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
['apple', 'orange', 'banana', 'cherry']
# Extend List
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
# Add Any Iterable 튜플 리스트 등 인덱싱할 수 있는 것은 다 된다.
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
['apple', 'banana', 'cherry', 'kiwi', 'orange']
Remove List Items
# Remove Specified Item
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
['apple', 'cherry']
# Remove Specified Index
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
['apple', 'cherry']
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
['apple', 'banana']
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
['banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
del thislist
print(thislist)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [36], in <cell line: 3>()
1 thislist = ["apple", "banana", "cherry"]
2 del thislist
----> 3 print(thislist)
NameError: name 'thislist' is not defined
# Clear the List
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
[]
Loop Lists
# Loop Through a List
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
apple
banana
cherry
# Loop Through the Index Numbers
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
apple
banana
cherry
# Using a While Loop
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1
apple
banana
cherry
# Looping Using List Comprehension
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
apple
banana
cherry
[None, None, None]
List Comprehension
- The Syntax
newlist = [expression for item in iterable if condition == True]
# List Comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
['apple', 'banana', 'mango']
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
['apple', 'banana', 'mango']
# Condition
newlist = [x for x in fruits if x != "apple"]
print(newlist)
['banana', 'cherry', 'kiwi', 'mango']
newlist = [x for x in fruits]
print(newlist)
['apple', 'banana', 'cherry', 'kiwi', 'mango']
# Iterable
newlist = [x for x in range(10)]
print(newlist)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
newlist = [x for x in range(10) if x < 5]
print(newlist)
[0, 1, 2, 3, 4]
# Expression
newlist = [x.upper() for x in fruits]
print(newlist)
['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
newlist = ['hello' for x in fruits]
print(newlist)
['hello', 'hello', 'hello', 'hello', 'hello']
newlist = [x if x != "banana" else "orange" for x in fruits]
print(newlist)
['apple', 'orange', 'cherry', 'kiwi', 'mango']
Sort Lists
# Sort List Alphanumerically
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
['banana', 'kiwi', 'mango', 'orange', 'pineapple']
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
[23, 50, 65, 82, 100]
# Sort Descending
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
[100, 82, 65, 50, 23]
# Customize Sort Function
def myfunc(n):
return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
print(thislist)
[50, 65, 23, 82, 100]
# Case Insensitive Sort
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort()
print(thislist)
['Kiwi', 'Orange', 'banana', 'cherry']
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key = str.lower)
print(thislist)
['banana', 'cherry', 'Kiwi', 'Orange']
# Reverse Order
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist)
['cherry', 'Kiwi', 'Orange', 'banana']
Copy Lists
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
['apple', 'banana', 'cherry']
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
['apple', 'banana', 'cherry']
Join Lists
# Join Two Lists
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
['a', 'b', 'c', 1, 2, 3]
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
['a', 'b', 'c', 1, 2, 3]
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
['a', 'b', 'c', 1, 2, 3]
List Methods
# count()
fruits = ['apple', 'banana', 'cherry']
x = fruits.count("cherry")
print(x)
1
# Definition and Usage : list.count(value)
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = points.count(9)
print(x)
2
# index()
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
2
# Definition and Usage : list.index(elmnt)
fruits = [4, 55, 64, 32, 16, 32]
x = fruits.index(32)
print(x)
3