본문 바로가기
프로그래밍/Python

[python] #2 파이썬 기본 개념 - 예약어 (Reserved Words) & 식별자 (Identifiers)

by research_notes 2023. 1. 11.
728x90
반응형

1. 파이썬의 예약어(=키워드)란?

: 예약어(Reserved word = Keyword) 특정 기능을 수행하도록 '미리 예약되어 있는 단어'이다. 각각의 예약어가 특정 기능 또는 의미를 가지고 있기 때문에, 예약어는 클래스, 함수, 변수 등의 이름 등의 식별자(identifier)으로 사용할 수 없다. 

  • predefined meaning and syntax in the python language. 
  • You cannot use reserved words as variable names, function names, or any other identifiers. 

*다음 코드를 이용하여 예약어 리스트를 확인할 수 있음.

>>> import keyword

>>> print(keyword.kwlist)

>>> print(len(keyword.kwlist)  # 예약어 리스트 확인

*대표적인 예약어들로는,

  • and: logical AND operator
  • or: logical OR operator
  • not: logical NOT operator
  • break: used to break out of a loop
  • class: used to create a class
  • def: used to create a function
  • if: used to define a conditional statement
  • elif: used in conditional statements, same as else if
  • for: used to create a for loop
  • while: used to create a while loop
  • TRUE: Boolean value True
  • FALSE: Boolean value False

2. 식별자 (Identifier)
: 사용자가 임의로 정의하는 변수, 함수, 상수, 클래스, 또는 객체를 식별하는데 이름을 말한다. 이름으로 뜻을 알 수 있도록 지정하는 것이 좋음.

  • the name given to variables, classes, methods, etc

*식별자를 만드는 규칙

  • 영문자 (A-Z or a-z) 또는 언더바(_)로 시작할 수 있음. 숫자로 시작할 수 없음
  • 두번째 글자부터는 문자, 숫자, 밑줄 사용 가능
  • 대소문자 구별 (e.g., Language ≠ language)
  • 한글, 한자 등도 사용 가능
  • 특수문자 !, @, #, $, % 등은 사용할 수 없음
  • 띄어쓰기(blank)는 포함할 수 없음
  • 예약어를 식별자로 사용할 수 없음

 

728x90
반응형