Variables · 변수
A box with a name
- A variable is a named box that holds a value.
- You put a value in the box, then use the name later.
- The value can be a number or a string.
이름이 있는 상자
- 변수는 값을 저장하는 이름이 붙은 상자입니다.
- 상자에 값을 넣고 나중에 그 이름을 사용합니다.
- 값은 숫자나 문자열일 수 있습니다.
Make a variable
- Write
name = valueto store a value. - The name is on the left. The value is on the right.
=means "store this value", not "is equal to".
변수 만들기
- 값을 저장하려면
name = value을 쓰세요. - 이름은 왼쪽에, 값은 오른쪽에 옵니다.
=은 "이 값이 등이다"가 아니라 "이 값을 저장하라"는 뜻입니다.
city = "Paris"
print(city)
Naming rules
- Use letters, digits, and the underscore
_. - A name cannot start with a digit and cannot have spaces.
- Names are case-sensitive:
scoreandScoreare different. - Pick clear names like
ageortotal_score.
변수 이름 규칙
- 알파벳, 숫자, 언더스코어
_을 사용하세요. - 이름은 숫자로 시작할 수 없으며 공백을 포함할 수 없습니다.
- 이름은 대소문자를 구분합니다:
score과Score은 다릅니다. age이나total_score처럼 명확한 이름을 선택하세요.
Change a variable
- You can store a new value at any time.
- The new value replaces the old one.
- The name still points to one value — the latest one.
변수 변경하기
- 언제든지 새로운 값을 저장할 수 있습니다.
- 새 값은 기존 값을 교체합니다.
- 이름은 여전히 하나의 값, 즉 최신 값을 가리킵니다.
A variable is a labelled box · 변수는 레이블이 붙은 상자입니다
Step through the program. Each line stores a value in a named box; reassigning count overwrites its box. One name, one current value. · 프로그램을 한 줄씩 추적해 보십시오. 각 줄은 이름이 붙은 상자(value)에 값을 저장합니다; 재할당(reassigning) count은 해당 상자를 덮어씁니다. 하나의 이름, 하나의 현재 값만 존재합니다.
count = 1
count = 2
print(count)
Constants
- A constant is a value that should not change.
- Python has no special keyword for it.
- By convention, we write constant names in
UPPERCASE.
상수
- 상수는 변해서는 안 되는 값입니다.
- Python에는 이를 위한 특수 키워드가 없습니다.
- 관례로 상수 이름은 모두 대문자로
UPPERCASE씁니다.
PI = 3.14159
print(PI)
Common mistakes
- Use
=to store a value: the name goes on the left, the value on the right. - Use a variable only after you have given it a value, or you get a NameError.
- Names cannot start with a number or contain spaces.
흔한 실수
- 값을 저장할 때는
=을 사용합니다: 이름은 왼쪽, 값은 오른쪽에 배치합니다. - 값을 할당하기 전에 변수를 사용하면 NameError가 발생합니다.
- 이름은 숫자로 시작할 수 없으며 공백을 포함할 수 없습니다.
Now you try
- Use a variable for each task below.
- Press Check answer to test your code.
이제 직접 해보기
- 아래 작업마다 변수를 하나씩 사용하세요.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Create a variable called age and store the number 15 in it. · age이라는 변수를 생성하고, 숫자 15을 여기에 저장하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The variable color starts as "red". Reassign it so that it becomes "blue". · 변수 color는 초기에 "red"입니다. 재할당하여 "blue"가 되도록 만드십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Constants are written in UPPERCASE by convention. Create a constant MAX_SCORE and set it to 100. · 관례상 상수(Uppercase)는 대문자로 표기합니다. 상수 MAX_SCORE를 생성하고 이를 100로 설정하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
a is 1 and b is 2. Swap them, so a becomes 2 and b becomes 1, without typing the numbers 1 or 2 again. (Tip: a third variable can hold one value for a moment.) · a는 1이고, b는 2입니다. 서로 바꾸어, a를 2로, b를 1로 만들되, 숫자 1과 2를 다시 입력하지 마십시오. (팁: 세 번째 변수를 사용하여 한 값만 잠시 보관할 수 있습니다.)
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.