Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

히바리 쿄야 와 함께 하는 Developer Cafe

[2일차] DO IT 점프 투 파이썬/p85 ~ p149/ 자료형 본문

Python

[2일차] DO IT 점프 투 파이썬/p85 ~ p149/ 자료형

TWICE&GFRIEND 2021. 2. 18. 23:16

 

콘솔창 실습 로그 내용 

 

t1 = (1,2,'a','b')

t1[0]
Out[3]: 1

t1[3]
Out[4]: 'b'

t1 = (1,2,'a','b')

t1[1:]
Out[6]: (2, 'a', 'b')

t2 = (3,4)

t1 + t2
Out[8]: (1, 2, 'a', 'b', 3, 4)

t2 = (3,4)

t1 + t2
Out[10]: (1, 2, 'a', 'b', 3, 4)

t2 * 3
Out[11]: (3, 4, 3, 4, 3, 4)

t1 = (1,2,'a','b')

len(t1)
Out[13]: 4

a1 = (1,2,3)

a1 + (4)
Traceback (most recent call last):

  File "<ipython-input-15-7efb7c6bbe6a>", line 1, in <module>
    a1 + (4)

TypeError: can only concatenate tuple (not "int") to tuple


a1 + (4,)
Out[16]: (1, 2, 3, 4)

a = {1:'a'}

a[2] = 'b'

a
Out[19]: {1: 'a', 2: 'b'}

a['name'] = 'kim'

a
Out[21]: {1: 'a', 2: 'b', 'name': 'kim'}

a[3] = [1,2,3]

a
Out[23]: {1: 'a', 2: 'b', 'name': 'kim', 3: [1, 2, 3]}

del a[1]

a
Out[25]: {2: 'b', 'name': 'kim', 3: [1, 2, 3]}

a = {'a':1,'b':2}

a['a']
Out[27]: 1

a['b']
Out[28]: 2

a = {'name':'emily','phone':'01022330333','birth':'1224'}

a.keys()
Out[30]: dict_keys(['name', 'phone', 'birth'])

a.values()
Out[31]: dict_values(['emily', '01022330333', '1224'])

a.items()
Out[32]: dict_items([('name', 'emily'), ('phone', '01022330333'), ('birth', '1224')])

a.clear()

a
Out[34]: {}

a = {'name':'emily','phone':'01022330333','birth':'1224'}

a.get('name')
Out[36]: 'emily'

a.get('phone')
Out[37]: '01022330333'

a = {'name':'emily','phone':'01022330333','birth':'1224'}

print(a.get('nokey'))
None

print(a['nokey'])
Traceback (most recent call last):

  File "<ipython-input-40-54514d6fa670>", line 1, in <module>
    print(a['nokey'])

KeyError: 'nokey'


a.get('foo','bar')
Out[41]: 'bar'

a = {'name':'emily','phone':'01022330333','birth':'1224'}

'name' in a
Out[43]: True

'email' in a
Out[44]: False

s1 = set([1,2,3])

sq
Traceback (most recent call last):

  File "<ipython-input-46-9f22476d0cca>", line 1, in <module>
    sq

NameError: name 'sq' is not defined


s1
Out[47]: {1, 2, 3}

s2 = set("Hello")

s2
Out[49]: {'H', 'e', 'l', 'o'}

s1 = set([1,2,3])  -> 순서가없다, 중복 허용 불가 

l1 = list(s1)

l1
Out[52]: [1, 2, 3]

l1[0]
Out[53]: 1

t1 = tuple(s1)

t1
Out[55]: (1, 2, 3)

t1[0]
Out[56]: 1

 

money = 2000

card = True

if money >= 3000 or card:
    print("택시를 타고 가라")
else:
    print("걸어 가라")
    
택시를 타고 가라

1 in [1,2,3]
Out[4]: True

1 not in [1,2,3]
Out[6]: False

'a' in ('a','b','c')
Out[7]: True

'j' not in 'python'
Out[8]: True

pocket = ['paper','cellphone','money']

if 'money' in pocket:
    print("택시를 타고 가라")
else:
    print("걸어가라")
    
택시를 타고 가라

pocket = ['paper','cellphone','money']

if 'money' in pocket:
    pass
else:
    print("카드를 꺼내라")
    

pocket = ['paper','cellphone']

card = True

if 'money' in pocket:
    print("택시를 타고 가라")
else:
    if card:
        print("택시를 타고 가라")
    else:
        print("걸어가라")
        
택시를 타고 가라

pocket = ['paper','cellphone']

card = True

if 'money' in pocket:
    print("택시 타고 가")
elif card:
    print("택시 타고 가")
else:
    print("걸어가")
    
