🐍 파이썬/파이썬 기본 문법
-
[에러/예외처리] Error handling🐍 파이썬/파이썬 기본 문법 2022. 8. 9. 12:15
Python Built-in Exceptions IndexError: list index out of range ValueError: invalid literal for (e.g. int("a")) NameError: name is not defined ZeroDivisionError: division by zero KeyError: a dictionary key is not found Raising Exceptions def do_something_with_number(n): if n < 0 : raise ValueError("negative number is given") Handling Exception : try / exception statement Code that can cause an ex..
-
[정수format] 귀찮지만 알면 좋은 다양한 정수 출력 방법🐍 파이썬/파이썬 기본 문법 2022. 8. 3. 05:37
1. 특정 칸 만큼 밀기 "{:d}".format(55) # 55 "{:10d}".format(55) # 55 2. 빈칸을 0으로 채우기 "{:05d}".format(55) # 00055 3. 부동소수점 다루기 "{:f}".format(12.345) # 12.345 "{:10f}".format(12.345) # 12.345 "{:+10f}".format(12.345) # +12.345 "{:+010f}".format(12.345) # +00012.345 "{:10.3f}".format(12.345) # 12.345 "{:10.2f}".format(12.345) # 12.34 "{:10.1f}".format(12.345) # 12.3 4. 의미 없는 0 지우기 "{:g}"
-
[입력/input] 인풋 함수 마스터하기🐍 파이썬/파이썬 기본 문법 2022. 8. 3. 05:26
파이썬의 input()함수는 사용자로 부터 값을 입력받아 결과로 내보내는 함수이다. 즉 입력값을 사용하고싶다면 반드시 변수에 저장해야한다. 그리고 이때 입력받은 결과는 무조건 문자열 타입으로 저장되므로, 숫자형 자료로 쓰고싶다면 형변환을 해야한다 ! number = input("enter the number> ") pirnt(type(number)) # number = int(number) print(number+1) # no type error
-
[string/문자열] 문자열 마스터 하기🐍 파이썬/파이썬 기본 문법 2022. 8. 3. 05:20
1. 문자열안에 따옴표 넣기 문자열은 큰따옴표 혹은 작은따옴표로 만들수있는데, 문자열 값자체에 따옴표를 넣고 싶을때는 따옴표 앞에 \, escape 문자열을 추가한다. "he said \"hello\" to her." "he thought \'i\'am hungry\'." 2. 여러줄 문자열 만들기 흔하게 뉴라인 캐릭터를 사용할 수 있지만, 가독성이 훨씬 좋은 방법이 있다. """ """ (''' ''')안에서 사용된 문자열내에서 사용한 엔터키는 자동으로 뉴라인으로 인식해준다. ''' 안녕 엔터키만 써도 알아서 줄바꿈이 된뎅 ! 짱좋다 '''
-
-
[OOP] The Four Core Principles of OOP🐍 파이썬/파이썬 기본 문법 2022. 7. 28. 12:10
•Abstraction •Define a concept, unrelated to a concrete instance. •Encapsulation •Hide implementation details (Fields cannot be overridden!). •Inheritance •Extend classes to reuse code (inherit behavior), is-arelationships. •Polymorphism •Replacing functionality in specializations. Dynamic selection of methods.
-
[inheritance] 상속 with 예제🐍 파이썬/파이썬 기본 문법 2022. 7. 28. 12:08
In OOP languages, classes can extend existing classes to inherit their behavior. At the same time, they can changeand extendthe behavior. *reminder : A class is an abstraction and represents a consistent combination of state(instance variables or “fields”) and operations to alter this state (“methods”). Lets start with the class represents "Animal" which has age and food state, and next_day, fee..