본문 바로가기
Hello World/Python

Python - python memo 변수과 계산 / 문법 익히기 [개발스터디 기몬]

by 기몬 2023. 3. 14.
728x90
반응형

Python

 

Python memo 


python 에서 실수는 float 하나 _ 정수는 int 뿐이다. 
java와 다르게 각각 하나씩 존재

cmd 창에서 ipython을 사용해서 type을 확인해본다. 
  In [5]: type(10/3)
Out[5]: float

In [6]: type(10.0/3)
Out[6]: float

In [8]: type(10//3)
Out[8]: int


floor division in python  : 
In this tutorial, you'll learn how to perform floor division in Python. 
You'll use Python's // operator, the floor function from Python's math module, 
and more – with code examples.

Arithmetic Operators in Python.
Operator Syntax Result
Floor division (/) num1//num2 Returns the quotient when num1 is divided by num2

ex )  -3.3333 >>> floor >>> 4   /   4 >>> ceil >>> 3 
floor division = 나눴을 때 /를 하나 더 추가하면 3.333 보다는 작지만 가장 큰 정수로 ceiling 한다. 
음수에서 -10//3 일때 -3.333 보다는 크지만 가장 작은 정수로 floor한다.

 

======================================================
jshell에서 floor 의 의미를 확인 했을 때
jshell> Math.floor(3.333)
$1 ==> 3.0

jshell> Math.floor(-3.3333)
$2 ==> -4.0
 
Python에서는 문자열을 표현할 때 ' 또는 '' 둘 다 사용이 가능하고
이스케이프 방식은 자바와 같이 \ 역슬래시를 사용한다. 

Python에서 모든 변수는 참조타입 ! 


==============================================================

중요 !!! 프로그래밍 
타입에 대한 이해 . 

wrapper primitive type Java 
 :  primitive type 에 대응되는 참조타입을  wrapper type 이라고 한다 
자바에서 변수타입을 대문자 풀네임 으로 작성. 

< java >

public class WrapperTypeExample {

public static void main(String[] args) {
// Reference type
// wrapper type = Primitive type to Reference type 
Byte b1 = 100;
Short s1 = 200;
    Character  c1 = 'A';
Integer i1 = 300;
Long l1 = 400L;

Float f1 = 10.5f;
Double d1 = 10.5;

Boolean tf = true;

}

public static void main2(String[] args) {
// Primitive type
byte b1 = 100;
short s1 = 200;
char c1 = 'A';
int i1 = 300;
long l1 = 400;

float f1 = 10.5f;
double d1 = 10.5;

boolean tf = true;

}

}


그렇지만 python은 다르다 


Python 은 타입을 입력하지 않지만 
변수가 가르키는 값을 type 함수를 사용해서 타입을 확인 할 수 있다.

< python >

b1 = 100
s1 = 200
c1 = 'A'    # 문자열
i1 = 300
l1 = 400 

f1 = 10.5
d1 = 10.5

tf = True

print("b1 = ", type(b1))    # int
print("s1 = ", type(s1))    # int
print("c1 = ", type(c1))    # str
print("i1 = ", type(i1))    # int
print("l1 = ", type(l1))    # int

print("f1 = ", type(f1))    # float
print("d1 = ", type(d1))    # float

print("tf = ", type(tf))    # bool

이렇듯 python 은 무조건 참조타입이고 
java의 wrapper type과 같은 type들은 
# 정수타입 int 와 실수타입 float
# 문자열 str / 논리형 bool  이 있다. 

파이썬의 타입 ==> (무조건 참조 타입) 
#
# 정수    int
# 실수    float
# 논리    bool    
# 문자열  str ==> python은 문자(char)타입이 없다.




In [25]: import math

In [26]: math.sqrt(2)
Out[26]: 1.4142135623730951

In [27]: math.pow(2,3)
Out[27]: 8.0

In [28]: math.radians(180)
Out[28]: 3.141592653589793

In [29]: math.radians(144)
Out[29]: 2.5132741228718345

In [30]: math.degrees(3.14)
Out[30]: 179.9087476710785

In [31]: math.degrees(math.pi)
Out[31]: 180.0

In [32]: math.degrees(2*math.pi)
Out[32]: 360.0

In [33]: math.cos(math.radians(45))
Out[33]: 0.7071067811865476



2의 3 승 / 2의 10 승
In [36]: 2**3
Out[36]: 8

In [37]: 2**10
Out[37]: 1024

In [38]: 2*3
Out[38]: 6

In [39]: math.pow(2,3)
Out[39]: 8.0

In [40]: math.pow(2,10)
Out[40]: 1024.0

In [41]: math.pow(2,100)
Out[41]: 1.2676506002282294e+30


python 
round - half even 

half up : 실수 5이상은 무조건 반올림 
half even : 파이썬에서 half even 법 
  홀수에서는 반올림
짝수에서는 버림
- 발생하는 오류의 이유 : 1.8500001 작은 자리가 한자리 더 있다면 
1.85보다 더 크다고 생각하고 반올림이 된다. 
python의 float은 IEEE 754로 계산됨.

부동소수점 계산은 오차에 주의해야 한다 
정확한 실수계산을 하기 위해서 라이브러리를 사용해야한다. 


round( 실수 , 구하고자 하는 소수점자리)
round(Decimal('실수'), 구하고자 하는 소수점자리) # 라이브러리 사용. 



In [41]: from decimal import Decimal


In [43]: round(Decimal('1.85'),1)
Out[43]: Decimal('1.8')

In [44]: round(1.85, 1)
Out[44]: 1.9

In [49]: round(Decimal('1.21345'),1)
Out[49]: Decimal('1.2')

In [50]: round(Decimal('1.21345'),4)
Out[50]: Decimal('1.2134')

In [51]: round(Decimal('1.21325'),4)
Out[51]: Decimal('1.2132')

In [52]: round(Decimal('1.21365'),4)
Out[52]: Decimal('1.2136')

In [53]: round(Decimal('1.21385'),4)
Out[53]: Decimal('1.2138')

In [54]: round(Decimal('1.21375'),4)
Out[54]: Decimal('1.2138')

In [55]: round(Decimal('1.21395'),4)
Out[55]: Decimal('1.2140')





ch = chr(int(r.random()*26) + ord('A'))           # 코드값을 알아내는 함수 ord 

In [1]: ord('A')
Out[1]: 65

In [2]: ord('a')
Out[2]: 97

In [3]: ord('0')
Out[3]: 48

In [4]: chr(65)
Out[4]: 'A'

In [5]: chr(97)
Out[5]: 'a'

In [6]: chr(48)
Out[6]: '0'

728x90
반응형

댓글