python
python?
Dev.Congsik
2024. 9. 6. 14:04
728x90
파이썬(Python)은 고급 프로그래밍 언어로, 간결하고 읽기 쉬운 문법을 가지고 있어 초보자부터 전문가까지 널리 사용됩니다. 파이썬은 다양한 용도로 사용될 수 있으며, 웹 개발, 데이터 분석, 인공지능, 자동화 등 여러 분야에서 활용됩니다.
- 파이썬의 주요 특징
- 간결하고 명확한 문법: 코드가 직관적이고 읽기 쉬워 유지보수가 용이합니다.
- 동적 타이핑: 변수의 타입을 명시적으로 선언할 필요가 없습니다.
- 풍부한 표준 라이브러리: 다양한 기능을 제공하는 라이브러리가 내장되어 있습니다.
- 크로스 플랫폼: 윈도우, 리눅스, 맥OS 등 다양한 운영체제에서 실행 가능합니다.
- 대규모 커뮤니티: 방대한 양의 패키지와 라이브러리, 그리고 활발한 커뮤니티가 존재합니다.
간단한 파이썬 문법 예시를 통해 파이썬의 기본적인 문법을 살펴보겠습니다.
1. 변수와 데이터 타입
# 변수 선언 및 초기화
x = 10
y = 3.14
name = "Python"
is_active = True
print(x, y, name, is_active)
2. 리스트와 딕셔너리
# 리스트
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple 출력
# 딕셔너리
person = {
"name": "John",
"age": 30,
"city": "New York"
}
print(person["name"]) # John 출력
3. 조건문
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
4. 반복문
# for 반복문
for i in range(5):
print(i) # 0부터 4까지 출력
# while 반복문
count = 0
while count < 5:
print(count)
count += 1
5. 함수
def greet(name):
return f"Hello, {name}!"
print(greet("Alice")) # Hello, Alice! 출력
6. 클래스
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name} and I am {self.age} years old."
# 객체 생성
person1 = Person("Bob", 25)
print(person1.introduce()) # My name is Bob and I am 25 years old. 출력
7. 파일 입출력
# 파일 쓰기
with open("example.txt", "w") as file:
file.write("Hello, Python!")
# 파일 읽기
with open("example.txt", "r") as file:
content = file.read()
print(content) # Hello, Python! 출력
728x90