본문 바로가기

XR, Extended Reality, AI

1일1프로그램, Study Python + review1

반응형

 

1일1프로그램, Study Python + review1

 

study

+review1

 

Python

 

Python, What is this?

https://www.python.org/

이미지 썸네일 삭제

Welcome to Python.org

The official home of the Python Programming Language

www.python.org

How to make a book?

What is your step?

How to approach?

 

아무래도 목차를 숙지하고 들어가는것이 이해하기 편하고,

전체구조를 그리기 쉽다.

 

김왼손의 유기농냠냠파이썬 세시간만에 끝내는 파이썬 (Python) 기초

 

1. 들어가기 전에

 1)유기농냠냠파이썬

 2)프로그램과 프로그래밍

 3)왜 파이썬일까?

 4)파이썬이 사용되는 곳

 5)파이썬 설치하기

 6)wrap-up.html

 

2. 프로그램의 기본 재료

 1)숫자형, 자료형

 2)더하기, 빼기, 곱하기, 나누기

 3)제곱, 몫, 나머지

 4)Wrap-up

 5)변수

 6)복합 할당 연산자

 7)변수 이름 짓기

 8)Wrap_up

 9)문자열 자료형

 10)문자열 연산하기

 11)문자 가져오기

 12)뒤에서부터 가져오기

 13)여러 개의 문자 가져오기

 14)숫자 생략하기

 15)문자열 분리하기

 16)문자열 포맷팅

 17)여러줄 문자열

 18)출력의 끝 지정하기

 19)이스케이프 코드

 20)C 스타일 포맷팅

 21)wrap_up

 22)주석

 23)wrap_up

 

3. 재료 모으기

 1)재료를 모으는 방법

 2)리스트 자료형

 3)리스트에 값 추가하기

 4)값에 접근하기

 5)값 제거하기

 6)여러 값 가져오기

 7)값 정렬하기

 8)값의 개수 세기

 9)in과 not in

 10)wrap_up

 11)튜플 자료형

 12)패킹과 언패킹

 13)wrap_up

 

4. 횟수로 반복하기

 1)for 문

 https://docs.python.org/3/tutorial/controlflow.html#for-statements

 4. More Control Flow Tools — Python 3.8.4rc1 documentation

 4. More Control Flow Tools Besides the while statement just introduced, Python uses the usual flow control statements known from other languages, with some twists. 4.1. if Statements Perhaps the most well-known statement type is the if statement. For example: >>> >>> x = int ( input ( "Please enter...

docs.python.org

 

 2)IDLE 에디터 모드

 3)문자열 반복하기

 4)들여쓰기

 5)range(stop)

 6)range(start, stop)

 7)for 중첩하기

 8)리스트 컴퓨리헨션

 9)wrap_up

 

5. 조건 판단하기

 1)논리 자료형

 2)비교 연산자

 3)논리 연산자

 4)논리 연산자를 사용하는 이유

 5)Wrap_up

 6)if 문

 7)else 와 elif

 8)Wrap_up

 

6. 조건으로 반복하기

 1) while 문

 2) pythontutor.com

 3) 넘어가기

 4) 멈추기

 5) wrap-up

 6) 입력받기

 7) 자료형 확인하기

 8) 자료형 바꾸기

 9) wrap-up

 

7. 재료 모으기 II

 1)딕션어리

 2)값 추가하기

 3)값 접근하기 제거하기

 4)딕션어리 메소드

 5)wrap_up

 

8. 모아서 다시 쓰기

 1)함수

 2)함수 만들기

 3)함수의 종류

 4)함수를 사용하는 이유

 5)여러 개 돌려주기

 6)독스트링

 7)wrap_up

 8)모듈

 9)random

 10)wrap_up

 

9. 더 알아보기

 1)객체

 2)wrap_up

 

 

 

1.들어가기 전에

 1)유기농냠냠파이썬

 김왼손, LG전자 

 처음 배우는 사람들

 

2)프로그램과 프로그래밍

프로그래밍 - 프로그램 만드는 작업

프로그램 - 어떤 문제해결을 위한 프로그램, 컴퓨터가 실행할 명령

 

순차

선택

반복

 

3)왜 파이썬일까요?

