Python

[Python][TIL] 기본문법

breadz 2021. 7. 13. 12:50

타입 확인하기

type(3)
Out[1]: int

type(3.14)
Out[2]: float

type(1+3j)
Out[3]: complex

type(True)
Out[4]: bool

 

객체 생성

※ 영어, underbar, 숫자 가능. 변수 이름의 첫 글자에 숫자는 불가능하다

print('python is so fun')

feel = 'python is so fun'

print(feel)

 

 

모든 변수 지우기

reset -sf

변수 b만 지우기

del b

console I/A 부분 지우기

clear

구간 나누기

#%%

 

주석처리

ctrl + 1

 

연산자

1) 산술 연산자

a = 7
b = 3

print(a+b, a-b, a*b, a/b, a//b, a%b, a**b)

divmod(7, 3)

div, remainder = divmod(7, 3)
print(div, remainder)

 

2) 할당 연산자

num1 = num2 = num3 = 5000
num4, num5 = 300, 500
num6, num7 = 300

string = 'hello'

 

3) 복합 할당 연산

+= 		i+=100		i = i+100
-= 		i-=100 		i = i-100
*= 		i*=100 		i = i*100
/= 		i/=100 		i = i/100
//= 		i//=100 	i = i//100
%= 		i%=100 		i = i%100
**= 		i**=100 	i = i**10

 

4) 비교 연산자

a,b = 100, 200
a == b
a != b
a > b
a >= b

 

5) 논리 연산자

a = 150
(a > 100) and (a < 200)

 

동전 교환하기

money, c500, c100, c50, c10 = 0, 0, 0, 0, 0

money = int(input("교환할 돈은 얼마에요? : "))

c500 = money // 500
money = money % 500
print("500원짜리 ==> %2d개" % c500)

c100 = money //100
money = money % 100
print("100원짜리 ==> %2d개" % c100)

c50 = money // 50
money = money % 50
print("50원짜리 ==> %2d개" % c50)

c10 = money //10
money = money % 10
print("10원짜리 ==> %2d개" % c10)

c1 = money //1
money = money % 1
print("1원짜리 ==> %2d개" % c1)

 

 

구구단

for item in range(2,20):
    print(' ')
    print(str(item)+' 단을 시작합니다')
    for each in range(1,20):
        print(item,'*',each,'=',item*each)

 

문자열 생성 방법

"Hello World"

'Python is fun'

"""Life is too short, You need python"""

'''Life is too short, You need python'''
Let1 = "kim's favorite food is apple"
print(Let1)

Let2 = '"Python is very easy." he says.
print(Let2)

Let1_1 = 'Python\'s favorite food is perl'
Let2_1 = "\"Python is very easy.\" he says."

mulL1 = "Life is too short\nYou need Python"
print(mulL1)

mulL2 = """
Life is too short
You need Python
"""
print(mulL2)

 

문자열 연결연산

head = "Python"
tail = " is fun!"

head + tail

head * 5
print("=" * 50)
print('My Program')
print("=" * 50)

 

문자열 길이

a = "Life is too short"
len(a)

 

 

indexing : 문자열의 특정 위치를 의미합니다

eStr_e1 = "Life is too short, You need Python"
len(Str_e1)

#Life is too short, You need Python
#0         1         2         3
#0123456789012345678901234567890123

Str_e1[3]
Str_e1[-1]

 

slicing : 전체 문자열 중 일부만 추출을 수행해줍니다.

Str_e2 = Str_e1[0] + Str_e1[1] + Str_e1[2] + Str_e1[3]

a = "Life is too short, You nee Python"

sel_a = a[0:4]
# 0 <= a < 4

 

test = 'pithon'

test[1]
new_test = test[:1]+'y'+test[2:]

print(new_test)

 

문자열 포매팅

숫자를 바로 입력하는 경우

print('i eat 5 apples.')

print('i eat %d apples.' %10)
print('i eat %d apples.' %20)

 

문자를 입력하는 경우 -> %s

print('i eat %s apples.' %'pin')

 

숫자를 변수로 입력하는 경우

num = 5
print('i eat %d apples' %num)

 

두 개 이상의 문자열 포매팅 -> 괄호로 묶어주기

number = 21
day = "five"
print("I ate %d apples. so I was sick for %s days." %(number, day))

 

%s 로 숫자도 입력할 수 있다

number = 21
day = "five"
print("I ate %s apples. so I was sick for %s days." %(number, day))