Variable,
Calculation,
Selection &
Loop
Function &
Data Type
Class,
Object &
GUI
Programming
File I/O,
Recursion,
Sort &
Search
Turtle
Graphics
Python for
Computational
Thinking
[자료형]을
이용한
컴퓨팅 사고력
리스트(List)
형 식 list = [ value1, value2, value3, ... ]
사용 예 >>> list = [1, 2, 3, ‘a’, ‘b’, ‘python’]
Index list[0] list[1]list[2] list[3] list[4] list[5]
값 1 23 a b python
음수 Index list[-6] list[-5]list[-4] list[-3] list[-2] list[-1]
형 식for 변수 in 리스트명 :
문장...
Source
list = [1, 2, 3, ‘a’, ‘b’, ‘python’]for i in list :
print(i)
Shell
123abpython
Shell
>>> number = [1, 2, 3, 4, 5]>>> number.append(6)>>> number[1, 2, 3, 4, 5, 6]
>>> number2 = [‘one’, ‘two’]>>> number2.append(‘three’)>>> number2[‘one’, ‘two’, ‘three’]
>>> number3 = [1, 2, 3, 4, 5]>>> number3.append([6, 7, 8, 9])>>> number3[1, 2, 3, 4, 5, [6, 7, 8, 9]]
Source
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]results = [ ]for i in nums :
if i > 5 :results.append(i)
print(results)
data = [ ]file = open(“C:\data.txt”, “r”)for i in file.readlines() :
data.append(i.strip())
print(data)
Shell [6, 7, 8, 9] [“one”, “two”, “three”]
Shell
>>> num3 = [1, 2, 3, 4, 5]
>>> num3.insert(2, 6)
>>> num3
[1, 2, 6, 3, 4, 5]
>>> num4 = [‘one’, ‘two’]
>>> num4.insert(1, ‘three’)
>>> num4
[‘one’, ‘three’, ‘two’]
Shell
>>> num1 = [1, 2, 3, 4, 5]
>>> num1.extend([6, 7, 8, 9])
>>> num1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Shell
>>> list3 = [1, 2, 3, 4, 5]
>>> list3.index(3)
2
>>> list3.index(5, 2)
4
>>> list3.index(3, 1, 4)
2
>>> list4 = [‘one’, ‘two’, ‘three’, ‘four’, ‘five’]
>>> list4.index(‘three’)
2
>>> list4.index(‘four’, 2)
3
>>> list4.index(‘three’, 1, 3)
2
Shell>>> num0 = [1, 2, 3, 4, 5, 3, 3]>>> num0.count(3)3
Shell
>>> list3 = [1, 2, 3, 4, 5]
>>> list3.pop()
5
>>> list3
[1, 2, 3, 4]
>>> list3.pop(2)
3
>>> list3
[1, 2, 4]
>>> list4 = ['A', 'B', 'C', 'D', 'E']
>>> list4.pop()
'E'
>>> list4
['A', 'B', 'C', 'D']
>>> list4.pop(1)
'B'
>>> list4
['A', 'C', 'D']
Shell
>>> num2 = [1, 2, 3, 4, 5, 3, 3]
>>> num2.remove(3)
>>> num2
[1, 2, 4, 5, 3, 3]
Shell
>>> num0 = [1, 9, 3, 8, 5]
>>> num0.sort()
[1, 3, 5, 8, 9]
>>> num0 = [1, 9, 3, 8, 5]
>>> num0.sort(reverse=True)
[9, 8, 5, 3, 1]
Source
def SelectionSort( ArList ) :
for i in range( len( ArList )) :
least = i
leastValue = ArList[i]
for j in range( i+1, len( ArList )) :
if (ArList[j]) < leastValue :
least = j
leastValue = ArList[j]
temp = ArList[i]
ArList[i] = ArList[least]
ArList[least] = temp
SList = [ 0, 1, 3, 5, 2, 6, 9, 7, 8 ]
SelectionSort(SList)
print(SList)
Shell [0, 1, 2, 3, 5, 6, 7, 8, 9]
Shell
>>> num1 = [1, 2, 3, 4, 5, 3, 3]>>> num1.reverse()>>> num1[3, 3, 5, 4, 3, 2, 1]
Shell
>>> num1 = [1, 2, 3]
>>> num2 = [4, 5, 6]
>>> num3 = num1 + num2
>>> num3
[1, 2, 3, 4, 5, 6]
>>> num4 = [1, 2, 3] * 2
>>> num4
[1, 2, 3, 1, 2, 3]
Shell
>>> num6 = [1, 2, 3, 4, 5, 6]
>>> len(num6)
6
>>> num7 = [‘A’, ‘B’, ‘C’]
>>> len(num7)
3
Shell
>>> num2 = [1, 2, 3, 4, 5, 6]
>>> num3 = [1, 2, 3, 4, 5, 6]
>>> num2 == num3
True
>>> num2 = [1, 2, 3, 4, 5, 6]
>>> num3 = [4, 5, 6]
>>> num2 < num3
True
Source
keywords = [“one”, “three”, “nine”]
s_key = keywords[0]
for i in range(1, len(keywords)) :
if len(keywords[i]) < len(s_key) :
s_key = keywords[i]
print(“가장 짧은 단어 : ”, s_key)
Shell
>>> num9 = [1, 2, 3, 4, 5, 6]
>>> max(num9)
6
가장 짧은 단어 : one
Shell
>>> str0 = “Who are you ?”
>>> str0.split()
[‘Who’, ‘are’, ‘you’, ‘?’]
>>> str3 = “There are you, I and her”
>>> str3.split(“,”)
[‘There are you’, ‘I and her’]
Shell
>>> nums = [1, 2, 3]
>>> numc = nums
>>> numc
[1, 2, 3]
>>> nums = [1, 2, 3]
>>> numc = list(nums)
>>> numc
[1, 2, 3]
>>> fromcopyimportdeepcopy
>>> nums = [1, 2, 3]
>>> numc = deepcopy(nums)
>>> numc
[1, 2, 3]
>>> nums = [1, 2, 3]
>>> numc = nums
>>> numc[2] = 10
>>> numc
[1, 2, 10]
>>> nums = [1, 2, 3]
>>> numc = list(nums)
>>> numc[2] = 10
>>> numc
[1, 2, 10]
>>> fromcopyimportdeepcopy
>>> nums = [1, 2, 3]
>>> numc = deepcopy(nums)
>>> numc[2] = 10
>>> numc
[1, 2, 10]
Source
grade = [
[ 90, 95, 98 ],
[ 85, 90, 80 ],
[ 60, 70, 80 ]
]
print(grade)
grade = [
[ 90, 95, 98 ],
[ 85, 90, 80 ],
[ 60, 70, 80 ]
]
rows = len(grade)
cols = len(grade[0])
for i in range(rows) :
for j in range(cols) :
print(grade[i][j], end=“,”)
print(grade)
Shell
[[90, 95, 98], [85, 90, 80], [60, 70, 8
0]]
90,95,98,
85,90,80,
60,70,80,
Source
grade = [ [ 90, 95, 98 ], [ 85, 90, 80 ], [ 60, 70, 80 ] ]
data = len(grade[0])
sum = 0
for i in range(data) :
sum = sum + grade[2][i]
print(sum)
Shell
210
Shell
>>> tuple2 = (1, 2, 3, 4, 5, 6)
>>> tuple2[3]
4
>>> tuple2[-4]
3
>>> tuple2 = (1, 2, 3, 4, 5, 6)
>>> tuple2[1:3]
(2, 3)
>>> tuple2[4:]
(5, 6)
Shell
>>> a, b = 10, 20
>>> print(a, b)
10 20
>>> a, b = b, a
>>> print(a, b)
20 10
>>> tuple3 = (‘A’, ‘B’, ‘C’)
>>> len(tuple3)
3
>>> ‘C’ in tuple3
True
>>> tuple3 + (‘D’, ‘E’, ‘F’)
(‘A’,‘B’,‘C’,‘D’,‘E’,‘F’
>>> tuple3 * 3
(‘A’,‘B’,‘C’,‘A’,‘B’,‘C’,‘A’,‘B’,‘C’)
>>> tuple4 = (‘파이썬’, ‘3’, ‘Windows’)
>>> (language, version, platform) = tuple4
>>> language
‘파이썬’
>>> version
‘3’
>>> platform
‘Windows’
Source
num3 = {2, 5, 3, 4, 1}
for i in num3 :
print(i, end=“ ”)
num3 = {2, 5, 3, 4, 1}
for i in sorted(num3) :
print(i, end=“ ”)
Shell
1 2 3 4 5 1 2 3 4 5
Shell
>>> set5 = {1, 2, 3, 4, 5}
>>> set5.add(6)
>>> set5
{1, 2, 3, 4, 5, 6}
>>> set6 = {1, 2, 3, 4, 5}
>>> set6.update([3, 4, 5, 6, 7, 8])
>>> set6
{1, 2, 3, 4, 5, 6, 7, 8}
>>> set7 = {1, 2, 3, 4, 5}
>>> set7.discard(4)
>>> set7
{1, 2, 3, 5}
>>> set8 = {1, 2, 3, 4, 5}
>>> set8.remove(4)
>>> set8
{1, 2, 3 5}
>>> set9 = {1, 2, 3, 4, 5}
>>> set9.clear()
>>> set9
set()
Shell
>>> set2 = {1, 2, 3, 4, 5}
>>> set3 = {1, 2, 3, 4, 5}
>>> set2 == set3
True
>>> set2 = {1, 2, 3}
>>> set3 = {3, 4, 5, 6, 7}
>>> set2 > set3
False
>>> set3 = {1, 2, 3, 4, 5}
>>> set4 = {4, 5}
>>> set4.issubset(set3)
True
>>> set3 = {1, 2, 3, 4, 5}
>>> set4 = {4, 5}
>>> set3.issuperset(set4)
True
>>> set9 = set(“IamPython”)
>>> ‘P’ in set9
True
Shell
>>> set2 = {1, 2, 3, 4, 5}>>> set3 = {6, 7, 8}>>> set2 | set3{1, 2, 3, 4, 5, 6, 7, 8}
>>> set2 = {1, 2, 3, 4, 5}>>> set3 = {6, 7, 8}>>> set2.union(set3){1, 2, 3, 4, 5, 6, 7, 8}>>> set3.union(set2){1, 2, 3, 4, 5, 6, 7, 8}
>>> set3 = {1, 2, 3, 4, 5}>>> set4 = {2, 3, 4, 5}>>> set3 & set4{2, 3, 4, 5}
>>> set3 = {1, 2, 3, 4, 5}>>> set4 = {2, 3, 4, 5}>>> set3.intersection(set4){2, 3, 4, 5}
>>> set3 = {1, 2, 3, 4, 5}>>> set4 = {2, 3, 4, 5}>>> set3 - set4{1}
>>> set3 = {1, 2, 3, 4, 5}>>> set4 = {2, 3, 4, 5}>>> set3.difference(set4){1}
Shell
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3[‘name’]
‘Kim’
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> del dic3[‘name’]
>>> dic3
{‘age’: 20}
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3[‘tel’] = ‘010-1234-5678’
>>> dic3
{‘tel’: ‘010-1234-5678’, ‘age’: ‘20’, ‘nam
e’: ‘Kim’}
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3.pop(“name”)
‘Kim’
>>> dic3
{‘age’: 20}
Source
dic3 = {‘name’: ‘Kim’, ‘age’: 20}
for i in dic3.items() :
print(i)
Shell
(‘name’, ‘Kim’)
(‘age’, 20)
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> ‘name’ in dic3
True
Source
dic3 = {‘name’: ‘Kim’, ‘age’: 20}
for i in dic3.keys() :
print(i)
Shell
age
name
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3.keys()
dict_keys([‘name’, ‘age’])
Shell
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3.values()
dict_values([20, ‘Kim’])
Shell
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3.items()
dict_items([(‘age’, 20), (‘name’, ‘Kim’)])
Shell
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> dic3.clear()
>>> dic3
{}
Shell
>>> dic3 = {‘name’: ‘Kim’, ‘age’: 20}
>>> ‘name’in dic3
True
\문자 설 명 \문자 설 명
\n 줄 바꿈(New Line) \₩ 문자 ‘₩’
\t 탭(Tab) \’ 단일 인용부호 (‘)
\0 NULL 문자 \” 이중 인용부호 ( ”)
Shell
>>> “010” “1234” “5678”
‘01012345678’
>>> “010”+“1234”+“5678”
‘01012345678’
>>> “010”* 3
‘010010010’
>>> “ABC” “DEF” “GHI”
‘ABCDEFGHI’
>>> “ABC”+“DEF”+“GHI”
‘ABCDEFGHI’
>>> “ABC”* 3
‘ABCABCABC’
Index string[0] string[1] string[2] string[3] string[4] string[5]
문자열(값) 1 2 3 a b python
음수 Index string[-6] string[-5] string[-4] string[-3] string[-2] string[-1]
Shell
>>> nums = “123456”
>>> nums[3]
‘4’
>>> nums[-4]
‘3’
>>> chars = “Nice to meet you!”
>>> chars[0:4]
‘Nice’
>>> chars[-4:-1]
‘you’
>>> nums = “123456”
>>> nums[3:]
‘456’
>>> nums[:3]
‘123’
>>> chars = “Nice to meet you!”
>>> chars[::5]
‘Nteu’
>>> chars[::-5]
‘!toi’
Shell
>>> print(“나는 %d살이고, 체중은 %.lf kg이며 사는 곳은 %s이다.”% (20, 70, ‘대한민국’))
나는 20살이고, 체중은 70 kg이며 사는 곳은 대한민국이다.
>>> height = 180.54
>>> print(“나의 키 : ”. format(height, ‘.1f’))
나의 키 : 180.5
>>> print(“우리지역 인구 수 : ”. format(123456789, ‘,d’))
우리지역 인구 수 : 123,456,789
>>> ‘{0} {1} {2} {0}’. format(24, 36, 48)
‘24 36 48 24’
>>> ‘{0} / {1} = {2:.3f}’. format(5, 3, 5/3)
‘5 / 3 = 1.667’
>>> ‘나는 {age}살이고 , 체중은 {weight}kg이며 사는 곳은 {country}이다 .’. format(age=20,
weight=70, country=‘대한민국’)
'나는 20살이고, 체중은 70kg이며 사는 곳은 대한민국이다.'
Shell
>>> chars = ‘abcDEF’
>>> chars.upper()
‘ABCDEF’
>>> chars = ‘abcDEF’
>>> chars.lower()
‘abcdef’
>>> chars = ‘Nice to meet you!’
>>> chars.find(‘you’)
13
>>> chars = ‘Nice-to-meet-you!’
>>> chars.replace(‘-’, ‘ ’)
‘Nice to meet you!’
>>> chars = ‘Nice to meet you! I’m your friend. My name is Python.’
>>> chars.count(‘you’)
2
Shell
>>> chars = ‘ Hi! ’
>>> chars.strip()
‘Hi!’
>>> chars = ‘1/2/3/4/5’
>>> chars.split(‘/’)
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
>>> chars = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
>>> sep = “-”
>>> sep.join(chars)
‘1-2-3-4-5’
>>> chars = ‘abcDEF’
>>> chars.isalpha()
True
>>> chars = ‘abcDEF’
>>> chars.isdigit()
False
>>> chars = ‘abcDEF’
>>> chars.startswith(“a”)
True
>>> chars = ‘abcDEF’
>>> chars.endswith(‘G’)
False