점프 투 파이썬의 예제를 푼 뒤, 그것에 대해 정리한 포스팅
새 창을 통해 점프 투 파이썬 으로 이동
# 1.
a = "a:b:c:d"
b = a.replace(":", "#")
print(b)
# 1.result
a#b#c#d
# 1.
a = {"A":90, "B":80}
print(a.get("C", 70))
# 1.result
70
# 1.
A = [20, 55, 67, 82, 45, 33, 90, 87, 100, 25]
def sum(num):
total = 0
for i in num:
if i >= 50:
total += i
else:
pass
print(total)
sum(A)
# 1.result
481
# 2.
A = [20, 55, 67, 82, 45, 33, 90, 87, 100, 25]
result = 0
for num in A:
if num >= 50:
result += num
print(result)
# 2.result
481
# 1.
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n-2) + fib(n-1)
for i in range(10):
print(fib(i), end=" ")
# 1.result
0 1 1 2 3 5 8 13 21 34
# 1.
a = input("Enter the number : ")
nums = a.split(",")
sum = 0
for num in nums:
sum += int(num)
print(sum)
# 1.result
Enter the number : 3, 6, 7, 14
30
# 1.
a = input("Enter the number you want to create a multiplication table : ")
for num in range(1, 10):
if 2 <= int(a) < 10:
print(int(a)*num, end=" ")
else:
print("Please enter a number between 2 and 9.")
break
# 1.result
Enter the number you want to create a multiplication table : 7
7 14 21 28 35 42 49 56 63
# 1.
a = open('abc.txt', 'r')
content = a.readlines()
a.close()
content.reverse()
a = open('abc.txt', 'w')
for sentence in content:
sentence = sentence.strip()
a.write(sentence)
a.write("\n")
a.close()
# 1.result
(in 'abc.txt')
EEE
DDD
CCC
BBB
AAA
# 1.
a = open('sample.txt')
content = a.readlines()
a.close()
sum = 0
for score in content:
num = int(score)
sum += num
avg = sum / len(content)
a = open("result.txt", "w")
a.write(str(avg))
a.close()
# 1.result
(in 'result.txt')
79.0
# 1.
class Calculator(object):
def __init__(self, a):
self.a = a
def sum(self):
total = 0
for num in self.a:
total += int(num)
print(total)
def avg(self):
total = 0
for num in self.a:
total += int(num)
average = total / len(self.a)
print(average)
call = Calculator([1, 2, 3, 4, 5])
call.sum()
call.avg()
# 1.result
15
3.0
# 2.
class Calculator(object):
def __init__(self, *args):
self.args = args
def sum(self):
total = 0
for num in self.args:
total += int(num)
print(total)
def avg(self):
total = 0
for num in self.args:
total += int(num)
average = total / len(self.args)
print(average)
call = Calculator(1, 2, 3, 4, 5)
call.sum()
call.avg()
# 2.result
15
3.0
# 1.
a = "4546793"
nums = list(map(int, a))
result = []
for i, num in enumerate(nums):
result.append(str(num))
if i < len(nums)-1:
odd = num % 2 == 1
nex_odd = nums[i+1] % 2 == 1
if odd and nex_odd:
result.append("-")
elif not odd and not nex_odd:
result.append("*")
print("".join(result))
# 1.result
454*67-9-3