[python] math, random, platform
A A

목차

    728x90

    1. math 모듈

    math — Mathematical functions

    This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if...

    docs.python.org


    일반 연산을 넘어, 조금 더 복잡한 산술 연산을 위한 math module.
    사용을 위해 import math 를 선언해야 한다.

    import math
    
    # math.ceil(x)	올림	
    math.ceil(2.3)	 # 3
    
    # math.floor(x)	내림	
    math.floor(2.9)  # 2
    
    # math.trunc(x)	소수점 버림	
    math.trunc(2.9)	# 2
    
    # math.sqrt(x)	제곱근	
    math.sqrt(16)	 # 4.0
    
    # math.pow(x, y)	거듭제곱	
    math.pow(2, 3)	 # 8.0
    
    # math.pi	원주율	
    math.pi	 # 3.14159...
    
    # math.e	자연로그 밑	
    math.e	  # 2.71828...
    

    주의 사항

    - math.pow(x, y)는 float 결과, x ** y는 정수 가능
    - math 모듈은 import math 후 사용해야 함 (math.sqrt(), math.ceil() 등)



    random 모듈

    https://docs.python.org/ko/3.13/library/random.html

    랜덤 숫자 생성 뿐 아니라 다양한 랜덤 관련 함수를 제공한다.
    사용을 위해 import random 을 선언해야한다.

    주요 함수
    import random
    
    # random.random() 0.0 ~ 1.0 실수	
    random.random()	# 0.5421...
    
    # random.randint(a, b)	a~b 정수 포함	
    random.randint(1, 3)	 # 1, 2, or 3
    
    # random.choice(seq)	시퀀스에서 하나	
    random.choice('abc')	 # 'a' 등
    
    # random.sample(seq, k)	중복 없이 k개	
    random.sample([1,2,3,4], 2)	  #[2,4]
    
    # random.seed(n)	랜덤 고정	
    random.seed(1)  #결과 반복 가능 
    # 시드 값 마다 결과값이 고정되어 나와, 인자 값 변경시에 다른 값으로 고정됨을 볼 수 있음.

    주의 사항

    - random.randint(a, b)는 a와 b 포함이다.
    - random.random()은 항상 0.0 이상, 1.0 미만


    platform 모듈

    https://docs.python.org/ko/3.13/library/platform.html

    platform —  Access to underlying platform’s identifying data

    Source code: Lib/platform.py Cross platform: Java platform: Windows platform: macOS platform: iOS platform: Unix platforms: Linux platforms: Android platform: Command-line usage: platform can also ...

    docs.python.org


    현재 실행중 시스템 정보를 파악하는데 사용되는 모듈이다.
    운영체제와 하드웨어에 대한 정보를 얻을 수 있다.
    이러한 정보를 통해 시스템에 따라 다르게 동작해야하는 애플리케이션의 개발을 할 때 유용하다.

    주요 함수
    # platform.system()	운영체제 이름	
    platform.system()	# 'Windows', 'Linux' 등
    
    # platform.machine()	아키텍처	
    platform.machine()	# 'x86_64'
    
    #platform.processor()	CPU 정보	
    platform.processor()	  # 'Intel64 Family 6'
    
    #platform.version()	OS 버전	
    platform.version()  # 	'10.0.19042'
    
    #platform.platform()	전체 정보 문자열	
    platform.platform()  # 	'Windows-10-10.0.19042-SP0'

    주의 사항

    platform 함수들은 반환값이 문자열이다.

    Copyright 2024. GRAVITY all rights reserved