Study Record

[파이썬] 기초 본문

서버보안/리눅스 서버보안

[파이썬] 기초

초코초코초코 2021. 11. 23. 15:52
728x90

※ python3 기준

 

☞ 여러줄 문자열( """ """ / ''' ''')

a = """Hello, my name is kkk.

Welcome"""

 

☞ 주석

#       : 한줄의 경우

""" """ : 여러줄 가능

 

☞ 한글 인코딩 방식

EUC-KR

CP949

UTF-8

UTF-16

 

☞ 다중 할당 가능 (권장하지는 않는다.)

a = b = c = 1      # a = 1 , b = 1 . c = 1

a , b = 1 , 3        # a = 1 , b = 3

a , b = b , a        # a 와 b 를 교체한다.

 

☞ help 함수

help(input)

 

☞ 출력 - print() 함수 사용법

a , b , c = 'happy' , 'python' , 'wow!';

print(a, b, 'is', c)  # (,)은 공백 한 칸을 띄워준다.
print("hello %s %s" %(c, a))
print( b + " is " + a )   # + 는 문자열을 이어붙인다.
print("happy", end='')  # "end=" 마지막 문자 표시 설정 (default : '\n')
print("end")

 

☞  이스케이프 문자

코드 설명 코드 설명
\n 개행(줄바꿈) \" 이중 인용부호(")
\t 수평 탭 \r 캐리지 리턴
\\ 문자 "\" \b 백 스페이스
\' 단일 인용부호(') \000 널문자

 

☞  입력 - input()

name = input("Enter your name : ")
print(name)

※ python2

raw_input()

 

☞ 자료형 확인 - type() 함수

수치형(Numeric Types) : int, float, complex
- 정수(int)
- 더 큰 정수(long int)
- 소수(float)
- 복소수(complex)

순서형(Sequence Types) : str, list, tuple, range
- 문자열(string)
- 리스트(list)
- 튜플(tuple)

매핑형(Mapping Type) : dict
- 딕셔너리(dictionary)

집합형(Set Types) : set, frozenset
- 집합(set)
- frozenset

부울형(Boolean Type) : bool
- 부울(bool)

바이너리형(Binary Types) : bytes, bytearray, memoryview

 

형변환 / 생성자 함수

데이터 타입  생성자 함수 사용 예
str x = str("Hello World")
int x = int(20)
float x = float(20.5)
complex x = complex(1j)
list x = list(("apple", "banana", "cherry"))
tuple x = tuple(("apple", "banana", "cherry"))
range x = range(6)
dict x = dict(name="John", age=36)
set x = set(("apple", "banana", "cherry"))
frozenset x = frozenset(("apple", "banana", "cherry"))
bool x = bool(5)
bytes x = bytes(5)
bytearray x = bytearray(5)
memoryview x = memoryview(bytes(5))

 

☞ 산술 연산자

a, b = 4, 10
print(a + b)
print(b - a)
print(a * b)
print(b ** a)
print(b / a)
print(b // a)     # 나머지

 

 

 

728x90