파이썬 1일차 - 포맷, 인데싱, 슬라이싱 (DMCCONET)

김회곤·2022년 6월 22일

파이썬(DMCCONET)

목록 보기
5/8

문자의 format

숫자를 넣고 0번째 대입

format01="I eat {0} apples".format(3)
print(format01)

문자를 넣고 0번째 대입

format01="I eat {0} apples".format("five")
print(format01)

변수를 선언후 변수를 넣기

number = 3
format01="I eat {0} apples".format(number)
print(format01)

숫자변수선언과 문자 변수선언후 포맷

number=10
day="three"

format01="I ate {0} apples. so I was sick for {1} days.".format(number, day)
print(format01)

따로 포맷안에 숫자부여

format01="I ate {number} apples. so I was sick for {day} days.".format(number=3, day=10 )
print(format01)

따로 포맷안에 숫자와 문자 부여

앞에 관호에 인댁스 순서를 넣거나, 변수를 직접넣어서 문장을 송출

format01="I ate {0} apples. so I was sick for {day} days.".format(3, day=10 )
print(format01)

포맷 % 정의

%s 문자열(String) -> 문자든 숫자든 상관없음
%c 문자 1개(character)
%d 정수(Integer)
%f 부동소수(floating-point)**

print("I have %d. apples" % 3)

I have 3. apples

%d 안에 3이라는 숫자가 대입

format01 = "Error is %d%.%"
print(format01)

Error is 98

%d라는 숫자에 98대입

format01 = "%0.4f" % 3.42134234
print(format01)

3.4213

0칸 정도 띄어서 소수점 4자리만큼만 송출

format01 = "%10.4f" % 3.42134234
print(format01)

3.4213

10정도의 칸만큼 띄어서 소수점 4번째 짜리까지만 송출

indexing

인덱싱(Indexing)이란 무엇인가를 "가리킨다"는 의미이고

a="niceday"
b="0123456"

print(a[0:3]) # Nic
print(a[:3]) # Nic
print(a[4:]) # day
print(a[:]) # NiceDay

print(a[4:-2]) # D

print(a[2:4]) # D

slicing

슬라이싱(Slicing)은 무엇인가를 "잘라낸다"는 의미이다

튜플데이터 뽑아내기

ex1)

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(3, 5)
print(a[x])

('d', 'e')

a라는 튜플데이터안에 3, 4번째를 인풋 해라

ex2)

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(0, 6, 3)
print(a[x])

('a', 'd')

a라는 튜플데이터안에 0 ~ 6까지 범위중 6은 포함하지않고 0송출하고 3칸띄어서 송출하고

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(0, 8, 3)
print(a[x])

('a', 'd','g')

a라는 튜플데이터안에 0 ~ 8까지 범위중 6은 포함하지않고 0송출하고 3칸띄어서 송출하고 위에 식보면 다른점을 알수있다

python_ex_03.py

print("niceDay")
print('4+5')
print("""
niceday
happyday
""")

print("="*30)
str="Nice Day"
print(str)
print(len(str))

python_ex_04.py

text = "We are the so-called \"Vikings\" from the north.";
print(text)

text = "We are \n the so-called \"Vikings\" from the north.";
print(text)

text = "We are \tthe so-called \"Vikings\" from the north.";
print(text)

python_ex_05.py

a="niceday"
b="0123456"

print(a[0]) # D
print(a[2]) # D
print(a[4]) # D
print(a[6]) # D

python_ex_06.py >> 슬라이싱(Slincilg)

인덱싱(Indexing)이란 무엇인가를 "가리킨다"는 의미이고,
슬라이싱(Slicing)은 무엇인가를 "잘라낸다"는 의미이다

a="niceday"
b="0123456"

print(a[0:3]) # Nic
print(a[:3]) # Nic
print(a[4:]) # day
print(a[:]) # NiceDay

print(a[4:-2]) # D

print(a[2:4]) # D

python_ex_07.py

>> 문자의 포매팅

"""
%s 문자열(String) -> 문자든 숫자든 상관없음
%c 문자 1개(character)
%d 정수(Integer)
%f 부동소수(floating-point)
"""

print("I have %d. apples" % 3) # D

format01 = "Error is %d%.%" % 98
print(format01)

format01 = "%0.4f" % 3.42134234
print(format01)

format01 = "%10.4f" % 3.42134234
print(format01)

python_ex_08.py

>> format함수 > 함수명 ()

format01="I eat {0} apples".format(3)
print(format01)

format01="I eat {0} apples".format("five")
print(format01)

number = 3
format01="I eat {0} apples".format(number)
print(format01)

number=10
day="three"

format01="I ate {0} apples. so I was sick for {1} days.".format(number, day)
print(format01)

format01="I ate {number} apples. so I was sick for {day} days.".format(number=3, day=10 )
print(format01)

format01="I ate {0} apples. so I was sick for {day} days.".format(3, day=10 )
print(format01)

python_ex_09.py

str = "☆\n☆☆\n☆☆☆\n☆☆☆☆\n☆☆☆☆☆"
print(str)

profile
전공 : 빅데이터통계학과, 부전공 : 인공지능 소프트웨어

0개의 댓글