Self-study/Python 11

[python] mutability

1. immutable : unchanging over time or unable to be changed String are immutable: Strings are cannot change once created. Instead, an assignment statement must be used. x = 10 print(id(x)) #1384163392 def add5_to_x(a): print(id(a)) # 1384163392 a = a + 5 return a x = add5_to_x(x) print(id(x)) #1384163472 x = add5_to_x(x) -> reassign 한 것이기 때문에 memory주소가 달라짐. (값 자체가 달라졌기 때문에 다른 곳을 reference) pytho..

Self-study/Python 2024.01.15

[Python] 내장함수와 외장함수

1. 내장함수(Bult-in Function) 1) input : 사용자로부터 입력을 받는 함수 language = input("무슨 언어를 좋아하세요?") 2) dir : 어떤객체를 넘겨줬을 때, 그 객체가 어떤 변수와 함수를 갖고 있는지 표시 print(dir()) import random print(dir()) import pickle print(dir()) 2. 외장함수 1) glob: 어떤 경로 내의 폴터/파일 목록 조회 import glob print(glob.glob("*.py")) #확장자가 py인 모든 파일 결과값: 2) os: 운영체제에서 제공하는 기본기능 import os print(os.getcwd()) #현재 디렉토리 표시 결과값: 3) time import time print(t..

Self-study/Python 2023.12.29

[Python] 모듈과 패키지

1. 모듈 한 번 구현한 파이썬 코드를 다른 파이썬 파일의 코드에서 공유해서 사용할 수 있도록 하기 위해 모듈module을 활용한다. 파이썬 모듈은 간단하게 말하면 하나의 파이썬 소스코드 파일이며, 확장자로 .py 가 사용된다. 모듈에는 보통 서로 연관된 함수와 클래스 등을 저장한다. (※참고) #theater_module.py def price(ppl): print("{0}명 가격은 {1}원 입니다.".format(ppl, ppl * 10000)) def price_morning(ppl): print("{0}명 가격은 {1}원 입니다.".format(ppl, ppl * 6000)) def price_soldier(ppl): print("{0}명 가격은 {1}원 입니다.".format(ppl, ppl *..

Self-study/Python 2023.12.29

[Python] 예외처리

1. 원래 있는 에러 해결할 때 ValueError, ZeroDivisionError print(err): 에러메시지 그대로 출력. try: print("divider calculator") num1 = int(input("enter the first number:")) num2 = int(input("enter the second number:")) print("{0} / {1} = {2}".format(num1, num2, int(num1/num2))) except ValueError: print("Error! Wrong number") except ZeroDivisionError as err: print(err) #어떤 에러가 발새했을 때, 그 메시지를 그대로 출력 try: print("divide..

Self-study/Python 2023.12.29

[Python] Class

1. 클래스 선언 (Declaration) class : def __init__(): 생성자 __init__(): 생성자. self제외 모든 파라미터를 self.로 재정의해줘야 함. 멤버변수: self.name (클래스 내에 정의된 변수) class Unit: def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed print("{0} 유닛이 생성되었습니다".format(name)) def move(self, location): print("[지상유닛 이동]") print("{0} : {1} 방향으로 이동합니다. [속도 : {2}]" \ .format(self.name, location, self.speed)..

Self-study/Python 2023.12.29

[Python] 표준입출력

1. 표준입출력 1) separator print("python", "java", sep = " , ", end ="?") #end: 문장의 끝부분을 ?로 바꿔달라. end는 줄바꿈이 디폴트인데 ?로 바뀜 print("what is more useful?") #출력: python, java 2) import sys print("Python", "java", file = sys.stdout) #표준출력 print("Python", "java", file = sys.stderr) #표준에러 - 따로 로딩해서 에러처리할 때 필요 3) set 출력 ljust( ): 왼쪽 정렬 rjust( ): 오른쪽 정렬 sep: separator scores = {"math" : 0, "english" : 50, "codin..

Self-study/Python 2023.12.27

[Python] List, Tuple, dictionary, Set

1. List [ a, b, c ...... ] 순서 ⭕️ , 중복 ⭕️ , 삭제 ⭕️ , 수정 ⭕️ #list 선언 subway = [10, 20, 30] subway = ["min", "moon", "young"] #다양한 자료형 함께 사용가능! mix_list=["cho", 11, True] #해당 value의 index 반환 print(subway.index("moon")) #list에 추가 (맨 뒤로 추가됨) subway.append("haha") print(subway) #추가될 위치 지정, 그 위치에 추가되고 뒤로 밀림 subway.insert(1, "hyung") print(subway) #remove : pop() 맨뒤부터 빠짐 print(subway.pop()) print(subway)..

Self-study/Python 2023.12.26