Python

[Python] ํŒŒ์ด์ฌ ๊ธฐ๋ณธ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ, print ํ•จ์ˆ˜, ์ธ๋ฑ์Šค ๊ณ„์‚ฐ, for ๋ฌธ ๋“ฑ.

allempty_sheep 2024. 7. 30. 15:21
๋ฐ˜์‘ํ˜•

 

๐Ÿ’จ ํŒŒ์ด์ฌ์˜ ์ถœ๋ ฅ์€ print() ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ’จ ํŒŒ์ด์ฌ์˜ ์ž…๋ ฅ์€ input() ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

๊ทธ๋ ‡๋‹ค๋ฉด ์•„๋ž˜ ๋‚ด์šฉ์„ ์ž…๋ ฅ, ์ถœ๋ ฅ์„ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด๋ณด์ž

์ด๋ฆ„, ๋‚˜์ด ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ , ๊ฒฐ๊ณผ์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค.

 

์ด๋ฆ„:XX
๋‚˜์ด:XX
์ฃผ์†Œ:XXXX

๊ฒฐ๊ณผ
ํ•™์ƒ์ •๋ณด: ์ด๋ฆ„์€ XXX์ด๊ณ  ๋‚˜์ด๋Š” XX์„ธ์ด๊ณ  ์ฃผ์†Œ๋Š” XXX์ž…๋‹ˆ๋‹ค.

 

 

 

 

name = input("์ด๋ฆ„")
age = input("๋‚˜์ด")
address = input("์ฃผ์†Œ")

print("ํ•™์ƒ์ •๋ณด: ์ด๋ฆ„์€ ",name,"์ด๊ณ  ๋‚˜์ด๋Š” ",age,"์„ธ์ด๊ณ  ์ฃผ์†Œ๋Š” ",address,"์ž…๋‹ˆ๋‹ค." )

 

,(์ฝค๋งˆ)  ๋Œ€์‹  + ๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

๋งŒ์•ฝ ๋‚˜์ด์— int ๊ฐ’์œผ๋กœ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

age = int(input("๋‚˜์ด"))

 

- ์ถœ๋ ฅ์‹œ ๋ฐ์ดํ„ฐ ํฌ๋งท์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

%s : ๋ฌธ์ž์—ด ๋ณ€์ˆ˜๊ฐ’์„ ์ถœ๋ ฅ
%d : ์ˆซ์ž ๋ณ€์ˆ˜๊ฐ’์„ ์ถœ๋ ฅ
%f : ์‹ค์ˆ˜ ๋ณ€์ˆ˜๊ฐ’์„ ์ถœ๋ ฅ

print("์ด๋ฆ„:%s\n๋‚˜์ด:%d\n์ฃผ์†Œ:%s" % (name,age,address))

 

๐Ÿ’จ ์ฃผ์„์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ๋Š”๋‹ค.

# ์ฃผ์„์ž…๋‹ˆ๋‹ค.

 

๐Ÿ’จ print ํ•จ์ˆ˜๋กœ sep ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋”ธ๊ธฐ']
print('์‚ฌ๊ณผ', '๋ฐฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋”ธ๊ธฐ', sep='*')

๐Ÿ’จ end ํ•จ์ˆ˜๋กœ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.

print('test')
print('test')

print('test', end=" ")
print('test')

 

- print ํ•จ์ˆ˜์™€ ๋ฌธ์ž์—ด์„ ํ•ฉ์ณ์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด + ๊ธฐํ˜ธ๋กœ ํ•ฉ์ณ์ค€๋‹ค.

fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋”ธ๊ธฐ']
text = '๋‚ด๊ฐ€ ์„ ํƒํ•œ ๊ณผ์ผ : '
print(text + fruits[2], fruits[4] , sep='*')

 

๐Ÿ’จ ํŒŒ์ด์ฌ์—์„œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค

k = ['a', 'b', 'c', 'd', 'e']
#index  0, 1, 2, 3, 4
#index -5 -4 -3 -2 -1

print(k[1])
print(k[1:3])    # 3์˜ ์ „ 2๊นŒ์ง€
print(k[-4:-2])  # -2์˜ ์ „ -3๊นŒ์ง€

 

๐Ÿ’จ ๋ฐฐ์—ด์„ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•

list1 = ['a']
list2 = ['b', 'c', 'd']

print(list1 + list2)
print(list2 + list1)

list1.append('b')
print(list1)
list1.append(list2)
print(list1)

append ์‹œ ์ด๋ ‡๊ฒŒ๋Š” ํ•  ์ˆ˜ ์—†๋‹ค.

list1.append('c', 'd')

 

๐Ÿ’จ for๋ฌธ ์‚ฌ์šฉ๋ฒ•

