๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
study๐Ÿ“š/python

[python/ํŒŒ์ด์ฌ] ๋ณ€์ˆ˜, ์ž๋ฃŒํ˜•( ์ˆซ์žํ˜•, ๋ฌธ์žํ˜•)

by ์Šค๋‹ 2022. 7. 14.

1. ํŒŒ์ด์ฌ์˜ ํŠน์ง•

  • ์„ธ๋ฏธ์ฝœ๋ก (;)์œผ๋กœ ๋์„ ๋งบ์ง€ ์•Š์•„๋„ ๋จ
print('Hello World!')

Hello World!

print('Hello World!');

Hello World!

  • ๋“ค์—ฌ์“ฐ๊ธฐ์— ์ฃผ์˜๊ฐ€ ํ•„์š”
1+1

2

            1+1

2

1+1
1+1

2

1+1
    1+1

  • ์ฃผ์„์€ #(ํ•œ ์ค„) ๋˜๋Š” ''',"""๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
# ํ•œ์ค„์งœ๋ฆฌ ์ฃผ์„์ž…๋‹ˆ๋‹ค.
'''
์ด๋ ‡๊ฒŒ
์—ฌ๋Ÿฌ์ค„์„
์ฃผ์„์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
'''
"""
    ์Œ ๋”ฐ์˜ดํ‘œ๋กœ๋„
ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
"""

'\n ์Œ ๋”ฐ์˜ดํ‘œ๋กœ๋„\nํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n'

2. ๋ณ€์ˆ˜

  • ๋ฌธ์ œ1
    a๋Š” 100, b๋Š” 10000์ด๋‹ค.
    a+b๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
100+10000

10100

  • ๋ฌธ์ œ2
    a๋Š” 100,b๋Š” 10000์ด๋‹ค.
    a*b๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
100*10000

1000000

  • ๋ฌธ์ œ3
    a๋Š” 100,b๋Š” 10000์ด๋‹ค.
    a/b๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
100/10000

0.01

์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋˜์ง€๋งŒ ๊ท€์ฐฎ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
๊ทธ๋ž˜์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ˆซ์ž๋“ค์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์—ฌ ์žฌ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด?

  • ๋ณ€์ˆ˜์— ์ƒ์„ฑํ•˜๋Š” ๋ฒ•1
a = 100
b = 10000
  • print ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ a,b์˜ ๊ฐ’ ํ™•์ธ
print(a)
print(b)

100
10000

  • ๋ณ€์ˆ˜ ์ƒ์„ฑํ•˜๋Š” ๋ฒ•2
a,b = 100, 10000
  • print ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ a,b์˜ ๊ฐ’ ํ™•์ธ
print(a)
print(b)

100
10000

  • ๋ฌธ์ œ4
    a๋Š” 300,b๋Š” 400์ด๋‹ค
    a+b,a-b,a*b,a/b๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
a = 300
b = 400

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

700
-100
120000
0.75

  • ๋ณ€์ˆ˜๋ช… ์ž‘์„ฑ์‹œ ์ฃผ์˜์‚ฌํ•ญ
    • ์˜๋ฌธ์ž,์ˆซ์ž,_์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•จ
a = 1
a1 = 1
_ = 1
  • ๋ณ€์ˆ˜๋ช…์˜ ์ฒซ ๊ธ€์ž๋Š” ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
1a = 1

  • ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋Š” ๊ตฌ๋ถ„๋˜์–ด ์‚ฌ์šฉ๋จ
a = 1
A = 10
print(a)
print(A)

1
10

  • ๋ณ€์ˆ˜๋ช… ์‚ฌ์ด์— ๋„์›Œ์“ฐ๊ธฐ๋Š” ๋ถˆ๊ฐ€
a b = 1

3. ์ž๋ฃŒํ˜•

  • ์ž๋ฃŒํ˜•(์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ)
a = 1
b = 0.1
c = 'hello'
print(type(a))
print(type(b))
print(type(c))

<class 'int'>
<class 'float'>
<class 'str'>

1) ์ˆซ์žํ˜•

  • int(์ •์ˆ˜์˜ ํ˜•ํƒœ)
