๐Python
Welcome to Python : ํด๋์ค
4:Bee
2023. 7. 21. 23:04
728x90
- ํด๋์ค : https://hi-code.tistory.com/123
- ๋ชจ๋ : https://hi-code.tistory.com/124
- ํจํค์ง : https://hi-code.tistory.com/125
- ์์ธ ์ฒ๋ฆฌ : https://hi-code.tistory.com/126
- ๋ด์ฅ ํจ์ : https://hi-code.tistory.com/127
- ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ : https://hi-code.tistory.com/128
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ :https://hi-code.tistory.com/129
| Title | Code | Console |
| ํด๋์ค๋ฅผ ์ฌ์ฉํ์ง ์์ ํจ์ |
result = 0 def add(num):
global result
result += num
return result
print(add(3))
print(add(4))
|
3 7 |
| ํด๋์ค์ ๊ธฐ๋ณธ ๊ตฌ์กฐ |
class Calculator: def __init__(self):
self.result = 0
def add(self, num):
self.result += num
return self.result
cal1 = Calculator()
cal2 = Calculator()
print(cal1.add(3))
print(cal1.add(4))
print(cal1.add(3))
print(cal1.add(7))
|
3 7 10 17 |
| ๊ฐ๋จํ ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ |
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)
print(a.add())
print(a.sub())
print(b.add())
print(b.mul())
|
6 2 11 24 |
| ํด๋์ค ์์ฑ์ |
class FourCal: def __init__(self, first, second):
self.first = first
self.second = second
...
a = FourCal(4, 2)
print(a.first)
print(a.second)
print(a.add())
print(a.div())
|
4 2 6 2.0 |
| ํด๋์ค ์์ |
class FourCal: def __init__(self, first, second):
self.first = first
self.second = second
... class MoreFourCal(FourCal):
pass
a = MoreFourCal(4, 2)
print(a.first)
print(a.second)
print(a.add())
print(a.div())
|
4 2 6 2.0 |
| ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ |
class FourCal: def __init__(self, first, second):
self.first = first
self.second = second
...
class SafeFourCal(FourCal):
def div(self):
if self.second == 0:
return 0
else:
return self.first / self.second
a = SafeFourCal(4, 0)
print(a.div())
|
0 |
| ํด๋์ค ๋ณ์ |
class Family: lastname = "๊น"
print(Family.lastname)
|
๊น |
|
class Family: lastname = "๊น"
a = Family()
b = Family()
print(a.lastname)
print(b.lastname)
|
๊น ๊น |
|
|
class Family: lastname = "๊น"
a = Family()
b = Family()
Family.lastname = "๋ฐ"
print(a.lastname)
print(b.lastname)
|
๋ฐ ๋ฐ |
728x90