ftuits = ['๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '๋ฐฐ', '๊ฐ']
for fruit in fruits:
    print(fruit)

๐Ÿ’จ for ๋ฌธ ๊ธธ์ด

๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

5 ์ „์˜ 4๊นŒ์ง€ ๋ฐ˜๋ณต๋œ๋‹ค.

for i in range(1, 5):
	print(i)
    
for i in range(5):
    print(i)

๐Ÿ’จ if ๋ฌธ

if ์กฐ๊ฑด์‹:
	๋ช…๋ น๋ฌธ
else :
	๋ช…๋ น๋ฌธ2
test = 0
if(test == 1):
    print("1")
else:
    print("2")

 

 

 

์œ„์˜ ๊ฒƒ๋“ค์„ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž.

 

1.์ฝ˜์†”์ฐฝ์—์„œ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  ์ˆซ์ž ์ž…๋ ฅ: XXXX

2.์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๊ฐ€ ๋งŒ์•ฝ 1000์ด๋ผ๋ฉด
  1๋ถ€ํ„ฐ 1000์‚ฌ์ด ์ˆซ์ž์ค‘ ์ง์ˆ˜๋ฅผ ๋”ํ•œ๋‹ค

3.๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค
  
  1๋ถ€ํ„ฐ 1000๊นŒ์ง€ ์ง์ˆ˜ํ•ฉ:XXX,XXX

 

 

 

 

start_number = int(input("์‹œ์ž‘ ์ˆซ์ž ์ž…๋ ฅ"))
end_number = int(input("๋ ์ˆซ์ž ์ž…๋ ฅ"))
sum = 0
for i in range(start_number, end_number+1):
    if((i%2) == 0):
        sum += i

# print("%d๋ถ€ํ„ฐ %d ๊นŒ์ง€ ์ง์ˆ˜ ํ•ฉ : %d" % (start_number, end_number, sum))
print(f"{start_number} ๋ถ€ํ„ฐ {end_number} ๊นŒ์ง€ ์ง์ˆ˜ ํ•ฉ : {sum}")

 

ํ™€์ˆ˜ ํ•ฉ, ์ด ํ•ฉ๊ณ„๋„ ์ถ”๊ฐ€ ํ•ด ๋ณด์ž.

start_number = int(input("์‹œ์ž‘ ์ˆซ์ž ์ž…๋ ฅ"))
end_number = int(input("๋ ์ˆซ์ž ์ž…๋ ฅ"))
sum = 0
sum2 = 0
for i in range(start_number, end_number+1):
    if((i%2) == 0):
        sum += i
    elif((i%2) != 0):
        sum2 += i

# print("%d๋ถ€ํ„ฐ %d ๊นŒ์ง€ ์ง์ˆ˜ ํ•ฉ : %d" % (start_number, end_number, sum))
print(f"{start_number} ๋ถ€ํ„ฐ {end_number} ๊นŒ์ง€ ์ง์ˆ˜ ํ•ฉ : {sum}")
print(f"{start_number} ๋ถ€ํ„ฐ {end_number} ๊นŒ์ง€ ํ™€์ˆ˜ ํ•ฉ : {sum2}")
print(f"{start_number} ๋ถ€ํ„ฐ {end_number} ๊นŒ์ง€ ์ด ํ•ฉ๊ณ„ ํ•ฉ : {sum + sum2}")

 

๐Ÿ’จ format ์‚ฌ์šฉ๋ฒ•

month = '2์›”'
day = '20์ผ'
say = "{} {} ์€ ์ผ์š”์ผ์ž…๋‹ˆ๋‹ค.".format(month, day)
print(say)

๐Ÿ’จ Strip(), replace(), split()

str = " Hello, World " 
print(str)
str1 = str.strip();					# ๊ณต๋ฐฑ์ œ๊ฑฐ.
print(str1)
str2 = str1.replace("Hello", "Hi")	# ๋ฌธ์ž์—ด ๋Œ€์ฒด.
print(str2)
str3 = str2.split(',')				# ํŠน์ • ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ ๋ฐฐ์—ด์œผ๋กœ ์ €์žฅ. 
print(str3)

 

๐Ÿ’จ ํ•จ์ˆ˜ ์„ ์–ธ ๋ฐ ์‚ฌ์šฉ

#define ํ•จ์ˆ˜ ์„ ์–ธ
def hi(name):
    print(f"Hi my name is {name}")
hi('dapin')

 

 

๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž

 

1.์ฝ˜์†”์ฐฝ์—์„œ ์ž…๋ ฅ

  ์‚ฌ์›๋ช…:ํ™๊ธธ๋™
  ๊ทผ๋ฌด์‹œ๊ฐ„:9

2.ํ•จ์ˆ˜ ์„ ์–ธ
  - ํ•จ์ˆ˜๋ช…:payCalc
  - ๋งค๊ฐœ๋ณ€์ˆ˜: name,workHour
  - ์‹œ๊ธ‰: 2000์›
  - 8์‹œ๊ฐ„ ์ดˆ๊ณผ์‹œ๊ฐ„์— ๋Œ€ํ•ด์„œ๋Š” ์‹œ๊ธ‰์˜ 1.5๋ฐฐ

2.๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๋„๋ก ์ฝ”๋”ฉ

  XXX ์‚ฌ์› ๊ธ‰์—ฌ๋Š” XXX์› ์ž…๋‹ˆ๋‹ค.

 

 

 

 

def payCalc(name, workHour):
    hr = 2000
    salary = 0
    if workHour > 8:
        hro = hr * 1.5
        salary =  round((hr) * 8 + hro * (workHour - 8))
    else:
        salary = hr * workHour

    print(f'{name} ์‚ฌ์›์˜ ๊ธ‰์—ฌ๋Š” {salary: ,}์› ์ž…๋‹ˆ๋‹ค.')

name = input("์ด๋ฆ„")
workHour = int(input("์‹œ๊ฐ„"))
payCalc(name, workHour)