a = 1
print(type(a))

<class 'int'>

b = 0
print(type(b))

<class 'int'>

c = -1
print(type(c))

<class 'int'>

  • float(์‹ค์ˆ˜์˜ ํ˜•ํƒœ)
a = 1.1
print(type(a))

<class 'float'>

b = 0.0
print(type(b))

<class 'float'>

c = -1.1
print(type(c))

<class 'float'>

d = 1.
print(type(d))

<class 'float'>

  • int ์™€ float ์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” int ์ผ๊นŒ, float์ผ๊นŒ?
a = 1
b = 1.0

print(type(a))
print(type(b))

<class 'int'>
<class 'float'>

print(type(a + b))

<class 'float'>

  • ์—ฐ์‚ฐ

-๋”ํ•˜๊ธฐ

a = 10
b = 2
a + b

12

-๋นผ๊ธฐ

a = 10
b = 2
a - b

8

-๊ณฑํ•˜๊ธฐ

a = 10
b = 2
a * b

20

-๋‚˜๋ˆ„๊ธฐ

a = 10
b = 2
a / b

5.0

-๋ชซ

a = 10
b = 3
a // b

3

-๋‚˜๋จธ์ง€

a = 10
b = 3
a % b

1

-์ œ๊ณฑ

a = 10
b = 3
a ** b

1000

  • ํ• ๋‹น์—ฐ์‚ฐ

-๋ณ€์ˆ˜ a๋Š” 1์ด๋‹ค. ํ•ด๋‹น ๋ณ€์ˆ˜ a์— 1์„ ๋”ํ•˜์—ฌ๋ผ.

a = 1
a = a + 1
print(a)

2

-ํ• ๋‹น์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•

a = 1
a += 1
print(a)

2

-๋นผ๊ธฐ

a = 1
a -= 1
print(a)

0

-๊ณฑํ•˜๊ธฐ

a = 2
a *= 2
print(a)

4

-๋‚˜๋ˆ„๊ธฐ

a = 1
a /= 2
print(a)

0.5

2)๋ฌธ์žํ˜•

