Develop/파이썬 (Python)

[Python] Repl.It - 입출력/나누기/올림/내림/math module

안다희 2019. 1. 16. 00:20
728x90

- string끼리 출력 : +


# Read a string:

a = input()

# Print a string:

print("Hello, " + a + "!") 



- string + int 출력 : ,


# Read an integer:

a = int(input())

# Print a value:

print("The next number for the number ", a, " is ", (a + 1))

print("The previous number for the number ", a, " is ", (a - 1)) 


- 정수 나누기 종류


print(63 / 5) : 소수점까지 나눈다

print(63 // 5) : 정수까지만 나눈다

print(63 % 5) : 나머지 계산


12.6

12

3


- float

# int는 무조건 내림

# round는 반올림


print(17 / 3)  # gives 5.66666666667

print(2 ** 4)  # gives 16

print(2 ** -2) # gives 0.25


print(int(1.3))   # gives 1

print(int(1.7))   # gives 1

print(int(-1.3))  # gives -1

print(int(-1.7))  # gives -1


print(round(1.3))   # gives 1

print(round(1.7))   # gives 2

print(round(-1.3))  # gives -1

print(round(-1.7))  # gives -2


print(0.1 + 0.2)  # gives 0.30000000000000004 


- input : 1.79 / output : 7 (소수점 첫째자리)


a = float(input())

a = int(a * 10) # 강제 형변환!

print(a % 10) 


- math module


import math


x = math.ceil(4.2)

print(x) // 5

print(math.ceil(1 + 3.8)) // 5 


from math import ceil

 

x = 7 / 2

y = ceil(x)

print(y) // 4 


반올림
floor(x)x보다 작은, 또는 최대의 정수 x의 바닥을 돌려줍니다.
ceil(x)x의 최대치, x보다 크거나 같은 최소의 정수를 돌려줍니다.
근원과 대수
sqrt(x)x의 제곱근을 돌려 준다.


FunctionDescription
Rounding
floor(x)Return the floor of x, the largest integer less than or equal to x.
ceil(x)Return the ceiling of x, the smallest integer greater than or equal to x.
Roots and logarithms
sqrt(x)Return the square root of x


https://snakify.org/en/lessons/integer_float_numbers/



- ceil 연습1 : 올림함수


A car can cover distance of N kilometers per day. How many days will it take to cover a route of length M kilometers? The program gets two numbers: N and M. // 700 750 -> 2 출력 


import math


n = int(input())

m = int(input())


rslt = math.ceil(m / n)

print(rslt) 


- ceil 연습2 : 세기 계산


Given a year (as a positive integer), find the respective number of the century. Note that, for example, 20th century began with the year 1901. 


import math


y = int(input())

y = y / 100

y = math.ceil(y)

print(y) // 100으로 나눠서 올림하면 됨~!