Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Attention
- Clustering
- textmining
- 군집화
- SOMs
- LSTM
- Generative model
- gaze estimation
- Ann
- MLOps
- BERT
- VGGNet
- tensorflow
- 자기조직화지도
- NER
- NMF
- RNN
- AI 윤리
- Python
- Binary classification
- Gradient Descent
- Support Vector Machine
- TFX
- ResNet
- stemming
- Transfer Learning
- cross domain
- nlp
- Logistic Regression
- 경사하강법
Archives
- Today
- Total
juooo1117
if문, while문, for in문(range, enumerate, zip), continue, break 본문
if문
if 조건(1):
명령문
elif 조건(2):
명령문
...
else:
명령문
# 공백을 인정해주고 싶지 않을때 -> 공백제거 .strip()사용
txt = input("text를 입력하세요: ")
if txt.strip():
print("입력한 text: ", txt)
else:
print("입력을 해야합니다.")
num = int(input("양수입력: "))
if num >= 100000:
print("아주커요")
elif num >= 1000:
print("커요")
elif num >= 100:
print("보통이에요")
elif num >= 0:
print("작아요")
while문
while 조건:
반복구문 ...
limit = int(input("반복횟수: "))
count = 0
if limit <= 0:
print("양수를 넣어주세요")
else:
while count < limit:
count += 1
print(f"{count}번째 실행중입니다.")
for in 문
for 변수 in Iterable:
반복구문 ...
# 짝수만 골라서 리스트 만들기
ori_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_list = []
for i in ori_list:
if i % 2 == 0:
even_list.append(i)
print(even_list)
# 중첩리스트를 사용해서 모든 원소 출력하기
ori_list = [[1,2,3], [10,20,30], ["가","나","다","라"]]
new_list = []
i = 0
k = 0
for i in ori_list:
for k in i:
new_list.append(k)
print(new_list)
#결과: [1, 2, 3, 10, 20, 30, '가', '나', '다', '라']
range(시작값, 멈춤값, 증감치)
for i in range(10, -10, -1): # 증감치를 뺌
print(i, end = ", ") # 출력형태를 설정
#결과: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
# n번 반복: for in range(n)
for i in range(5):
print(f"{i+1}. 안녕하세요")
l = list(range(1, 11)) # list안에 값을 넣어줌
enumerate(Iterable ,start = integer)
names = ["유재석", "박명수", "홍길동", "정준하"]
for index, name in enumerate(names, start = 1): # tuple 대입, 시작값을 1로 해줘
print(f"{index}. {name}")
#결과: 1. 유재석 2. 박명수 3. 홍길동 4. 정준하
zip(Iterable1, Iterable2, Iterable3 ... )
여러 개의 Iterable에서 값을 받아서, 같은 index의 값끼리 튜플로 묶어서 반환시킴
names = ["유재석", "박명수", "홍길동", "정준하"]
ages = [20, 30, 40, 50]
addresses = ["서울", "인천", "서울", "대전"]
for name, age, address in zip(names, ages, addresses):
print(f"이름: {name}, 나이: {age}, 주소: {address}")
# 이름: 유재석, 나이: 20, 주소: 서울
# 이름: 박명수, 나이: 30, 주소: 인천
# 이름: 홍길동, 나이: 40, 주소: 서울
# 이름: 정준하, 나이: 50, 주소: 대전
for index, value in enumerate(zip(names, ages, addresses), start = 1):
print(index, value)
# 1 ('유재석', 20, '서울')
# 2 ('박명수', 30, '인천')
# 3 ('홍길동', 40, '서울')
# 4 ('정준하', 50, '대전')
#( )는 tuple!
for index, (name, age, address) in enumerate(zip(names, ages, addresses), start = 1):
print(f"No.{index} 이름: {name}, 나이: {age}, 주소: {address}")
# No.1 이름: 유재석, 나이: 20, 주소: 서울
# No.2 이름: 박명수, 나이: 30, 주소: 인천
# No.3 이름: 홍길동, 나이: 40, 주소: 서울
# No.4 이름: 정준하, 나이: 50, 주소: 대전
Continue, Break 제어
break: 특정한 조건에서 반복문을 중단할 때 사용
continue: 현재의 반복을 멈추고, 다음 반복을 진행할 때 사용
import random
result = []
while True:
num = random.randint(-100, 100) # -100 ~ 100사이의 정수를 랜덤하게 반환
if num == 0: # 랜덤값이 0이면 종료
break
elif num < 0: # -값이면 다음 랜덤으로 돌아감
continue
result.append(num) # 빈 list에 삽입
result.sort()
print(result)
'python' 카테고리의 다른 글
데이터 정렬 & 집계 - pandas (0) | 2023.09.29 |
---|---|
데이터 불러오기 & 조회 및 변경 - pandas (0) | 2023.09.29 |
Comprehension (0) | 2023.08.17 |
List, Tuple, Dictionary, Set (2) | 2023.08.14 |
문자열(string) (0) | 2023.08.11 |