Programming Tools/Python_파이썬

[Python] 파이썬 자료형과 연산

LiDARian 2021. 10. 17. 04:15
반응형


자료형

 

# 다양한 자료형 소개 및 자료형 변환 함수를 소개합니다.

# 빈 값이기에 모두 0 혹은 False가 나온다.
int()
float()
complex()
str()
bool()

# 소수 자료형이 정수가 된다.
print(int(1))
print(int(1.3))
print(int(1.8))

# 문자열도 숫자로 바꿔준다.
print(int('1'))
print(float('1.3'))

# 소수/정수 문자열을 교차해서 바꾸는 건 안됨
# print(int('1.3')) 

# 이 경우는 가능
print(float('1'))

# 무한 표현
print(float('inf'))
print(float('-inf'))

# 복소수 표현 : j로 한다.
print(complex(1))
print(complex(2j))
print(complex(3+5j))
print(complex('3+5j'))

# 문자열 표현
print(str(1))
print(str(1.3))
print(str(2+5j))
print(str('abc'))

# bool 연산 : 0 혹은 공백이면 거짓, 그 이외는 모두 참
print(bool(0))
print(bool(1))
print(bool(0.001))
print(bool(''))
print(bool('a'))

# 화면 청소
import os
_ = os.system('cls')
# _ 는 마지막으로 실행된 값(공학용 계산기의 ans를 생각하면 된다.)
# https://mingrammer.com/underscore-in-python/


산술연산

 

# + - * / 는 간단하므로 생략

print(" \n 산술 연산 \n")

print(5 // 2) # 정수만 나오는 나누기
print(5 % 2)  # 나머지 구하기
print(abs(-3)) # 절대값 구하기
print(divmod(5,3)) # 몫,나머지 튜플 반환
print(pow(2,4))    # 2의 4제곱
print(2**4) # 2의 4제곱



# 논리 연산 < <= > >= == != 는 너무 쉬우니깐 생략
# is, is not은 type을 확인하는 데 자주 사용

print(" \n 논리 연산 \n")

print(type(3.1) is type(3))
print(type(3.1) is not type(3))

# Boolean 연산은 and, or, not이 있다.
# 왼쪽부터 오른쪽으로 연산 방향이 정해져 있다.
# 상수를 and or 하는 경우 맨 뒤에 있는 것이 결과로 나온다.
print(" \n 불리언 연산 \n")

print(True and True)
print(True and True and True)
print(True and True and False)
print(True and False or False)
print(1 and 100)
print(1 or 100)

# 비트 연산
print(" \n 비트 연산 \n")
print(~0b1010)
print(bin(0b1010 ^ 0b0110))
반응형