쉽지만 쉽지않다. 초심자에게

 

4)파이썬이 사용되는곳?

교육용,

구글 3대 언어, 구글입사 후

유튜브

나사

데이터 분석, 시각화, 라이브러리

게임

 

5)Python 설치하기 (홈페이지 조사하기)

www.python.org  

 

Welcome to Python.org

The official home of the Python Programming Language

www.python.org

 

Welcome to Python.org

The official home of the Python Programming Language

https://docs.python.org/release/3.8.3/

 

3.8.3 Documentation

Python 3.8.3 documentation Welcome! This is the documentation for Python 3.8.3. Parts of the documentation: What's new in Python 3.8? or all "What's new" documents since 2.0 Tutorial start here Library Reference keep this under your pillow Language Referen

docs.python.org

3.8.3 Documentation

Python 3.8.3 documentation Welcome! This is the documentation for Python 3.8.3. Parts of the documentation: What's new in Python 3.8? or all "What's new" documents since 2.0 Tutorial start here Library Reference keep this under your pillow Language Reference describes syntax and language elements Py...

docs.python.org

 

The Python Tutorial — Python 3.8.3 documentation

The Python Tutorial Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language f...

docs.python.org

 

 

2.프로그램의 기본재료

1)숫자형 자료형

 

print(1)

print(-1)

>>> print(1)

1

>>> print(-1)

-1

>>> print(3.14)

print(value,

 

2) 더하기, 빼기, 곱하기, 나누기

print (3+5) #plus,

print (3-5) #minus,

print (3 * 5) #multiplication, multiply

print (3 / 5) #subtract, division

 

3)제곱, 나머지, 몫 (연산을 사용하기 위한 도구)

print (2 ** 2) #square, 제곱

print (6 // 3) #share, 몫

print (6 //3) #remainder, 나머지

 

4) warp-up

 

 

5) 변수, Variable, 값을 잠시 저장하는 공간

변수는 왜 쓰게 되는가?

왜 변수를 만들어서 쓰는가?

왜 변수값을 만들어서 다시 사용할수 있도록 하는가?

 

연산자?

= #오른쪽의 값을 변수에 저장한다.

 

my_int = 1 #왼쪽의 지정한 변수를 =을 기준으로 오른쪽에 1을 저장, 할당한다.

my_str = 'Python' #문자열

my_bool = True #논리형 재료

my_list = [1, 2, 3] #리스트, 여러개 값을 한데 모아서 정리

 

왜 이렇게 변수에 할당하여 저장을 하는가?

 

6.복합할당 연산자, #연산을 하면서 할당을 같이한다?

a = a + b

=>a += b #이렇게 표현한다. 룰이다. 변수를 복합으로 쓰다보니 생기는 문제인가?

어떻때 주로 사용하는가? 반복할때마다 +1 증가하도록 할때

i = i + 1 # 1이 계속 + 되는것을 표현

=> i + = 1 #간단하게 표현, 단축하여 쓴다.+1씩 증가되는것을 표현

 

+=

-=

*=

/=

 

count = 0 #

count += 1 #count 라는 변수가 1를 더해줌

count -= -1 #count 라는 변수가 -1를 빼줌

count *= 2 #count 라는 변수에 2를 곱해줌

 

왜 이렇게 하는건가?

count = 0

count += 1

count -= -1

count *= 2

print(count)

얼마가 나오는가?

 

count = 0

count += 1

count -= 2

count *= 3

print(count) ?

 

>>> count = 0

>>> count += 1

>>> count -= 2

>>> count *= 3

>>> print(count)

-3

이렇게 해서 언제 적용해 볼수 있는것인가?

 

7) 변수 이름 짓기 # 아무이름이나 지어도 상관없다. 당연하지 변수인데..._ 언더바를 사용, 숫자를 사용x, 빈칸x, 대소문자 구분, 값을 설명하기 좋은 이름, 다른 사람이 내 코드를 볼때 이해하기 쉽게, 추후에 내가 내 코딩을 볼때 기억하기 쉽게.

 

8) wrap up

 

9) 문자열 자료형 #문자열, string?

"문자열",'문자열'

 

>>> my_str1 = 'a' # 작은, 큰 따옴표 안에 들어가면 문자열로 인식

