본문 바로가기

Python16

python class2 import Calculator; cal1 = Calculator.Calculator(); cal2 = Calculator.Calculator(); print(cal1.Add(1, 2)); print(cal1.Sub(1, 2)); print(cal2.Add(2, 2)); print(cal2.Sub(2, 2)); class Calculator: def __init__(self): self.result = 0; def Add(self, number1, number2): return number1 + number2; def Sub(self, number1, number2): return number1 - number2; 3 -1 4 0 2019. 9. 6.
python class 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(cal2.Add(3)); print(cal2.Add(7)); 3 7 3 10 대부분 객체지향 언어는 Self를 생략하지만 파이썬에서는 self를 꼭 명시해야 한다. 그리고 self는 자동으로 전달된다. 2019. 9. 4.
python file 입출력 file = open("C:\\python\\newFile.txt", "w"); file.close(); filePath = "C:\\python\\newFile.txt"; file = open(filePath, "w"); for i in range(1, 11): line = "%d Line. \n" %i; file.write(line); file.close(); 1 Line. 2 Line. 3 Line. 4 Line. 5 Line. 6 Line. 7 Line. 8 Line. 9 Line. 10 Line. 쓰기 모드로 열 때, 이미 존재하는 파일을 열면 파일 내용이 모두 사라진다. filePath = "C:\\python\\newFile.txt"; file = open(filePath, "r"); lin.. 2019. 9. 3.
python input, print number = input("insert coin :"); print(number); insert coin :10 10 print("life" "is" "too short"); lifeistoo short print("life" + "is" + "too short"); lifeistoo short print("life", "is", "too short"); life is too short 2019. 9. 2.