Python Data Type and Operations¶
In [1]:
import warnings
warnings.filterwarnings('ignore')
I. Python Function¶
1) 함수 실행¶
- print( ) 실행
In [2]:
print('Hello World')
Hello World
- 매개변수(Parameter) 및 인자/인수(Argument) 지정
In [3]:
print('Hello World', end = '\t')
Hello World
In [4]:
print('Hello', 'World', sep = '-')
Hello-World
- 내장 매뉴얼
In [5]:
help(print)
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
2) 객체(Object) 선언¶
- 파이썬 식별자(Identifiers)는 객체, 함수, 클래스, 모듈 또는 다른 개체 식별에 사용되는 이름
- 식별자는 문자(A-Z, a-z)로 시작하고 밑줄(_), 숫자(0~9)를 사용
- 특수문자 @, $, %는 식별자로 사용할 수 없음
- 파이썬은 대소문자를 구분
In [6]:
koo = 'Data Analytics'
print(koo)
Data Analytics
In [7]:
type(koo)
Out[7]:
str
- 예약어(35개)
- 객체명으로 사용할 수 없음
In [8]:
import keyword
keyword.kwlist
Out[8]:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
II. Python Data Type¶
1) Numeric¶
- 정수
In [9]:
print(888)
print(type(888))
888 <class 'int'>
- 8진수
In [10]:
print(0o77)
print(type(0o77))
63 <class 'int'>
- 16진수
In [11]:
print(0xff)
print(type(0xff))
255 <class 'int'>
- 2진수
In [12]:
print(0b1001)
print(type(0b1001))
9 <class 'int'>
- 실수
In [13]:
print(3.14)
print(type(3.14))
3.14 <class 'float'>
- 과학적 표기법
In [14]:
print(5e-4)
print(type(5e-4))
0.0005 <class 'float'>
In [15]:
print(5e+4)
print(type(5e+4))
50000.0 <class 'float'>
- 복소수(Complex)
In [16]:
print(8 + 9j)
print(type(8 + 9j))
(8+9j) <class 'complex'>
2) String¶
In [17]:
print('A')
print(type('A'))
A <class 'str'>
In [18]:
print('Data Analytics')
print(type('Data Analytics'))
Data Analytics <class 'str'>
3) Logical¶
- 참(True)
- 거짓(False)
In [19]:
print(True)
print(type(True))
True <class 'bool'>
In [20]:
print(False)
print(type(False))
False <class 'bool'>
III. 산술 연산(Arithmetic Operation)¶
1) 사칙연산¶
- 덧셈
In [21]:
8 + 9
Out[21]:
17
- 뺄셈
In [22]:
8 - 9
Out[22]:
-1
- 곱셈
In [23]:
8 * 9
Out[23]:
72
- 실수 나눗셈
In [24]:
10 / 3
Out[24]:
3.3333333333333335
- 나눗셈 후 몫 반환
In [25]:
10 // 3
Out[25]:
3
- 나눗셈 후 나머지 반환
In [26]:
10 % 3
Out[26]:
1
- 제곱
In [27]:
9 ** 3
Out[27]:
729
In [28]:
pow(9, 2)
Out[28]:
81
2) 절댓값¶
In [29]:
abs(-3)
Out[29]:
3
3) 진법 변환¶
- 16진수
In [30]:
hex(16)
Out[30]:
'0x10'
- 8진수
In [31]:
oct(8)
Out[31]:
'0o10'
- 2진수
In [32]:
bin(9)
Out[32]:
'0b1001'
4) 반올림¶
- round(number[, ndigits])
In [33]:
round(24.47)
Out[33]:
24
In [34]:
round(24.57)
Out[34]:
25
In [35]:
round(24.57, ndigits = 0)
Out[35]:
25.0
In [36]:
round(24.57, ndigits = 1)
Out[36]:
24.6
In [37]:
round(24.57, ndigits = -1)
Out[37]:
20.0
In [38]:
round(25.47, ndigits = -1)
Out[38]:
30.0
In [39]:
round(25.478, ndigits = 2)
Out[39]:
25.48
IV. 비교 연산(Comparison Operation)¶
1) Numeric¶
In [40]:
8 > 9
Out[40]:
False
In [41]:
8 < 9
Out[41]:
True
In [42]:
8 >= 9
Out[42]:
False
In [43]:
8 <= 9
Out[43]:
True
In [44]:
8 == 9
Out[44]:
False
In [45]:
8 != 9
Out[45]:
True
2) Character¶
- 동작-1
In [46]:
'A' > 'B'
Out[46]:
False
- 동작-2
In [47]:
'A' < 'B'
Out[47]:
True
In [48]:
'A' == 'B'
Out[48]:
False
In [49]:
'A' != 'B'
Out[49]:
True
- Error-1
In [52]:
#'팔' > 9
- Error-2
In [ ]:
#'A' > 9
V. 논리 연산(Logical Operation)¶
In [53]:
X = True
Y = False
Z = True
print(X, Y, Z)
True False True
1) AND¶
In [54]:
X and Y
Out[54]:
False
2) OR¶
In [55]:
X or Y
Out[55]:
True
3) NOT¶
In [63]:
not X
Out[63]:
False
4) 괄호 연산자¶
In [57]:
(X and Y) and (Y or Z)
Out[57]:
False
'# Coding > 데이터 분석을 위한 Python' 카테고리의 다른 글
Python 넘파이 (0) | 2023.10.02 |
---|---|
Python 클래스 & 패키지 (0) | 2023.10.02 |
Python 함수와 모듈 (0) | 2023.10.02 |
Python 제어문 (0) | 2023.10.02 |
Python 데이터 구조 (0) | 2023.10.02 |