>>> my_str2 = '3.14'

>>> my_str3 = 'coding'

>>> my_str4 = "coding"

>>> print(my_str1)

a

>>> print(my_str2)

3.14

>>> print(my_str3)

coding

>>> print(my_str4)

coding

>>> print (type(my_str2)) # Type이 class str 문자열로 인식됨을 확인

<class 'str'>

>>> print(my_str3, my_str4) # ( , ) 컴마를 쓰면 여러가지 한꺼번에 프린트

coding coding

 

 

10) 문자열 연산하기 #숫자 뿐 아니라 문자도 연산이 가능? +, * 가능, 연결하는 기능

 

>>> my_str1 = 'a' + 'b' + 'c'

>>> my_str2 = 'cod' + 'ing'

>>> my_str3 = 'abc' * 4

>>> my_str4 = '=' * 10

>>> print(my_str1)

abc

>>> print(my_str2)

coding

>>> print(my_str3)

abcabcabcabc

>>> print(my_str4)

=============

 

왜 이렇게 해야 하는것인가?

 

 

11) 문자 가져오기

 

인덱싱, 문자의 위치?

문자의 열마다 위치 숫자를 가진다.

0 1 2 3 4 5 -> 인덱스

a b c d e f

 

>>> alphabet = 'abcde'

>>> print(alphabet[0]) #첫번째 문자 가져와

a

>>> print(alphabet[1]) #두번째 문자 가져와

b

>>> print(alphabet[2]) #세번째 문자 가져와

c

>>> print(alphabet[3]) #네번째 문자 가져와

d

>>> print(alphabet[4]) #다섯번째 문자 가져와

e

>>> print(alphabet[5]) #여섯번째 문자 가져와, 없는데...인덱스 에러

Traceback (most recent call last):

File "<pyshell#37>", line 1, in <module>

print(alphabet[5])

IndexError: string index out of range

 

 

12) 뒤에서부터 문자가지고 오기

-6 -5 -4 -3 -2 -1

P y t h o n

 

>>> print(alphabet[-1]) #뒤 첫번째에서 부터 가져와

e

>>> print(alphabet[-2]) #뒤 두번째에서 부터 가져와

d

>>> print(alphabet[-3])

c

>>> print(alphabet[-4])

b

>>> print(alphabet[-5])

a

>>> print(alphabet[-6]) #뒤 여번째에서 부터 가져와, 없잖아 에러.

Traceback (most recent call last):

File "<pyshell#43>", line 1, in <module>

print(alphabet[-6])

IndexError: string index out of range

 

 

13) 여러 개의 문자 가져오기

슬라이싱,

여러개의 문자를 잘라서 가지고 오다.

[1:4] #2부터 5까지 가지고 오라

 

0 1 2 3 4 5

P y t h o n

 

>>> my_str = "Hello Python!'

SyntaxError: EOL while scanning string literal #변수 따옴표 맞춰라.

>>> my_str = "Hello Python!"

>>>

>>> print(my_str[0:1]) #처음(0)부터 인덱스 그전까지 가지고 와라.

H

>>> print(my_str[0:2])

He

>>> print(my_str[0:3]) #처음(0)부터 2 그전까지 가지고 와라.

Hel

>>> print(my_str[0:4])

Hell

>>> print(my_str[0:5])

Hello

>>> print(my_str[0:6]) #처음(0)부터 공백 그전까지 가지고 와라. 공백도 포함

Hello

>>> print(my_str[0:7])

Hello P

>>> print(my_str[0:8])

Hello Py

>>> print(my_str[0:9])

Hello Pyt

>>> print(my_str[0:10])

Hello Pyth

>>> print(my_str[0:11])

Hello Pytho

>>> print(my_str[0:12])

Hello Python

>>> print(my_str[0:13])

Hello Python!

>>> print(my_str[0:14])

Hello Python!

>>> print(my_str[0:15])

Hello Python!

 

>>> print(my_str[3:7]) #3(0부터 0,1,2)부터 7(0,1,2,3,4,5,6) 그전까지 가지고 와라.

lo P

 

 

14) 숫자 생략하기

[:3] #앞에 숫자 생략하고 3까지 가져와라

[2:] #2부터 뒤에 다 가지고 와라

 

