코드
def print_reverse(string):
print(string[::-1])
실행 결과
print_reverse("python")
코드를 실행시키면
nohtyp 역순으로 출력
코드
my_dict = {"10/26" : [100, 130, 100, 100],
"10/27" : [10, 12, 10, 11]}
정답
def print_value_by_key (my_dict, key):
print(my_dict[key])
실행 결과
print_value_by_key (my_dict, "10/26")
이 코드 실행시키면
[100, 130, 100, 100] 이렇게 출력됨
코드
def calc_monthly_salary(annual_salary):
return print(int(annual_salary/12))
calc_monthly_salary(12000000)
실행 결과
1000000
코드
def pickup_even(numbers):
result = []
for number in numbers:
if number % 2 == 0:
result.append(number)
return result
실행 결과
pickup_even([3,4,5,6,7,8])
이 코드를 실행하면
[4, 6, 8] 이렇게 출력됨
코드
def convert_int(string):
string = string.replace(",", "")
number = int(string)
return number
다른 버전
def convert_int(string):
return int(string.replace(',', ''))
실행 결과
convert_int("1,234,567")
위 코드 실행하면
1234567 이 출력됨
코드
class FourCal:
def setdata(self, first, second):
self.first = first
self.second = second
코드
class FourCal:
def setdata(self, first, second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
a = FourCal()
a.setdata(4, 2)
a.add()
실행 결과
6
코드
class FourCal:
def setdata(self, first, second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def mul(self):
result = self.first * self.second
return result
def sub(self):
result = self.first - self.second
return result
def div(self):
result = self.first / self.second
return result
a = FourCal()
b = FourCal()
a.setdata(4, 2)
b.setdata(3, 8)
실행 결과
>>> a = FourCal()
>>> b = FourCal()
>>> a.setdata(4, 2)
>>> b.setdata(3, 8)
>>> a.add()
6
>>> a.mul()
8
>>> a.sub()
2
>>> a.div()
2
>>> b.add()
11
>>> b.mul()
24
>>> b.sub()
-5
>>> b.div()
0.375
코드
class Human:
def __init__(self):
print("응애응애")
실행 결과
areum = Human()
이 코드 실행시
'응애응애' 가 출력됨