아래는 파이썬에서 쉽게 따라 할 수 있는 간단한 실습 예제입니다.
1. Hello, World! 출력
파이썬 코딩의 기본입니다.
print("Hello, World!")
결과:
Hello, World!
2. 숫자 계산하기
간단한 계산을 해봅시다.
a = 10
b = 20
print(a + b) # 덧셈
print(a * b) # 곱셈
print(b / a) # 나눗셈
print(b - a) # 뺄셈
결과:
30
200
2.0
10
3. 조건문
숫자가 짝수인지 홀수인지 확인하는 코드입니다.
number = 7
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
결과:
7 is odd.
4. 반복문
1부터 10까지의 합을 계산하는 코드입니다.
total = 0
for i in range(1, 11):
total += i
print("The sum of numbers from 1 to 10 is:", total)
결과:
The sum of numbers from 1 to 10 is: 55
5. 리스트 다루기
리스트의 요소를 하나씩 출력합니다.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}")
결과:
I like apple
I like banana
I like cherry
6. 함수 사용하기
숫자의 제곱을 계산하는 함수입니다.
def square(num):
return num ** 2
print(square(4)) # 4의 제곱 출력
print(square(9)) # 9의 제곱 출력
결과:
16
81
## 7. 사용자 입력 받기
사용자로부터 입력을 받아 계산합니다.
name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello, {name}! You will be {age + 1} years old next year.")
결과:
What is your name? John
How old are you? 25
Hello, John! You will be 26 years old next year.
8. 파일 읽고 쓰기
간단한 파일 입출력을 해봅시다.
# 파일에 쓰기
with open("example.txt", "w") as file:
file.write("This is a test file.")
# 파일 읽기
with open("example.txt", "r") as file:
content = file.read()
print(content)
결과:
This is a test file.
'Python 연습 > 1. Python 설치와 실습 - 아나콘다' 카테고리의 다른 글
Q_01_02. 파이썬 다운로드하고 설치하기 (0) | 2025.04.27 |
---|---|
Q_01_01. 파이썬이란 무엇인가? (0) | 2025.04.27 |
Q_01_51. 가상환경 이란 무엇인가? (0) | 2025.03.20 |
Q_01_42. 아나콘다 다운로드와 설치하기 (0) | 2025.03.20 |
Q_01_41. 아나콘다란 무엇인가? (0) | 2025.03.20 |