학습내용
다양한 IF 문의 구조를 배우고 어떤식으로 활용 할수있는지에 대해 배웠습니다.
번외로 모듈의 정의와 Random모듈 실습을 하였습니다.
if (조건):
조건이 참일 경우 실행
Python
복사
if (조건):
조건이 참일 경우 실행
else:
조건이 참이 아닐 경우 실행
Python
복사
if (조건1):
if (조건2):
if (조건3):
조건 1,2,3 이 전부 맞을시 실행
이런식으로 코드를 짜면 더러우니
if (조건1 and 조건3 and 조건3):
조건 1,2,3 이 전부 맞을시 실행
Python
복사
중첩 If문의 구조
if (조건1):
실햄문1
else:
if(조건2):
실행
else:
실행문 2
Python
복사
이런 코드로 짤수있지만 위 코드는 더러우니
if (조건1):
실행문1
elif (조건2):
실행문2
else:
실행문3
Python
복사
으로 짜면 가독성도 좋고 코드를 한눈에 읽기도 편하다
참인 부분에 IF가 중첩 되는경우
if (x < 10):
if (x % 3 == 0):
if(x % 2 == 0):
출력문;
if (x<=10 and x%3 == 0 and x%2 == 0)
과 표현식이 같다
Python
복사
파이썬에서는 다양한 조건문을 제공한다
in | not in |
x in 리스트 | x not in 리스트 |
x in 튜플 | x not in 튜플 |
x in 문자열 | x not in 문자열 |
1 in [1, 2, 3] # [1, 2, 3] 안에 1이 있는가?
1 not in [1, 2, 3] # [1, 2, 3] 안에 1이 없는가?
출력 결과:
True
False
Python
복사
튜플과 문자열에도 적용이 가능하다
pocket = ['paprer', 'phon', 'money']
if 'money' in pocket:
print('택시를 탈수 있음')
else:
print('걸어서 가야함')
Python
복사
위 코드 처럼 활용 할수있다.
모듈 사용하기
파이썬 모듈은 상관있는 코드들을 모아둔 파일 이다. 함수와 변수, 클래스등이 잘 정리 되어있다.
파이썬에는 많은 모듈이 있다.
대다수는 미리 만들어진 모듈을 사용할 것이다.
import calendar
print(calendar.prmonth(2024,3))
March 2024
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Python
복사
위 처럼 달력을 쉽게 만들어주는 코드이다.
Import 모듈명 as 별명
위 예제를 보면 prmonth() 함수를 쓰기위해 앞에 모듈명을 입력 했지만
하지만 모듈명이 너무 길다면 줄일수도 있습니다.
import calendar as cal
print(cal.prmonth(2024,3))
Python
복사
모듈명 조차 쓰기 귀찮다면 이렇게 하세요!
from calendar import *
print(prmonth(2024,3))
Python
복사
from calendar import prmonth
print(prmonth(2024,3))
Python
복사
하지만 위 방법은 겹치는 함수명이 있으면 오류가 발생할수도 있으니 사용에 권장 하지 않습니다.
난수 만들기 모듈(random)
앞에서 했듯이 random 이라는 모듈을 가져올려면 import ramdom 를 이용 하면 된다!
위처럼 벌명으로도 가져올수있는데 import random as r 로 하면 함수명을 r로 사용할 수 있다.
Random 속 함수 알아보기
random.random() : 0.0 ~ 1.0 0이상 1미만 실수 중 난수를 생성
random.randint(n, m): n이상, m이하의 정수 중 난수를 생성
random.randrange() : range()함수로 생성된 정수 중 난수 생성
random.uniform(n, m): n이상 m미만의 실수 값 생성
random.sample(반복가능한개체, k): 반복가능한 개체에서 k개의 난수를 리스트 형태로 생성(중복x)
원소 썩기:
random.shuffle(리스트)
random.choice(리스트) : 무작위로 하나의 원소를 추출
import random as r
card = ['A', '4', '1', 'K', 'Q']
print(card)
r.shuffle(card)
print(card)
print(r.choice(card))
Python
복사
어려웠던점
elif 문을 배우기 전엔 조건문이 여러개일 경우 코드를 해석하기가 어려웠었다
if (a == 1):
print("a는 1")
else:
if (b == 1):
print("b는 1")
else:
if (c == 1):
print("c는 1")
else:
if (d == 1):
print("d는 1")
else:
if (e == 1):
print("e도 1")
Python
복사
이 코드는 한눈에 읽기가 힘들고 어떻게 진행되는지 눈으로 보기가 어렵다는 단점이 있었다
해결 방법
if a == 1:
print("a는 1")
elif b == 1:
print("b는 1")
elif c == 1:
print("c는 1")
elif d == 1:
print("d는 1")
elif e == 1:
print("e는 1")
Python
복사
이 코드는 처음부터 차례로 조건을 확인하고, 각 조건이 만족되는 경우에만 해당 조건문을 실행 한다.
이런식으로 코드를 활용해서 읽기쉽게 만들었다.
In 2013, my laptop was stolen and I had to re-configure everything. At that time, there were thousands of themes out there, but none of them were appealing to me. So I decided to create my own.
Fast forward to 2020, Dracula is one of the most popular themes in the world. Still, I felt that something was missing. I wanted to help with more than just a theme, that's why I created Dracula PRO.
This is a package built for developers who are willing to invest in their productivity.
Easy on the Eyes
Dark mode is everywhere, and there's a reason for that. From improved battery consumption to better visibility on low-light environments, there are many reasons to love.
Less Context Switch
By having the same color scheme across multiple apps, you reduce the time it takes to switch context between tasks. That's why Dracula PRO is available in as many platforms as possible.
Precise Contrast
The entire palette was tested against the WCAG 2.0 level AA spec, which requires a contrast ratio of at least 4.5:1 for normal text, therefore affording the best readability.
