본문 바로가기
Python

(파이썬 다시 보기)파이썬, python: 1. 자료형_숫자형, 문자열, bool

by 하방주인장 2023. 5. 1.
반응형

목차

     

    1. 숫자형

    - 정수형(int)

    - 실수형(float): 소수점이 포함된 숫자

    - 8진수와 16진수: 8진수(0o~), 16진수(0x~)

     

    1-1. 연산자

    a = 10
    b = 3
    a + b
    >>> 13
    
    a - b 
    >>> 7
    
    a * b 
    >>> 30
    
    a ** b # 제곱
    >>> 1000 
    
    a / b 
    >>> 3.33333
    
    a // b # 몫 
    >>> 3
    
    a % b # 나머지 
    >>> 1
    
    # 대입 연산자
    a += 1 
    >>> 11
    
    a -= 1
    >>> 9
    
    a *= 2 
    >>> 20
    
    a /= 2 
    >>> 5
    
    a **= 2 
    >>> 100
    
    # 비트 연산자
    1 & 1 # 비트 AND 연산: 대응되는 비트가 모두 1이면 1을 반환함
    >>> 1
    
    0 | 1 # 비트 or 연산: 대응되는 비트 중에서 하나라도 1이면 1을 반환함
    >>> 1

     

    2. 문자열

    2-1. 인덱싱 & 슬라이싱

    language = 'English'
    language[:2] # == language[0:2]
    >>> 'En' 
    
    language[2:-2]
    >>> 'gli'

     

    2-2. 문자열과 연산자

    'hello' + 'encore'
    >>> 'helloencore'
    
    'hello' + ' ' + 'encore'
    >>> 'hello encore'
    
    'hello' * 3 + ' ' + 'encore'
    >>> 'hellohellohello encore'

     

    2-3. 문자열 출력과 포맷팅

    # 문자열 출력
    print('I\'m a man')
    >>> I'm a man
    
    multiline = """
    i am a woman
    i am a woman
    i am a woman
    """
    print(multiline)
    >>> i am a woman
    	i am a woman
    	i am a woman
        
    print('I\nam a boy')
    >>> I
    	am a boy
    
    # 포맷팅
    age = 25
    name = 'Alice'
    
    print(f'제 이름은 {name}이고 나이는 {age}입니다.')
    print('제 이름은 {name}이고 나이는 {age}입니다.'.format(name, age))
    print('제 이름은 %s이고 나이는 %d입니다.' %(name, age)) #%s: str, %d: int
    >>> 제 이름은 Alice이고 나이는 25입니다.

     

    2-4. 문자열과 함수

    - count: 문자 개수 세기

    text = ' Hello world! '
    text.count('llo')
    >>> 1
    text.count('z') # 찾는 문자가 없으면 0을 반환함
    >>> 0


    - find, index: 위치 알려주기

    text = 'Hello world!'
    text.index('llo')
    >>> 2
    
    text.find('llo')
    >>> 2
    
    # index와 find의 차이
    text.index('z')
    >>> ValueError: substring not found
    
    text.find('z')
    >>> -1



    - join: 문자열 삽입

    '+'.join('12345')
    >>> '1+2+3+4+5



    - upper / lower /capitalize: 대문자/소문자/첫번째 문자만 대문자로 바꾸기

    text = 'hello World'
    text.upper()
    >>> 'HELLO WORLD'
    
    text.lower()
    >>> 'hello world'
    
    text.capitalize()
    >>> 'Hello World'



    - lstrip /strip /rstrip: 윈쪽/양쪽/오른쪽 공백 지우기

    text = ' Hello World '
    text.lstrip()
    >>> 'Hello world! '
    
    text.rstrip()
    >>> ' Hello world!'
    
    text.strip()
    >>> 'Hello world!'



    - replace: 문자열 바꾸기

    text = 'hello world world world'
    text.replace('world', 'python')
    >>> 'hello python python python'
    
    text.replace('world', 'python', 2) # count 설정
    >>> 'hello python python world'



    - split: 문자열 나누기

    text = 'Hello, World'
    text.split(',')
    >>> ['Hello', ' World']

     

    3. bool

    - 불(bool) 자료형이란 참(True)과 거짓(False)을 나타내는 자료형이다. 

    x, y = 3, 4
    
    x == y
    >>> False
    
    x != y
    >>> True
    
    bool(1)
    >>> True
    
    bool(0)
    >>> False
    
    bool([1, 2])
    >>> True
    
    bool([])
    >>>False
    
    # 논리연산자
    True and False
    >>> False
    
    True or False
    >>> True
    
    not True
    >>> False

    댓글