0522
파이썬 기초 문법
text = 'abcdefghijk'
result = len(text)
print(result)
result = text[:3]
print(result) // abc
myemail = 'asd@sparta.co'
result = myemail.split('@')[1].split('.')[0]
print(result)
a_list = [1,3,5,7,8]
result = a_list[-1]
print(a_list)
a_list = [1,33,5,7,8]
a_list.sort()
print(a_list)
a_list.sort(reverse=True)
a_list = [1,33,5,7,8]
result = (5 in a_list)
print(result)
a_dict = {'name':'bob','age':27, 'friend':['영희','철수']}
result = a_dict['friend']
print(result)
people = [
{'name': 'bob', 'age': 20, 'score':{'math':90,'science':70}},
{'name': 'carry', 'age': 38, 'score':{'math':40,'science':72}},
{'name': 'smith', 'age': 28, 'score':{'math':80,'science':90}},
{'name': 'john', 'age': 34, 'score':{'math':75,'science':100}}
]
print(people[2]['score']['science'])
money = 3000
if money > 3800:
print('택시를 타자!')
elif money > 1200:
print('버스를 타자!')
else:
print('걸어가자')
fruits = ['사과', '배', '감', '수박', '딸기']
for fruit in fruits:
print(fruit)
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
for person in people:
name = person['name']
age = person['age']
if age > 20:
print(name, age)
for i, person in enumerate(people):
name = person['name']
age = person['age']
print(i, name, age)
if i > 2:
break
0 bob 20
1 carry 38
2 john 7
3 smith 17
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4]
for num in num_list:
if num % 2 == 0:
print(num)
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4]
count = 0
for num in num_list:
if num % 2 == 0:
count += 1
print(count)
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4]
sum = 0
for num in num_list:
sum += num
print(sum)
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4]
max = 0
for num in num_list:
if max < num:
max = num
print(max)
def hello():
print('안녕!')
print('좋은 날씨야')
hello()
hello()
def sum(a,b):
print('더하기를 하셨네요!')
return a+b
result = sum(1,2)
print(result)
def bus_rate(age):
if age > 65:
print('무료입니다')
elif age > 20:
print('성인입니다')
else:
print('청소년입니다')
bus_rate(15)
def check_gender(pin):
num = pin.split('-')[1][:1]
if int(num) % 2 == 0:
print('여성')
else:
print('남성')
check_gender('110312-2029371')