← Back to Blog

[Python] Variables and Data Types

computer science > programming

2026-07-044 min read

#computer-science #programming #python

파이썬 문법 (자료형, print)

  1. 변수 선언 및 자료형

변수 작명 방식

변수 이름 규칙:

예시를 통해 변수 이름 규칙을 보여드리겠습니다:

# 올바른 변수 이름
my_variable = 10
name = 'Alice'
num_students = 50
total_marks = 500

# 잘못된 변수 이름
2nd_place = 'Bob'  # 숫자로 시작함
totalMarks = 500   # 대문자와 소문자를 혼용함
for = 'Loop'       # 예약어를 변수 이름으로 사용함

이러한 변수 이름 규칙을 따르면 코드의 가독성을 높일 수 있고, 코드를 이해하고 유지보수하는 데 도움이 됩니다.

자료형:

x = 5
y = -10
z = 1000
pi = 3.14
euler = 2.71828
negative_float = -0.5
greeting = '안녕하세요'
language = "파이썬"
number_as_string = '123'
is_valid = True
is_invalid = False
fruits = ['사과', '바나나', '오렌지']
numbers = [1, 2, 3]
mixed_list = [1, 'two', 3.0]
point = (10, 20)
rgb_values = (255, 128, 64)
person = {'이름': 'Alice', '나이': 30}
fruit_prices = {'사과': 1.5, '바나나': 2.0, '오렌지': 1.0}
prime_numbers = {2, 3, 5, 7, 11}
unique_characters = {'a', 'b', 'c', 'd'}

각 코드 블록은 해당 자료형의 설명과 함께 예시를 포함하고 있습니다. 이러한 설명과 예시를 통해 각 자료형을 이해할 수 있을 것입니다.


  1. 출력

파이썬으로 결과물을 출력하거나 테스트를 해볼 때, 값을 출력하여 아는 방법이 있습니다.

그때 사용하는 것이 print라는 함수입니다.

한 문장 출력

print("Hello World")
# Hello World
print('Hello World')
# Hello World

C/C++에서는 ‘(작은 따옴표)는 문자를 “(큰 따옴표)는 문자열을 의미하지만,

파이썬에서는 “(큰 따옴표), ‘(작은 따옴표)의 구분이 없습니다.

문자는 문자 ‘하나’를 의미하고, 문자열은 문자의 집합이라고 이해하면 될 것 같습니다.

공백을 두고 출력하기

print(1, 2)
# 1 2
print(1, 2, sep=" ") # 공백을 두고 print
# 1 2
print(1, 2, sep=",") # ,을 두고 print
# 1,2
print(1, 2, sep="/") # /을 두고 print
# 1/2

print()는 end 값에 기본적으로 ‘\n’(Enter key)가 들어가 있기 때문에 알아서 다음 줄로 넘어갑니다. 하지만 end 값을 변경하면 다른 출력을 볼 수 있습니다.

print(1, end=' ')
print(2)
# 1 2

특수 문자 출력

다음과 같이 코드를 작성하게 되면 hi\\\ 출력을 예상하셨겠지만, 에러가 발생합니다.

그 이유는 문자열 안에서 명령어로 사용되는 특수 문자들 때문에 발생합니다.

간단히 말하여 문자로 쓰려고 했지만, 명령어로 인식하여 에러가 발생하는 것입니다.

print("hi\\\"
# error
문자설명
\n줄바꿈(enter)
\t탭 (tab)
\백슬래시() 자체
’‘(작은 따옴표)
”“(큰 따옴표)

이를 해결할 수 있는 방법은 크게 두 가지가 있습니다.

print("hi\\\\\\)
# hi\\\
print("""Hello World
Welcome to Inha Univ""")
# Hello World
# Welcome to Inha Univ

위 처럼 “”” 또는 ‘’’를 사용하게 되면, 사용자가 보는 그대로 출력을 하게 됩니다.

저 예제를 보면, \n(Enter key)가 World 뒤에 붙어있지만 그 또한 포함하여 출력을 한 것입니다.

변수를 포함한 출력

a = 1
b, c = 2, 3
d = a + b # d -> 3; 1 + 2
print('a =', a)
# a = 1
print('d =', d)
# d = 3
toy = "lego"
pi = 3.1415926535
print(type(toy))
#
print(type(pi))
#
코드설명
%s문자열(String)
%c문자(Character)
%d정수(Integer)
%f실수(Floating-Point)
a = 10
print("a = %d" % a)
# 10

b = "Hello"
print("b = %s" % b)
# Hello

print("a = %d \n b = %s" % (a, b))
# a = 10
# b = Hello
x, y = 10, "code"

print("x is {0}" .format(x))
print("x is {new_x}" .format(new_x=x))

print("\n")

print("x is {0} and y is {1}" .format(x, y))
print("x is {new_x} and y is {new_y}" .format(new_x=x, new_y=y))
print("y is {1} and x is {0}" .format(x, y))
print("y is {new_y} and x is {new_x}" .format(new_x=x, new_y=y))

#print("x is {x}" .format(x))  # error


x is 10
x is 10

x is 10 and y is code
x is 10 and y is code
y is code and x is 10
y is code and x is 10
x, y = 10, "code"

print(f"x is {x}")
print(f"y is {y}")
print(f"x is {x} and y is {y}")


x is 10
y is code
x is 10 and y is code

실수형 변수 소수점 수정 후 출력

x = 3.141592653
print("%.4f" % x)
# 3.1416
x = 3.141592653
print("{0:.4f}" .format(x))
# 3.1416
x = 3.141592653
print(f"{x:.4f}")
# 3.1416