택시 타고 가

treehit = 0

while treehit < 10:
    treehit = treehit + 1
    print("나무를 %d번 찍었습니다." % treehit)
    if treehit == 10:
        print("나무 넘어갑니다")
        
나무를 1번 찍었습니다.
나무를 2번 찍었습니다.
나무를 3번 찍었습니다.
나무를 4번 찍었습니다.
나무를 5번 찍었습니다.
나무를 6번 찍었습니다.
나무를 7번 찍었습니다.
나무를 8번 찍었습니다.
나무를 9번 찍었습니다.
나무를 10번 찍었습니다.
나무 넘어갑니다

prompt = """
1.add
2.del
3.list
4.quit

Enter number: """

number = 0

while number != 4:
    print(prompt)
    number = int(input())
    

1.add
2.del
3.list
4.quit

Enter number: 

1

1.add
2.del
3.list
4.quit

Enter number: 

4


coffee = 10

money = 300

while money:
    print("돈 받았으니 커피 준다")
    coffee = coffee - 1
    print("남은 커피의 양은 %d개입니다." % coffee)
    if coffee == 0:
        print("커피가 다 떨어졌습니다.판매를 중지합니다")
        break
        
돈 받았으니 커피 준다
남은 커피의 양은 9개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 8개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 7개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 6개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 5개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 4개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 3개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 2개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 1개입니다.
돈 받았으니 커피 준다
남은 커피의 양은 0개입니다.
커피가 다 떨어졌습니다.판매를 중지합니다

#coffee.py
coffee = 10
while True:
    money = int(input("돈을 넣어 주세요: "))
    if money == 300:
        print("커피를 줍니다.")
        coffee = coffee - 1
    elif money > 300:
        print("거스름돈 %d를 주고 커피를 줍니다." % (money - 300))
        coffee - coffee - 1
    else:
        print("돈을 다시 돌려주고 커피를 주지 않습니다.")
        print("남은 커피의 양은 %d개 입니다." % coffee)
    if coffee == 0:
        print("커피가 다 떨어졌습니다. 판매를 중지합니다.")
        break
        
돈을 넣어 주세요: 500
거스름돈 200를 주고 커피를 줍니다.

돈을 넣어 주세요: 300
커피를 줍니다.

돈을 넣어 주세요: 100
돈을 다시 돌려주고 커피를 주지 않습니다.
남은 커피의 양은 8개 입니다.

돈을 넣어 주세요: 

a = 0

while a < 10:
    a = a + 1
    if a % 2 == 0: continue
    print(a)
    
1
3
5
7
9

test_list = ['one','two','three']

for i in test_list:
    print(i)
    
one
two
three

a = [(1,2),(3,4),(5,6)]

for (first, last) in a:
    print(first + last)
    
3
7
11


#marks2.py
marks = [90,25,67,45,80]
number = 0
for mark in marks:
    number = number + 1
    if mark < 60: continue
    print("%d번 학생 축하합니다.합격입니다." % number)
    

1번 학생 축하합니다.합격입니다.
3번 학생 축하합니다.합격입니다.
5번 학생 축하합니다.합격입니다.


a = range(10)

a
Out[42]: range(0, 10)

a = range(1,11)

a
Out[44]: range(1, 11)

add = 0

for i in range(1,11):
    add = add + i
    print(add)
    
1
3
6
10
15
21
28
36
45
55


marks = [90,25,67,45,80]
for number in range(len(marks)):
    if marks[number] < 60: continue
    print("%d번 학생 축하합니다.합격입니다." % (number + 1))
    
1번 학생 축하합니다.합격입니다.
3번 학생 축하합니다.합격입니다.
5번 학생 축하합니다.합격입니다.  



for i in range(2,10):
    for j in range(1,10):
        print(i*j, end=" ")
    print('')
    
2 4 6 8 10 12 14 16 18 
3 6 9 12 15 18 21 24 27 
4 8 12 16 20 24 28 32 36 
5 10 15 20 25 30 35 40 45 
6 12 18 24 30 36 42 48 54 
7 14 21 28 35 42 49 56 63 
8 16 24 32 40 48 56 64 72 
9 18 27 36 45 54 63 72 81 

a = [1,2,3,4]

result = []

for num in a:
    result.append(num*3)
    

print(result)
[3, 6, 9, 12]

a = [1,2,3,4]

result = [num * 3 for num in a]

print(result)
[3, 6, 9, 12]

a = [1,2,3,4]

result = [num * 3 for num in a if num % 2 == 0]

print(result)
[6, 12]


result = [x*y for x in range(2,10)
    for y in range(1,10)]

print(result)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, 12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30, 35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63, 8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81]
Comments