Expression (표현식)
프로그래밍 또는 컴퓨터 과학 분야에서 Expression은 흔히, function call
, identifier
, number
, operator
, literal
등으로 이루어진다.
- 표현식(or 수식) 으로 번역.
- 하나의 value로 reduce 될 수 있는 code를 말함. (python에서는 여러 value들을 가진 collection을 반환하는 expression도 가능하나, 이것도 하나의 collection으로 reduce된 것으로 볼 수 있음.)
evaluation
이라는 용어와 함께 쓰이는데, expression
을 결과 value로 바꾸어주는 동작을 의미함.
2+3
이라는 expression 을5
로 구해주는 처리가 바로evaluation
임.- 이후 다루는
eval
이라는 built-in function이 바로evaluation
을 추상화 및 구현한 함수.
다양한 Expression들
a,b,c = 1,2,3
l = ['a','b','c']
def my_func(x):
return x+2 # return value가 있는 경우, function call = expression
#---------------------------
# 아래의 각 line들은 모두 expression임.
my_func(3)
a # Expressions don’t have to involve an operator
1 # A number by itself is an expression
a*b*c
1*2*3
l[1]
a+my_function(b)
eval
function of Python
- expression을 나타내는
string
을 argument로 받아, 이 결괏값을 반환하는 함수임. evaluation
을 수행하기 때문에 앞의 4글자를 따서eval
이라고 명명됨.
a, b = 1,2
eval('a+b')
eval('1+2')
Statement (문장)
- 문장 이라고 번역되는 것.
- executable (source) code 를 가르키며 일반적으로 single line code나 code block을 가르킴. (multi-line statement를 compound statement라고 부름)
- 일반적으로 하나 이상의
expression
과keyword
로 구성됨.expression
을 포함하고 있음.- 즉, 모든
expression
은statement
임. - 하지만, 모든
statement
는expression
이 아님.
- 즉, 모든
- 예를 들어 assignment
a=1
의 경우, 하나의 값으로 reduce되지는 않으므로expression
은 아니지만statement에
포함됨.
exec
function of python
statement
에 해당하는 string을 argument로 받아 실행하는 built-in function임.
ds_len = 5
for i in range(ds_len):
idx = '{:02}'.format(i)
exec('mmmil'+idx+'='+str(i+1))
- 위의 코드는 mmmil00 의 형태의 변수를
de_len
개 를 만들고 각각에1~de_len
까지의 값을 할당함. - exec 의 경우 항상 None을 반환함.
Reference
https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile
What's the difference between eval, exec, and compile?
I've been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement. Can someone please explain the difference between eval and exec,...
stackoverflow.com
https://docs.python.org/ko/3/reference/compound_stmts.html
8. Compound statements
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although i...
docs.python.org
2023.06.11 - [Programming] - [Python] Keyword란? (Soft Keyword 포함)
[Python] Keyword란? (Soft Keyword 포함)
Keywords (or Reserved Words)Keyword란 Python에서 특별한 단어 (special word)들을 가르킨다.Keyword들은 Python에서 특정한 목적으로 사용되도록 이미 정해진 word들로Python에서 정해놓은 방법 외로는 사용을 할 수
dsaint31.tistory.com
https://gist.github.com/dsaint31x/31f032e2a8f7aedabf924f9773abae5f
py_expression_vs_statements.ipynb
py_expression_vs_statements.ipynb. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
2023.02.20 - [Programming] - [Basic] Literal
[Basic] Literal
Literal소스 코드 상에서 고정된 값을 가르킴. (또는 고정된 값을 나타내는 표기법을 의미함.)Programming language에서 data의 값을 지정(specifying data values)하는 방법은 다음 중의 하나임.1. Literal을 사용.2
dsaint31.tistory.com
2023.06.10 - [Programming] - [Python] Function Call (함수호출)
[Python] Function Call (함수호출)
Function call은 함수를 호출하여, 해당 함수를 실행하는 것을 의미한다. Python에서는 다양한 built-in function을 제공하고 있으며, third-party에서 제공하는 library의 함수들까지 고려한다면, application을 만
dsaint31.tistory.com
'Programming' 카테고리의 다른 글
[Python] Arithmetic in Python and Augmented Assignment (0) | 2023.06.12 |
---|---|
[Python] (Data) Type: Summary (0) | 2023.06.12 |
[WSL] Install WSL (Windows Subsystem for Linux) (0) | 2023.06.12 |
[Python] Python 소개? (0) | 2023.06.12 |
[Python] Dynamic Typed Language vs. Static Typed Language (0) | 2023.06.11 |