Python 키워드와 식별자(업데이트됨)

자, 파이썬 키워드와 식별자에 대해 이야기해 봅시다. 최근에는 이 파이썬 튜토리얼에서 초보자를 위한 파이썬 설치와 설정에 대한 완전한 튜토리얼도 다루었습니다.

파이썬 키워드

간단하게 말해서, 파이썬 키워드는 예약된 단어입니다. 즉, 변수, 클래스 및 함수와 같은 개체의 이름으로 사용할 수 없습니다.

그래서 이 키워드들이 무엇인지 궁금할 것입니다. 이들은 파이썬 언어의 구문과 구조를 정의하기 위한 것입니다.

이 튜토리얼을 작성할 당시에는 파이썬 프로그래밍 언어에 33개의 키워드가 있음을 알아야 합니다. 그 수는 시간이 지남에 따라 변할 수 있습니다. 또한 파이썬의 키워드는 대소문자를 구분합니다. 그래서 그대로 작성되어야 합니다. 여기 파이썬 프로그래밍의 모든 키워드 목록이 있습니다.

모든 키워드를 보고 한 번에 모두 이해하려고 하면 압도될 것입니다. 그래서 지금은 그냥 이것들이 키워드라는 것을 알아두세요. 각각의 사용법을 배워갈 것입니다. 파이썬 셸 도움말을 통해 파이썬 키워드 목록을 얻을 수 있습니다.

모든 파이썬 키워드 목록

and Logical operator
as Alias
assert For debugging
break Break out of Python loops
class Used for defining Classes in Python
continue Keyword used to continue with the Python loop by skipping the existing
def Keyword used for defining a function
del Used for deleting objects in Python
elif Part of the if-elif-else conditional statement in Python
else Same as above
except A Python keyword used to catch exceptions
FALSE Boolean value
finally This keyword is used to run a code snippet when no exceptions occur
for Define a Python for loop
from Used when you need to import only a specific section of a module
global Specify a variable scope as global
if Used for defining an “if” condition
import Python keyword used to import modules
in Checks if specified values are present in an iterable object
is This keyword is used to test for equality.
lambda Create anonymous functions
None The None keyword represents a Null value in PYthon
nonlocal Declare a variable with non-local scope
not Logical operator to negate a condition
or A logical operator used when either one of the conditions needs to be true
pass This Python keyword passes and lets the function continue further
raise Raises an exception when called with the specified value
return Exits a running function and returns the value specified
TRUE Boolean value
try Part of the try…except statement
while Used for defining a Python while loop
with Creates a block to make exception handling and file operations easy
yield Ends a function and returns a generator object

아래는 파이썬 프로그램에서 if-else 사용 예제를 보여주는 간단한 예입니다.

var = 1;

if(var==1):
    print("odd")
else:
    print("even")

위의 프로그램을 실행하면 파이썬은 고정된 키워드와 구문으로 if-else 블록을 이해하고 추가 처리를 수행합니다.

파이썬 식별자란 무엇인가요?

파이썬 식별자는 변수, 함수, 클래스, 모듈 또는 다른 객체를 식별하는 데 사용하는 이름입니다. 즉, 엔터티에 이름을 부여하려고 할 때 식별자라고 합니다.

가끔 변수와 식별자가 종종 동일하게 오해되지만, 그렇지 않습니다. 명확성을 위해 변수가 무엇인지 살펴보겠습니다.

파이썬에서 변수란 무엇인가요?

A variable, as the name indicates is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s identifier, the nickname.

식별자 작성 규칙

식별자 작성을 위한 몇 가지 규칙이 있습니다. 그러나 먼저 파이썬이 대소문자를 구분한다는 것을 알아야 합니다. 즉, Namename은 파이썬에서 두 개의 다른 식별자입니다. 파이썬에서 식별자를 작성하는 데 적용되는 몇 가지 규칙이 있습니다.

  1. 식별자는 대문자와 소문자의 조합, 숫자 또는 밑줄(_)일 수 있습니다. 따라서 myVariable, variable_1, variable_for_print은 모두 유효한 파이썬 식별자입니다.
  2. 식별자는 숫자로 시작할 수 없습니다. 그래서 variable1은 유효하지만, 1variable은 유효하지 않습니다.
  3. !,#,@,%,$ 등 특수 기호는 식별자에 사용할 수 없습니다.
  4. 식별자의 길이는 어떤 길이든 될 수 있습니다.

이러한 것들은 식별자를 작성하기 위한 엄격한 규칙이지만, 따르는 것이 좋은 관행인 몇 가지 명명 규칙도 있습니다.

  1. 클래스 이름은 대문자로 시작합니다. 다른 모든 식별자는 소문자로 시작합니다.
  2. 단일 선행 밑줄로 식별자를 시작하면 해당 식별자가 비공개임을 나타냅니다.
  3. 식별자가 양쪽에 언더스코어로 시작하고 끝난다면, 해당 식별자는 언어에서 정의된 특수 이름입니다.
  4. c = 10은 유효하지만, count = 10을 작성하는 것이 더 의미가 있으며, 코드를 오랜 시간 후에 보더라도 그 역할을 쉽게 알 수 있습니다.
  5. 여러 단어는 밑줄을 사용하여 구분할 수 있습니다. 예를 들어 this_is_a_variable입니다.

다음은 파이썬 변수에 대한 샘플 프로그램입니다.

myVariable="hello world"
print(myVariable)

var1=1
print(var1)

var2=2
print(var2)

프로그램을 실행하면 출력은 아래 이미지와 같을 것입니다.

결론

그러니까, 오늘은 여기까지입니다. 다음 강의에서는 Python 문장과 주석에 대해 배우겠습니다. 그때까지 #happy_coding 🙂

Source:
https://www.digitalocean.com/community/tutorials/python-keywords-identifiers