Python 기초: 변수와 데이터 타입
by NyangPolice
Python 기초: 변수와 데이터 타입
Python은 동적 타입 언어로, 변수를 선언할 때 타입을 명시하지 않아도 됩니다.
변수 선언
# 변수 선언은 간단합니다
name = "Python"
age = 30
height = 175.5
is_student = True
기본 데이터 타입
1. 숫자 타입
# 정수 (int)
count = 10
# 실수 (float)
price = 99.99
# 복소수 (complex)
complex_num = 3 + 4j
2. 문자열 (str)
# 문자열 선언
greeting = "안녕하세요"
message = 'Python은 쉽습니다'
# 여러 줄 문자열
multiline = """
첫 번째 줄
두 번째 줄
세 번째 줄
"""
3. 불리언 (bool)
is_true = True
is_false = False
4. 리스트 (list)
# 리스트 선언
fruits = ["사과", "바나나", "오렌지"]
numbers = [1, 2, 3, 4, 5]
# 리스트 접근
print(fruits[0]) # "사과"
5. 딕셔너리 (dict)
# 딕셔너리 선언
person = {
"name": "홍길동",
"age": 25,
"city": "서울"
}
# 딕셔너리 접근
print(person["name"]) # "홍길동"
타입 확인
# type() 함수로 타입 확인
print(type(10)) # <class 'int'>
print(type("hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>
타입 변환
# 타입 변환
num_str = "123"
num_int = int(num_str) # 문자열을 정수로
num_float = float(num_str) # 문자열을 실수로
str_num = str(123) # 숫자를 문자열로
Python의 변수와 데이터 타입은 매우 직관적이고 사용하기 쉽습니다!
tags: python - 기초 - 변수 - 데이터타입Python 카테고리의 글 목록
-
Python 기초: 제어문 (조건문과 반복문)
-
Python 기초: 변수와 데이터 타입