[ Function and Console I/O ]
sort vs sorted
sorted : 기존 리스트는 건드리지 않고 정렬된 새로운 리스트를 리턴한다. 파라미터 존재.
ex) sorted( my_list, (reverse=True) )
sort() : 아무것도 리턴하지 않고 기존 리스트를 정렬한다. 파라미터가 존재하지 않음
list_ex = [5,4,3,2,1]
# sorted 함수는 sorting이 된 함수를 복사한 리턴값을 가지고 있다.
print(sorted(list_ex))
# sort 함수는 리스트 자체에 변화가 일어난다. 리턴값이 존재하지 않음.
list_ex.sort()
print(list_ex)
input
input은 string 타입만 받을 수 있다.
* 타입이 다른 값들을 연속해서 출력하기 위해서는 콤마(,)를 사용해서 출력해야한다.
level = float(input("레벨을 입력하세요 : ")) # float로 형변환하기
print("레벨은 ", level, "lv 입니다.")
print(type(level))

% format
| type | |
| %s | 문자열 ( String ) |
| %c | 문자 1개 ( Character ) |
| %d | 정수 ( Integer ) |
| %f | 부동 소수 ( floating-point ) |
| %o | 8진수 |
| %x | 16진수 |
| %% | Literal % ( 문자 % 자체 ) |
* 소수점 나타내기
소수점을 표현하고 싶은 경우 " : + . + 숫자 + f " 를 작성해 줘야함
PI = 3.141592653589
print("원주율 값 : {}".format(PI))
#소수점 5자리까지 표현
print("원주율 값 : {:.5f}".format(PI))
#앞에 10칸을 비우고 표현
print("원주율 값 : {:10.5f}".format(PI))
#fString
print(f"원주율 값 : {PI:.5f}")
print(f"원주율 값 : {PI:10.5f}")
Padding
> : 오른쪽부터 정렬
< : 왼쪽부터 정렬
^ : 가운데부터 정렬
# 10칸을 비우고 정렬해라
# 왼쪽 비우기
print("오렌지 : {:>10s}".format("orange"))
# 오른쪽 비우기
print("오렌지 : {:<10s}".format("orange"))

name = "2CHAECHAE"
print(f"Welcome {name} TISTORY!")
print(f"{name:20}") # 오른쪽부터 20칸 채우기
print(f"{name:>20}") # 왼쪽부터 20칸 채우기
print(f"{name:*<20}") # name 이후에 20칸까지 *로 채우기
print(f"{name:*>20}") # name 이전에 20칸에 맞게 *로 채우기
print(f"{name:*^20}") # name을 가운데로 양쪽으로 20칸 채우기

섭씨 화씨 변환기
섭씨 온도를 화씨로 변환해주는 프로그램 만들기
화씨 온도 변환 공식 : ( (9/5) * 섭씨온도 ) + 32
# 섭씨 온도 변환 공식 : ( (9/5) * 섭씨온도 ) + 32
def celsius_to_fahrenheit(my_temperature) :
fah_temp = ( (9/5) * my_temperature ) + 32
return fah_temp
temperature = float(input("변환하고 싶은 섭씨 온도를 입력해주세요 : "))
print("섭씨 온도 : {:.2f}".format(temperature))
print("화씨 온도 : {:.2f}".format(celsius_to_fahrenheit(temperature)))
* round 함수 : 반올림
print( round(3.141592) )
# 소수점 둘째 자리까지 반올림하고 싶은 경우 파라미터를 사용
print( round(3.141592,2) ) # 3.14
print( round(3.141592,4) ) # 3.1416
'Python > 네이버 부스트코스 AI BASIC 코칭스터디' 카테고리의 다른 글
| [ AI 코칭스터디 ] 1주차 학습 ( Python Data Structure ) (0) | 2022.01.21 |
|---|---|
| [ AI 코칭스터디 ] 1주차 학습 ( String and advanced function concept ) (0) | 2022.01.21 |
| [ AI 코칭스터디 ] 1주차 학습 ( Conditionals and Loops ) (0) | 2022.01.19 |
| [ AI 코칭스터디 ] 1주차 학습 ( Variables ) (0) | 2022.01.17 |
| [ AI 코칭스터디 ] 1주차 학습 ( 파이썬 준비하기 ) (0) | 2022.01.16 |