02. Python Basics - Numeric Type(숫자형)(int, float, complex)

Jayson Hwang·2022년 4월 4일
0

Python Basics

목록 보기
2/10

2. Numeric Type(숫자형 데이터 타입)

int(정수), float(실수), complex(복소수)

x = 1
print(type(x)) # int

y = 2.8 # float
print(type(y)) # float

z = 1j # complex
print(type(z)) # complex

2-(1) Int(Integer)

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

	x = 1
    y = -324123
    z = 234958092834095234...

2-(2) Float

Float, or "floating point number" is a number, positive or negative, containing one or more decimals.

	x = 1.10
    y = 1.0
    z = -34.123

2-(3) Complex

Complex numbers are written with a "j" as the imaginary part
a+bi 로 나타낼 수 있는 숫자, a(실수부)와 b(허수부)는 실수, i는 허수 단위로 i2 = -1을 만족

	x = 3 + 5j
    y = 5j
    z = -5j

2-(4) 연산자

▶︎ 사칙연산
	◦ 더하기(+)
    	print(1+2) # 3
        
    ◦ 빼기(-)
    	print(2-1) # 1
        
    ◦ 곱하기(*)
    	print(2*3) # 6
        
    ◦ 나누기(/)
    	print(6/2) # 3
        
    ◦ 제곱(**)
    	print(2**3) # 8
        
    ◦ 몫 구하기(//)
    	print(5//3) # 1
        
    ◦ 나머지 구하기(%)
    	print(5%3) # 2
▶︎ 참 & 거짓
    - print(10 > 3) # True
    - print(4 >= 7) # False
    
    - print(3 == 3) # True (앞과 뒤가 동일하다)
    - print( 3 + 4 == 7) # True
    
    - print(1 != 3) # True (앞과 뒤가 같지 않다)
    - print(not(1 != 3)) # False
    
    - print((3 > 0) and (3 < 5)) # True
    - print((3 > 0) & (3 < 5)) # True
    
    - print((3 > 0) or (3 > 5)) # True
    - print((3 > 0) | (3 > 5)) # True

2-(5) 수식

  number = 2 + 3 * 4
  print(number) # 14
  
  number = number + 2
  print(number) # 16
  
  number += 2 (위에 "number = number +2"와 완전히 동일함)
  print(number) # 18
  
  number *= 2
  print(number) # 36
  
  number /= 2
  print(number) # 18
  
  number -= 2
  print(number) # 16

  number %= 5
  print(number) #1

2-(6) 숫자처리 함수

  print(abs(-5)) #5 -> "absolute"(절대값)
  print(pow(4, 2)) #4^2 = 16 -> "Power"
  print(max(5, 12)) #12 -> 최대값 (5와 12 중 최대값)
  print(min(5, 12)) #5 -> 최소값 (5와 12 중 최소값)
  print(round(3.14)) #3 -> 반올림
  print(round(4.99)) #5

  from math import * -> math library안에 있는 모든 것을 이용하겠다는 뜻
  print(floor(4.99)) #4 -> 내림
  print(ceil(3.14)) #4 -> 올림
  print(sqrt(16)) #4 -> 제곱근

2-(7) 랜덤함수(난수)

  from random import *
  print(random()) -> 0.0 ~ 1.0 미만의 임의의 값 생성
  print(random() * 10) -> 0.0 ~ 10.0 미만의 임의의 값 생성
  print(int(random() * 10)) -> 0 ~ 10 미만의 임의의 정수 값 생성("int" 사용)
  print(int(random() * 10) + 1) -> 1 ~ 10 이하의 임의의 정수 값 생성

ex) 로또번호 생성 예제
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성
  print(int(random() * 45) + 1) #1~45 이하의 임의의 값 생성

"randrange"를 사용하여 위와 동일한 값을 구하는 함수를 아래와 같이 다시 만들 수 있다.
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성
  print(randrange(1, 46)) #1~46 미만의 임의의 값 생성

미만, 이상이 헷갈리는 경우,
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성
  print(randint(1, 45)) #1~45 이하의 임의의 값 생성

ex) 예제

#조건1: 랜덤으로 날짜를 뽑아야함
#조건2: 월별 날짜는 다름을 감안하여 최소 일수인 28일 이내로 정함
#조건3: 매월 1~3일은 준비기간으로 선정에서 제외

  from random import *
  date = randint(4, 28)
  print("오프라인 스터디 모임 날짜는 매월 " +str(date)+ "일로 선정되었습니다.")

-> 결과: 오프라인 스터디 모임 날짜는 매월 (4~28일 사이 랜덤 숫자)일로 선정되었습니다.
profile
"Your goals, Minus your doubts, Equal your reality"

0개의 댓글