파이썬 자료형
1. 숫자형 (int, float)
2. 연산자:
a = 7
b = 3
print(a // b) # 2
print(a % b) # 1
print(a ** b) # 343
#복합대입연산자
x = 10
x += 5 # x = x + 5
print(x) # 15
3. 문자형 (String)
이스케이프 코드
\n : 줄바꿈
\t : 탭
\ : 역슬래시
\' 또는 \" : 따옴표 출력
print("Hello\nWorld")
print("나는 \"파이썬\"을 좋아해")
# 문자열 길이
s = "Python"
print(len(s)) #6
4. 자료형의 종류: type()
x = 10
print(type(x)) # <class 'int'>
5. 문자열 슬라이싱
# 끝은 포함되지 않음
s = "Python"
print(s[0:2]) #Py
print(s[:3]) # 'Pyt'
print(s[::2]) # 'Pto'
자료형들
1. List (리스트)
리스트명 = [1, 2, 3, 4, 5]
리스트명.append(6)
print(리스트명) #[1,2,3,4,5,6]
2. Tuple (튜플)
튜플명 = (1, 2, 3)
print(튜플명)
3. Set (집합)
집합명 = {1, 2, 3, 3, 4}
print(집합명) # {1, 2, 3, 4}
4. Dictionary (딕셔너리)
딕셔너리명 = {"이름": "홍길동", "나이": 30}
print(딕셔너리명["이름"])
5. Bool (불린형)
파이썬 기초문법
1. 반복문
# while + 조건문
i = 0
while i < 5:
print(i)
i += 1
# continue와 break
i = 0
while i < 5:
i += 1
if i == 3:
continue # 3은 건너뜀
if i == 5:
break # 5에서 종료
print(i)
# for + range()
for i in range(1,6,1)
print(i)
2. 연산자
비교 연산자
= (대입)
== (같다)
!= (다르다)
논리 연산자
and (그리고)
or (또는)
not (부정)
기타 연산자
in, not in (포함 여부 확인)
x = 5
print(x == 5) # True
print(not x == 5) # False
print(3 in [1, 2, 3, 4]) # True
3. 조건문
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C")
4. 함수 (def)
def add(a, b):
return a + b
result = add(3, 4)
print(result) # 7