본문 바로가기
Python/데이터 과학을 위한 파이썬 프로그래밍

[데이터 과학을 위한 파이썬 프로그래밍]연습문제 3장.<화면 입출력과 리스트>

by mintee
728x90
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 3장(화면 입출력과 리스트) 연습문제 #
 
 
# Lab: 화씨 온도 변환기.
 
# 화씨온도 = (섭씨온도 * 1.8) +32
 
print("본 프로그램은 섭씨온도를 화씨온도로 뱐환하는 프로그램입니다.")
print("변환하고 싶은 섭씨온도를 입력하세요.")
= float(input())
print("섭씨온도: ",c)
print("화씨온도: ", (c * 1.8+32  )
 
  
# 연습문제
 
#1
= [0,1,2,3,4]
print(a[:3], a[:-3]) # [0, 1, 2] [0, 1]
 
#2
print(a[::-1]) # [4, 3, 2, 1, 0]
 
 
#3
 
first = ["egg"'salad','bread','soup','canafe']
second=['fish','lamb','pork','beef','chicken']
third=['apple','banana','orange','grape','mango']
 
order = [first, second, third]
john = [order[0][:-2], second[1::3], third[0]]
del john[2]
john.extend([order[2][0:1]])
print(john) # [['egg', 'salad', 'bread'], ['lamb', 'chicken'], ['apple']]
 
 
 
#4 **
list_a = [3,2,1,4]
list_b = list_a.sort()
print(list_a, list_b) # [1, 2, 3, 4] None
 
#5 ** sort한거 왜 널값?
 
a=[5,7,3]
b=[3,9,1]
c=a+b
= c.sort()
print(c) # None
 
#-
b=a
a.sort()
print(b)
 
 
#6-5
 
#7
fruits = ['apple','banana','cherry','grape','orange','strawberry','melon']
print(fruits[-3:], fruits[1::3]) # ['orange', 'strawberry', 'melon'] ['banana', 'orange']
 
 
#8
num=[1,2,3,4]
print(num*2# [1, 2, 3, 4, 1, 2, 3, 4]
 
#9
a=[1,2,3,4]
b=['a','b','c','d','e']
a.append('g')
b.append(6)
print('g'in b, len(b)) #False 6
 
#10
 
list_a= ['Hankook','University','is','an','academic','institute','located','in','South Korea']
list_b=[]
for i in range(len(list_a)):
    if i % 2 != 1:
        list_b.append(list_a[i])
        
print(list_b) # ['Hankook', 'is', 'academic', 'located', 'South Korea']
 
 
#11
 
admission_year = input("입학 연도를 입력하세요: ")
print(type(admission_year))
 
 
#입학 연도를 입력하세요: 2018
#<class 'str'>
#입학 연도를 입력하세요: "2018"
#<class 'str'>
# 정답 -> 3번
 
#12
 
country = ['Korea','Japan','China']
capital = ['Seoul','Tokyo','Beijing']
index = [1,2,3]
country.append(capital) # *이중 리스트 형태로 추가됨!!* # ['Korea', 'Japan', 'China', ['Seoul', 'Tokyo', 'Beijing']]
country[3][1= index[1:] # 도쿄 위치에 삼중?리스트 형태로 추가됨
print(country) # ['Korea', 'Japan', 'China', ['Seoul, [2, 3], 'Beijing']]
 
#13
week1 = ['Mon''Tue''Wed']
week2 = ['Thu''Fri''Sat''Sun']
week3 = [a]
print(week2[:len(week3) + 1])
# ['Thu', 'Fri']
 
# 14
= 1
= 1 
is b # True
 
= 300
= 300
is b # False
 
# -> 파이썬의 정수형 저장방식 때문. 파이썬은 -5부터 256까지의 정수값을 특정 메모리주소에 저장해서
# 해당 숫자를 할당하려고 할 때 해당 변수는 그 숫자가 가진 메모리 주소로 연결됨. 따라서 주소와 값이 모두 같은 것으로 나온다.
## 정확하게 이해가진 않지만 -5에서 256까지의 숫자만 같은 숫자의 변수 입력시 메모리 주소가 같은 주소로 할당 된다는 말인 것 같다. 
 
# 15
= [5,4,3,2,1]
b=a
= [5,4,3,2,1]
is b # True
is c # False
# -> a와b는 같은 메모리 주소를 써서 트루가 나오지만, c는 같은 값이어도 같은 메모리주소를 쓰지 않으므로 false가 나온다.
 
cs
728x90

댓글