>>> print(my_str[:5]) #5번째까지 가지고 와라, 좋은말로 할때.

Hello

 

>>> my_str = "Hello Python!"

>>> print(my_str[6:]) #6부터 이후로 다 가지고 와라.

Python!

 

 

15) 문자열 분리하기

메소드? 특정 영역에서 사용할수 있는 함수

.split()

 

>>> fruit_str = 'apple banana lemon'

>>> fruits = fruit_str.split() #변수하고 .split() 입력

 

>>> print(fruits)

['apple', 'banana', 'lemon'] #공백을 기준을 열이 나눠서 나옴

 

>>> print(fruits[0]) #1열 소환

apple

>>> print(fruits[1]) #2열 소환

banana

>>> print(fruits[2]) #3열 소환

lemon

>>> print(fruits[3]) #4열 소환, 없잖아 그런거.

Traceback (most recent call last):

File "<pyshell#74>", line 1, in <module>

print(fruits[3])

IndexError: list index out of range

 

 

16) 문자열 포맷팅 .format()

format?

 

the way in which something is arranged or set out.

the format of the funeral service

 

(especially in computing) arrange or put into a format.

 

 

.format()

#자유롭게 표현? 문자열

 

>>> print('Life is {}'.format('Short!')) #.format의 문자가 {}안으로 들어가도록

Life is Short!

 

왜 이렇게 하는걸까??

 

>>> print('{} x {} = {} .format(2, 3, 2 * 3)) #' 하나라도 빼먹으면 에러

SyntaxError: invalid syntax

 

>>> print( '{} x {} = {} ' .format(2, 3, 2*3))

2 x 3 = 6

 

 

17) 여러줄 문자열

'''문자열''', # 작은 따옴표 왜 세개나 쓰는거야?

 

>>> print('첫 번째

SyntaxError: EOL while scanning string literal #작은따옴표는 하나의 문자열로도 표현안됨

 

>>> print('''첫 번째

두 번째

세 번째''') #여러줄 문자열 표현가능

첫 번째

두 번째

세 번째

 

>>> print("""1

2

3

4

5""") #큰따옴표 3개로도 여러문자열 가능

1

2

3

4

5

 

 

18) 출력의 끝 지정하기

print('', end='') #출력의 끝을 지정가능

 

에스케이프 코드

print('coding', end='')

print('coding', end='-')

print('coding', end='\n') #\n 줄바꿈

print('coding', end='\t') #\t 탭

 

잘안됨....

 

 

19) 이스케이프 코드 #특정한 기능을 수행, 문자열에 포함해서

\n #줄바꿈

\t #수평탭

 

원화표시가 백슬래쉬

 

>>> print('줄바꿈\n줄바꿈')

줄바꿈

줄바꿈

 

 

>>> Print('탭\t탭') #대소문자 가린다.

Traceback (most recent call last):

File "<pyshell#103>", line 1, in <module>

Print('탭\t탭')

 

 

NameError: name 'Print' is not defined

>>> print('탭\t탭')

탭 탭

 

파이썬 이스케이브 코드로 구글에서 검색해보라

 

>>> print('\\n') #백슬래쉬, 백슬래쉬 두개 쓴다. \\

\n

>>> print('\\t')

\t

 

안나오는데...??

 

 

>>> print('I'm yours

 

SyntaxError: invalid syntax #' ' 사이가 문자로 인식 되어 에러

>>>

>>> print('I\'m yours') # 백스페이스 넣으면 해결

I'm yours

 

 

20) C 스타일 포맷팅

연산자

%d, #정수

%f, #실수

%s, #문자열

 

>>> print('LIfe is %s' % 'Short!') #%로 short!로 대입되어 들어감

LIfe is Short!

 

>>> print('%d x %d = %d' % (2, 3, 2*3)) #%d 정수에 %로 지정한 내용이 대치된다.

2 x 3 = 6

 

 

21) wrap-up

 

 

22) 주석

comment

설명을 메모

#, 설명을 메모, 컴터는 볼수 없다.

 

>>> print('안녕!') #문자열을 출력

안녕!

>>> print(123456) #숫자를 출력

123456

>>> print(2 +3) #계산결과를 출력

5

 

 

