정수형 이란 정수를 뜻하는 자료형을 말한다.
a = 123
a = -178
a = 0
실수형은 소수점이 포함된 숫자를 말한다.
a = 1.2
a = -3.45
a = 3
b = 4
a + b
7
a - b
-1
a * b
12
a / b
0.75
a = 3
b = 4
a ** b
81
7 % 3
1
3 % 7
3
7 / 4
1.75
7 // 4
1
"Hello world"
'Python is fun'
"""Life is too short, You need python"""
'''Life is too short, You need python'''
Python's favorite food is perl
food = "Python's favorite food is perl"
print(food)
"Python's favorite food is perl"
food = 'Python's favorite food is perl'
File "<stdin>", line 1
food = 'Python's favorite food is perl'
^
SyntaxError: invalid syntax
"Python is very easy." he says.
say = '"Python is very easy." he says.'
print(say)
"Python is very easy." he says.
food = 'Python\'s favorite food is perl'
say = "\"Python is very easy.\" he says."
Life is too short
You need python
multiline = "Life is too short\nYou need python"
multiline ='''
life is too short
You need python
'''
multiline ="""
life is too short
You need python
"""
print(multiline)
Life is too short
You need python
head = "Python"
tail = " is fun!"
head + tail
'Python is fun!'
a = "python"
a * 2
'pythonpython'
a = "Life is too short"
len(a)
17
a = "Life is too short, You need Python"

a = "Life is too short, You need Python"
a[3]
'e'
a[0]:'L', a[1]:'i', a[2]:'f', a[3]:'e', a[4]:' ', ...
a = "Life is too short, You need Python"
a[0]
'L'
a[12]
's'
a[-1]
'n'
a[-0]
'L'
a[-2]
'o'
a[-5]
'y'
a = "Life is too short, You need Python"
b = a[0] + a[1] + a[2] + a[3]
b
'Life'
a = "Life is too short, You need Python"
a[0:4]
'Life'
하지만 ‘a[0]은 L, a[1]은 i, a[2]는 f, a[3]은 e이므로 a[0:3]으로도 Life라는 단어를 뽑아 낼 수 있지 않을까?’라는 의문이 생길 것이다. 다음 예로 확인해 보자.
a[0:3]
'Lif'
0 <= a < 3
a[0:5]
'Life '
a[0:2]
'Li'
a[5:7]
'is'
a[12:17]
'short'
a[19:]
'You need Python'
a[:17]
'Life is too short'
a[:]
'Life is too short, You need Python'
a[19:-7]
'You need
a = "20230331Rainy"
date = a[:8]
weather = a[8:]
date
'20230331'
weather
'Rainy'
a = "20230331Rainy"
year = a[:4]
day = a[4:8]
weather = a[8:]
year
'2023'
day
'0331'
weather
'Rainy'
"현재 온도는 18도입니다."
"현재 온도는 20도입니다"
"I eat %d apples." % 3
'I eat 3 apples.'
"I eat %s apples." % "five"
'I eat five apples.'
number = 3
"I eat %d apples." % number
'I eat 3 apples.'
number = 10
day = "three"
"I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10 apples. so I was sick for three days.'

"I have %s apples" % 3
'I have 3 apples'
"rate is %s" % 3.234
'rate is 3.234
"I eat {0} apples".format(3)
'I eat 3 apples'
"I eat {0} apples".format("five")
'I eat five apples'
number = 3
"I eat {0} apples".format(number)
'I eat 3 apples'
number = 10
day = "three"
"I ate {0} apples. so I was sick for {1} days.".format(number, day)
'I ate 10 apples. so I was sick for three days.'
"I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3)
'I ate 10 apples. so I was sick for 3 days.'
"I ate {0} apples. so I was sick for {day} days.".format(10, day=3)
'I ate 10 apples. so I was sick for 3 days.
name = '홍길동'
age = 30
f'나의 이름은 {name}입니다. 나이는 {age}입니다.'
'나의 이름은 홍길동입니다. 나이는 30입니다.'
age = 30
>>> f'나는 내년이면 {age + 1}살이 된다.'
'나는 내년이면 31살이 된다.
d = {'name':'홍길동', 'age':30}
f'나의 이름은 {d["name"]}입니다. 나이는 {d["age"]}입니다.'
'나의 이름은 홍길동입니다. 나이는 30입니다.'
y = 3.42134234
f'{y:0.4f}' # 소수점 4자리까지만 표현
'3.4213'
f'{y:10.4f}' # 소수점 4자리까지 표현하고 총 자리수를 10으로 맞춤
' 3.4213'
f'{{ and }}'
'{ and }'
a = "hobby"
a.count('b')
2
a = "Python is the best choice"
a.find('b')
14
a.find('k')
-1
a = "Life is too short"
a.index('t')
8
a.index('k')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
find 함수는 문자열 안에 포함되지 않는 문자를 찾으면 -1을 반환한다. index함수는 문자열 안에 존재하지 않는 문자를 찾으면 오류가 발생한다는 것이다
",".join('abcd')
'a,b,c,d'
join 함수는 문자열뿐만 아니라 앞으로 배울 리스트나 튜플도 입력으로 사용할 수 있다(리스트와 튜플은 곧 배울 내용이므로 여기에서는 잠시 눈으로만 살펴보자). join 함수의 입력으로 리스트를 사용하는 예는 다음과 같다.
",".join(['a', 'b', 'c', 'd'])
'a,b,c,d'
a = "Life is too short"
a.replace("Life", "Your leg")
'Your leg is too short'
a = "Life is too short"
a.split()
['Life', 'is', 'too', 'short']
b = "a:b:c:d"
b.split(':')
['a', 'b', 'c', 'd']