[빅데이터 스터디 1주차] 3. Python 001

이도연·2021년 9월 5일
0

환경 -> Goormide 에서 Jupyter notebook으로 시작

Goormide를 시작하려면?

Goormide 접속 후 새 컨테이너를 생성하고 Jupyter notebook을 선택해준다.
창이 뜨면 [프로젝트 -> 실행 -> URL 클릭] 한다.


📂 함수 설명

int : 정수 (ex) 0b1001 #2진수
0o1001 #8진수
0x1001 #16진수
float : 실수
str : 문자열

+) or 연산자 |는 Shift + W 누르면 된다.


1. type 보는 법

a = 10
print(type(a))

👉 결과
<class 'int'>

2. 순서 보는 법

s = 'paullab CEO LeeHoJun'
print(s[0])         #s의 0번째는 p이다. 항상 0부터 시작한다.
print(s[0:7])       #0부터 6번째까지 출력해라.
print(s[0:15:2])    #0부터 15까지 2만큼 건너뛰면서 출력해라.
print(s[10:0:-1])   #10에서 1까지 -1만큼 건너뛰면서 출력해라.
print(s[:10:2])     #처음부터 10까지 2만큼 건너뛰면서 출력해라.
print(s[::2])       #처음부터 마지막까지 2만큼 건너뛰면서 출력해라.
print(s[::-1])      #처음부터 마지막까지 -1만큼 건너뛰면서 출력해라.
print(s[-1])        #마지막 값

👉 결과
p
paullab
pulbCOLe
OEC ballua
pulbC
pulbCOLeou
nuJoHeeL OEC balluap
n

2. dir()

print(type())print(dir()) 은 항상 세트로 가져가야한다.

s = 'paullab CEO LeeHoJun'
print(type(s))
print(dir(s))

👉 결과
<class 'str'>
['add', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

dir() 내장 함수는 어떤 객체를 인자로 넣어주면 해당 객체가 어떤 변수와 메소드(method)를 가지고 있는지 나열해준다.

3. 대문자/소문자 변형, count()

s = 'paullab CEO LeeHoJun' 
print(s.upper())       #대문자 변형
print(s.lower())       #소문자 변형
print(s.count('l'))    #count() 는 개수를 세준다.

👉 결과
PAULLAB CEO LEEHOJUN
paullab ceo leehojun
2

4. strip(), split()

ss = '      hello world     '
print(ss.strip())       #공백을 없애준다.
print(s.split(' ') )    #공백단위로 분리한다.

👉 결과
hello world
['paullab', 'CEO', 'LeeHoJun']

5. join()

a = s.split(' ')
print(a)
print('!'.join(a))        #!로 합친 a를 보여준다.
print('제 이름은 {}입니다. 제 나이는 {}입니다.'.format('이호준', 33))    #순서대로 매치가 된다.

👉 결과
['paullab', 'CEO', 'LeeHoJun']
paullab!CEO!LeeHoJun
제 이름은 이호준입니다. 제 나이는 33입니다.

6. spe, end

a = 2019
b = 9
c = 24
# 2019/9/24
print(a, b, c, sep='/', end=' ')
print(a, b, c)

👉 결과
2019/9/24 2019 9 24

print() 는 ',' 로 연결이 가능하다.
sep='/' 각각의 요소들을 /로 나누겠다.
end=' ' 는 끝나는 지점을 엔터가 아닌 스페이스로 주겠다.

7. 형변환

a = 10
b = '10'
print(a + int(b))     #b를 정수형으로 변환시켰다.
print(str(a) + b)     #a를 문자열로 변환시켰다.

👉 결과
20
1010

8. True, False

a = True
b = False
print(type(a))
# print(dir(a))

👉 결과
<class 'bool'>

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

print(bool(' '))    #스페이스가 들어있어서 True
print(bool(''))     #공백(0)이라서 False
print(bool(0))
print(bool(1))
print(bool(None))

👉 결과
True
False
False
True
False

9. 산술연산

a = 3
b = 10
print(a + b)
print(a - b)
print(b / a)    #float(t실수)
print(b // a)   #int(정수)  #몫
print(b * a)
print(b ** a)   #제곱수
print(b % a)    #나머지

👉 결과
13
-7
3.3333333333333335
3
30
1000
1

10. 비교연산

a = 10
b = 3
print(a >= b)
print(a > b)
print(a < b)
print(a <= b)
print(a == b)   #같다
print(a != b)   #다르다

👉 결과
True
True
False
False
False
True

11. 논리연산

a = True        #1
b = False       #0
print(a and b)  # and=*
print(a or b)   # or=+
print(not b)    # not=반대

👉 결과
False
True
True

12. 할당연산

a = 10
a = a + 10    #누적된다고 생각하면 쉽다.
a += 10
print(a)

👉 결과
30

13. bit연산

a = 40
b = 14
print(a & b)
# &, |, ~
print(bin(a)[2:].zfill(6))   #bin(a) 는 이진수이다.
print(bin(b)[2:].zfill(6))   #zfill(6) 6자리 수
# 101000
# 001110
# ------- and 연산
# 001000

👉 결과
8
101000
001110

14. def return

def f(x,y):
    z = x + y    #z 앞은 Tab이 아니라 Space 4번이다.
    return z
print(f(3,5))

👉 결과
8

def ff():
    print('1')
    print('2')
    return None
print('3')
print(4)
ff()

👉 결과
3
4
1
2

def circle(r):
    width = r*r*3.14
    return width
print(circle(10))

👉 결과
314.0

a = 10
def aplus():
    global a
    a += 10
    return a
print(aplus())

👉 결과
20

0개의 댓글