3.재료 모으기

1)재료를 모으는 방법

 

2) 리스트 자료형

 

[요소1, 요소2, ...]

 

>>> my_list1 = []

SyntaxError: invalid character in identifier #복사해서 쓰니 에러가 생김

 

>>> my_list1 = []

>>> my_list2 = [1, 2, 3]

>>> my_list3 = ['a' , 'b']

 

 

3) 리스트에 값 추가하기

.append()

#리스트에서 만 사용할수, 추가할수 있는 함수, 뒤로 추가됨

>>> my_list = []

>>> my_list .append(123)

>>> my_list .append('abc')

>>> my_list .append(True)

>>> print(my_list)

[123, 'abc', True]

 

 

4) 값에 접근하기

인덱싱

 

0 1 2 (인덱스)

123 abc True (요소)

 

>>> print(my_list[0])

123

>>> print(my_list[1])

abc

>>> print(my_list[-1])

True

 

>>> my_list[0] = 3.14 #기존의 값을 대치할수 있다.

>>> my_list[1] = 'ABC'

>>> my_list[2] = False

>>> print(my_list)

[3.14, 'ABC', False]

 

 

5)값 제거하기

del

#이미 지정된 변수라서 이름으로 지정할수가 없다.

 

>>> print(my_list)

[3.14, 'ABC', False]

>>> del my_list[0]

>>> del my_list[1]

>>> del my_list[2]

Traceback (most recent call last):

File "<pyshell#150>", line 1, in <module>

del my_list[2]

IndexError: list assignment index out of range

>>> print(my_list)

['ABC']

 

 

>>> my_list = [123, 'abc', True]

>>> del my_list[0]

>>> print(my_list)

['abc', True]

 

 

>>> my_list = [123, 'abc', True]

>>> del my_list[0]

>>> print(my_list)

['abc', True]

>>> del my_list[1]

>>> print(my_list)

['abc']

 

 

6) 여러가지 값 가져오기

슬라이싱

 

여러가지 요소를 잘라올수 있다.

 

0 1 2

123 'abc' True [0:2]

 

>>> my_list = ['a', 'b', 'c']

>>> print(my_list[:1])

['a']

>>> print(my_list[1:3])

['b', 'c']

>>> print(my_list[2:])

['c']

 

 

7) 값 정렬하기

.sort()

#리스트 안의 요소를 정렬

 

>>> my_list = [3, 2, 4, 1]

>>> my_list.sort()

>>> print(my_list)

[1, 2, 3, 4]

 

 

8) 값의 개수 세기

.count()

#갯수 세기 당연히...

 

>>> my_list = ['a', 'c', 'a', 'b']

>>> print(my_list.count('a'))

2

>>> print(my_list.count('b'))

1

>>> print(my_list.count('c'))

1

 

#이걸 어디다 적용해 볼까?

 

 

9) in 과 not in

>>> my_list = ['a', 'b', 'c', 'd']

>>> print('a' in my_list) #리스트 안에 'a'가 있냐?

True

>>> print('f' not in my_list) #리스트 안에 'f'가 없냐?

True

>>> print('e' in my_list)

False

 

 

10)wrap_up

 

 

11) 튜플 자료형

튜플

#리스트처럼 값을 바꿀수 없다 (imutable) emutable?

 

(요소1, 요소2, ....

 

 

>>> my_tuple1 = ()

>>> my_tuple2 = (1,) # (,)가 tuple로 인식

>>> my_tuple3 = ('a', 'b', 'c')

>>> my_tuple4 = 3.14, 'Python', True

>>> print(my_tuple)

 

>>> print(type(my_tuple2))

<class 'tuple'>

>>> my_tuple1 = (1) #(,) 없으면 int 정수로 인식

>>> print(type(my_tuple1))

<class 'int'>

>>> print(my_tuple4)

(3.14, 'Python', True)

>>> print(type(my_tuple4))

<class 'tuple'>

 

 

12) 패킹과 언패킹

패킹/언패킹

>>> my_tuple = 3.14, 'Python', False

>>> i, s, b = (123, 'abc', True)

>>> print(my_tuple)

(3.14, 'Python', False)

>>> print(i, s, b)

123 abc True

 

 

 

반응형