-๋ฌธ์žํ˜•์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌธ์ž๋ฅผ ๋”ฐ์˜ดํ‘œ('), ์Œ ๋”ฐ์˜ดํ‘œ(")๋กœ ๊ฐ์‹ธ์ค€๋‹ค

a = 'hello'
a = "hello"
  • ์ธ๋ฑ์Šค
    • ์ธ๋ฑ์Šค []๋ฅผ ํ†ตํ•ด ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 0๋ถ€ํ„ฐ ์‹œ์ž‘
a = hellow world'

-๋ณ€์ˆ˜ a์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

print(a[0])

h

-๋ณ€์ˆ˜ a์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

print(a[-1])

d

  • ์Šฌ๋ผ์ด์Šค
    • ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ
a = 'hellow world'

-๋ณ€์ˆ˜ a ์ „์ฒด๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค

print(a)

hellow world

or

print(a[:])

hellow world

-๋ณ€์ˆ˜ a์˜ 6~8๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค

print(a[5:8]) # 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 5๋ฅผ ์ž…๋ ฅ

w w

-๋ณ€์ˆ˜ a์˜ ์ฒ˜์Œ ~ 5๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

print(a[:5])

hello

-๋ณ€์ˆ˜ a์˜ ๋’ค์—์„œ๋ถ€ํ„ฐ 5๋ฒˆ์งธ ~ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

print(a[-5:])

world

-๋ณ€์ˆ˜ a์˜ ์ฒ˜์Œ ~ ๋’ค์—์„œ๋ถ€ํ„ฐ -5๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

print(a[:-4])

hellow w

  • ๋ฌธ์ž์—ด์˜ ํ•ฉ
a = 'hellow'
b = 'world'
print(a + b)

hellowworld

  • ๋ฌธ์ž์—ด์˜ ๊ณฑ
a = 'hellow'
print(a * 3)

hellowhellowhellow

  • ๋ฌธ์žํ˜•๊ณผ ์ˆซ์žํ˜•์˜ ํ˜ผ์šฉ

-๋ฌธ์ œ : ๋ณ€์ˆ˜(์ˆซ์ž)๋ฅผ ๋„ฃ์–ด 'My number is (๋ณ€์ˆ˜)' ๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

๋ฐฉ๋ฒ•1

a = 'My number is '
b = 1
print(a, b)

My number is 1

๋ฐฉ๋ฒ•2

a = 'My number is '
b = 1
print(a + b)

๋ฌธ์žํ˜•๊ณผ ์ˆซ์žํ˜•์„ ๋”ํ•ด์ค’๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ˆซ์žํ˜•์„ ๋ฌธ์žํ˜•์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์–ด์•ผ๋งŒ ํ•œ๋‹ค

print(a + str(b))

My number is 1

๋ฐฉ๋ฒ•3

print('My number is {}'.format(b))

My number is 1

-๋‘ ๊ฐœ ์ด์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

print('์˜ค๋Š˜์€ {}์›” {}์ผ ์ž…๋‹ˆ๋‹ค.'.format(5, 10))

์˜ค๋Š˜์€ 5์›” 10์ผ ์ž…๋‹ˆ๋‹ค.

-์ด๋ฆ„์„ ํ†ตํ•œ ๊ฐ’ ๋„ฃ๊ธฐ

print('์˜ค๋Š˜์€ {month}์›” {day}์ผ ์ž…๋‹ˆ๋‹ค. {month}์›”์€ ๊ฐ€์ •์˜ ๋‹ฌ์ž…๋‹ˆ๋‹ค.'.format(month = 5, day = 10))

์˜ค๋Š˜์€ 5์›” 10์ผ ์ž…๋‹ˆ๋‹ค. 5์›”์€ ๊ฐ€์ •์˜ ๋‹ฌ์ž…๋‹ˆ๋‹ค.

a = '์˜ค๋Š˜์€ {month}์›” {day}์ผ ์ž…๋‹ˆ๋‹ค. {month}์›”์€ ๊ฐ€์ •์˜ ๋‹ฌ์ž…๋‹ˆ๋‹ค.'.format(month = 5, day = 10)
print(a)

์˜ค๋Š˜์€ 5์›” 10์ผ ์ž…๋‹ˆ๋‹ค. 5์›”์€ ๊ฐ€์ •์˜ ๋‹ฌ์ž…๋‹ˆ๋‹ค.

  • ๋ฌธ์ž์—ด ๋ถ„ํ•ด

-'hello world' ๋ฅผ 'hello'์™€ 'world'๋กœ ๋ถ„๋ฆฌํ•˜์‹œ์˜ค.

a = 'hello world'
a.split()

['hello', 'world']

-'I am a boy' ๋ฅผ ๊ฐ ๋‹จ์–ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜์‹œ์˜ค.

a = 'I am a boy'
a.split()

['I', 'am', 'a', 'boy']

-'I.am.a.boy' ๋ฅผ '.'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜์‹œ์˜ค.

a = 'I.am.a.boy'

['I.am.a.boy']

a.split('.')

['I', 'am', 'a', 'boy']


 

๋‹ค์‹œ ์˜ค๋žœ๋งŒ์— ํŒŒ์ด์ฌ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ์–ด์ƒ‰ํ•˜๊ณ  ์ง„์งœ ์ƒˆ๋กœ ๋ฐฐ์šฐ๋Š” ๋А๋‚Œ์ด๋ž„๊นŒ... ๊ทผ๋ฐ ๋„ˆ๋ฌด ์ต์ˆ™ํ•˜๊ณ  ์‰ฌ์šดใ…Žใ…Ž  ๋ถ„๋ช… ์ฒ˜์Œ์—” ํ—ท๊ฐˆ๋ฆฌ๊ณ  ์–ด๋ ค์› ๋Š”๋ฐ!! ์•„๋Š” ๋‚ด์šฉ์ด๋ผ ์ง€๋‚˜์น˜๊ณ  ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๋ž˜๋„ ๋ณต์Šตํ•˜๊ธฐ day1 ๋‚ด๊ฐ€ ํ•ด๋ƒ„โ˜บ๏ธ

๋Œ“๊ธ€