본문으로 건너뛰기

파이썬 요약정리 Python Cheat Sheet

기본

자료형

  • Number
    • integer : 3
    • float : 3.14
  • String : 'Jace'
  • Boolean : True, False
  • List : [1, 2, 3]
    a = [10, 20, 30]      # 10, 20, 30
    a.append(50) # 10, 20, 30, 50
    a.insert(3, 40) # 10, 20, 30, 40, 50
    a.remove(30) # 10, 20, 40, 50
    a.append(3) # 10, 20, 40, 50, 30
    a.sort() # 10, 20, 30, 40, 50
    a.sort(reverse=True) # 50, 40, 30, 20, 10
    a[1] # 40
    a[:3] # 50, 40, 30
    10 in a # True
    len(a) # 5
    a + [1, 2] # 50, 40, 30, 20, 10, 1, 2
    a * 2 # 50, 40, 30, 20, 10, 50, 40, 30, 20, 10
  • Dictionary : {'name': 'Jace', 'sex': 'male', 'birth': 11}
    person = {'name': 'Jace', 'sex': 'male', 'birth': 11}
    person['name'] # Jace
    del person['birth'] # person = {'name': 'Jace', 'sex': 'male'}

비교연산자

  • == 같다
  • != 다르다
  • > 왼쪽이 크다
  • >= 왼쪽이 크거나 같다
  • <= 왼쪽이 작거나 같다
  • < 왼쪽이 작다

논리연산자

  • and
  • or
  • not

조건문

if 조건1:
실행1
elif 조건2:
실행2
else:
실행

반복문

for i in [1, 2, 3]:
print(i)

for i in range(3): # 0, 1, 2
pass

for i in range(2, 10, 2): # 2, 4, 6, 8
pass

출력

print(f"출력할 내용{변수}")

print(f"{1234.5678:,}")     # 1,234.5678
print(f"{1234.5678:,.3}") # 1.23e+03
print(f"{1234.5678:,.7}") # 1,234.568
print(f"{1234.5678:,.3f}") # 1,234.568

Module, Package

user > cal.py
def plus(a, b):
return a + b
import user.cal

cal.plus(3, 4)
import user.cal import plus
plus(3, 4)