[Python_basic]함수(연산자, 랜덤, 문자열처리)

Ronie🌊·2021년 1월 8일
0

Python👨🏻‍💻

목록 보기
2/11
post-thumbnail

git 바로가기


연산자
랜덤
문자열 처리


연산자

  • abs(n) = abstract(절대값)
  • pow(n1, n2) = 제곱값 n1에n2승
  • max(n1, n2, n3) = n1, n2, n3 중에 최대값
  • round(n1, n2) = 반올림함수, n1해당 숫자의 n2번째 소수점에서 반올림한다.
  • floor(n) = 내림함수, 무조건 내림한다.
  • trunc(n) = 내림함수, 0으로 향하여 내림한다.
print(abs(-5))
print(pow(4,2))
print(max(5,12))
print(round(3.14))
# math 연산자 관련 모듈
from math import *
print(floor(-4.99))
print(trunc(-4.99))
출력결과
5
16
12
3.1
-5
-4

랜덤

코딩테스트에 잘 쓰이는 랜덤함수이다.

from random import *
print(random()) # 0.0 ~ 1.0 미만의 임의의 값 생성
print(random()*10) # 0.0 ~ 10.0 미만의 임의의 값 생성
print(int(random()*10)) # 0 ~ 10 미만의 임의의 값 생성
print(int(random()*10)+1) # 1 ~ 10 이하의 임의의 값 생성
print(int(random()*45)+1) # 1 ~ 45 이하의 임의의 값 생성(로또)
print(randint(1,46)) # 1 ~ 45 이하의 임의의 값 생성(로또)
출력결과
0.3424331718946467
6.869113110524587
7
10
26
4

문자열 처리

python = "Python is Amazing"
print(python.lower())#소문자로
print(python.upper())#대문자로
print(python[0].isupper())#n번째 자리 대문자로
print(len(python))#문자열 길이
print(python.replace("Python","Java"))#n1찾아서n2로 대체
index = python.index("n")#첫번째 n 자리 찾기
print(index)
index = python.index("n",index + 1)#두번째 n 자리 찾기
print(index)
print(python.find("Java")) #없으면 -1 반환
#print(python.index("Java")) #없으면 바로 에러발생하여 다음 처리에 문제가 생길 수 있다.
print(python.count("n"))
출력결과
python is amazing
PYTHON IS AMAZING
True
17
Java is Amazing
5
15
-1
2

0개의 댓글