123, 123.45, (123+45j)
'abc', "abc"
['abc', 123]
('abc', 123)
{'name':'Youngeun', 'id':0831}
x_value=12345
print(f'{x_value=}\n{type(x_value)=}')
👉 결과
x_value = 12345
type(x_value) = <class 'int'>
x_str = 'abcde'
print(f'{x_str = }\n{type(x_str) = }')
👉 결과
x_str = 'abcde'
type(x_str) = <class 'str'>
x_list = [x_str, x_value]
print(f'{x_list = }\n{type(x_list) = }')
👉 결과
x_list = ['abcde', 12345]
type(x_list) = <class 'list'>
x_tuple = (x_str, x_value)
print(f'{x_tuple=}\n{type(x_tuple)=}')
👉 결과
x_tuple=('abcde', 12345)
type(x_tuple)=<class 'tuple'>
x_dict = {'name': 'Youngeun', 'id':83100}
print(f'{x_dict=}\n{type(x_dict)=}')
👉 결과
x_dict={'name': 'Youngeun', 'id': 83100}
type(x_dict) = <class 'dict'>
print(f'Before: {x_list = }')
x_list[1] = 'fghij' # 인덱스 1 원소를 'fghij'로 변경
print(f'After : {x_list = }')
👉 결과
Before: x_list = ['abcde', 12345]
After : x_list = ['abcde', 'fghij']
print(f'Before: {x_dict = }')
x_dict['name'] = 'Gildong Hong' # 인덱스 1 원소를 'fghij'로 변경
print(f'After : {x_dict = }')
👉 결과
Before: x_dict = {'name': 'Youngeun', 'id': 83100}
After : x_dict = {'name': 'Gildong Hong', 'id': 83100}
print(f'Before: {x_str = }')
x_str[1] = '2' # 인덱스 1 원소를 '2'로 변경
print(f'After : {x_str = }')
👉 결과
Before: x_str = 'abcde'
TypeError: 'str' object does not support item assignment
print(f'Before: {x_tuple = }')
x_tuple[1] = 'fghij' # 인덱스 1 원소를 'fghij'로 변경
print(f'After : {x_tuple = }')
👉 결과
Before: x_tuple = ('abcde', 12345)
TypeError: 'tuple' object does not support item assignment
x_dict['name']
👉 결과
'Gildong Hong'
print(f'{x_str = } \t\t==> {x_str[0] = }') # 0은 첫원소의 인덱스
print(f'{x_str = } \t\t==> {x_str[3] = }')
print(f'{x_list = } \t==> {x_list[0] = }')
print(f'{x_tuple = } \t==> {x_tuple[-1] = }') # -1은 마지막 원소의 인덱스
print(f'{x_dict = } \t==> {x_dict["name"] = }') # dictionary는 key로 인덱싱
👉 결과
x_str = 'abcde' ==> x_str[0] = 'a'
x_str = 'abcde' ==> x_str[3] = 'd'
x_list = ['abcde', 'fghij'] ==> x_list[0] = 'abcde'
x_tuple = ('abcde', 12345) ==> x_tuple[-1] = 12345
x_dict = {'name': 'Gildong Hong', 'id': 83100} ==> x_dict["name"] = 'Gildong Hong'
print(f'{x_str = } \t\t==> {x_str[2:] = }')
print(f'{x_list = } \t==> {x_list[0:1] = }')
print(f'{x_tuple = } \t==> {x_tuple[0:1] = }')
👉 결과
x_str = 'abcde' ==> x_str[2:] = 'cde'
x_list = ['abcde', 'fghij'] ==> x_list[0:1] = ['abcde']
x_tuple = ('abcde', 12345) ==> x_tuple[0:1] = ('abcde',)
if (조건 1):
(실행문 1)
elif (조건 2): # 생략 및 추가 기능
(실행문 2)
else: # 생략 가능
(실행문 3)
money = 1000
if money <= 500:
print('걸어간다.')
else:
print('택시탄다.')
👉 결과
택시탄다.
money = 1000
if money <= 500:
print('걸어간다.')
elif money <= 2500:
print('버스탄다.')
else:
print('택시탄다.')
👉 결과
버스탄다.
조건변수 = 초기값
while (조건):
조건변수 업데이트 (실행문)
n=0
while n < 5:
n = n+1
print("n=", n)
👉 결과
n= 1
n= 2
n= 3
n= 4
n= 5
for 변수 in (유한한 변수의 값들):
(실행문)
for n in range(0,6):
print("n=", n)
👉 결과
n= 0
n= 1
n= 2
n= 3
n= 4
n= 5
for item in x_list:
print(f'{item = }')
👉 결과
item = 'abcde'
item = 'fghij'
x = [10,20,30,40,50]
for method in [len, max, sum]:
print(f'{method(x) = }')
👉 결과
method(x) = 5
method(x) = 50
method(x) = 150
for item in enumerate(['a','b','c','d','e']):
print(f'{item = }')
👉 결과
item = (0, 'a')
item = (1, 'b')
item = (2, 'c')
item = (3, 'd')
item = (4, 'e')
for i, item in enumerate(['a','b','c','d','e']):
print(f'{i = }, {item = }')
👉 결과
i = 0, item = 'a'
i = 1, item = 'b'
i = 2, item = 'c'
i = 3, item = 'd'
i = 4, item = 'e'
def 함수명(매개변수):
(실행문)
return 변수
def minus(a,b): # a,b는 parameters
result = a-b
return result
a = minus(3,7) # 3,7은 arguments
print(a)
👉 결과
-4
lambda 인자 : 표현식
lambda x : x+1
👉 결과
<function main.(x)>
add_ten = lambda x: x + 10
print((lambda x: x+1)(10))
print(add_ten(10))
👉 결과
11
20
y = 100
(lambda x: x+y+1)(10)
👉 결과
111
list(map(lambda x: x + 10, [1, 2, 3]))
👉 결과
[11, 12, 13]
a = range(10)
list(map(lambda x: 'str'+str(x) if x % 3 == 0 else x, a))
👉 결과
['str0', 1, 2, 'str3', 4, 5, 'str6', 7, 8, 'str9']
a = range(10)
list(map(lambda x: 'str'+str(x) if x % 3 == 0 else float(x) if x % 3 == 1 else x, a))
👉 결과
['str0', 1.0, 2, 'str3', 4.0, 5, 'str6', 7.0, 8, 'str9']
a = range(10)
b = [100]*10 # 브로드캐스팅은 안됨
list(map(lambda x, y: x if x % 3 == 0 else x + y, a, b))
👉 결과
[0, 101, 102, 3, 104, 105, 6, 107, 108, 9]
my_var = 'my_var'
def my_func():
return 'my_func'
def _my_private_func():
return 'my_private_func'
import my_module as mm
print(mm.my_var)
print(mm.my_func())
print(mm._my_private_func())
👉 결과
my_var
my_func
my_private_func
import numpy
import matplotlib.pyplot as plt
print(numpy.sum([1,2,3,4,5]))
plt.plot([10,20,30,40], [1,4,9,16], 'rs--', [10,20,30,40], [11,24,9,6], 'g^-')
plt.show()
👉 결과
import pandas as pd
df = pd.read_csv('bank.csv', sep = ',')
print(df.head(3)) # default=5
print(df.tail(2)) # default=5
👉 결과(모양 깨짐) age;"job";"marital";"education";"default";"balance";"housing";"loan";"contact";"day";"month";"duration";"campaign";"pdays";"previous";"poutcome";"y"
0 30;"unemployed";"married";"primary";"no";1787;...
1 33;"services";"married";"secondary";"no";4789;...
2 35;"management";"single";"tertiary";"no";1350;...
age;"job";"marital";"education";"default";"balance";"housing";"loan";"contact";"day";"month";"duration";"campaign";"pdays";"previous";"poutcome";"y"
4519 28;"blue-collar";"married";"secondary";"no";11...
4520 44;"entrepreneur";"single";"tertiary";"no";113...
df.to_csv('data/bank_new.csv', index=False)
string = "Hello!!!\tPython world.\n My name is Youngeun"
rstring = r"Hello!!!\tPython world.\n My name is Youngeun"
print(string)
print(rstring)
👉 결과
Hello!!! Python world.
My name is Youngeun
Hello!!!\tPython world.\n My name is Youngeun
names = ['홍익', '파이썬', '스트링']
num_ints = [10, 20, 30]
num_float = 4321.12345678
# 변수 지정
print(f'{names}의 나이는 {num_ints}이다.')
# 변수를 인덱싱, 슬라이싱하여 지정
print(f'{names[0]}의 나이는 {num_ints[:2]}이다.')
# {}내 연산 가능
print(f'{names[0]}의 나이는 {num_ints[0] + num_ints[1]}이다.')
# 변수로 사용
for name, age in zip(names, num_ints):
print(f'{name}의 나이는 {age}이다.')
👉 결과
['홍익', '파이썬', '스트링']의 나이는 [10, 20, 30]이다.
홍익의 나이는 [10, 20]이다.
홍익의 나이는 30이다.
홍익의 나이는 10이다.
파이썬의 나이는 20이다.
스트링의 나이는 30이다.
# 자리수와 정렬 지정
print(f'{names[0]:10s}의 나이는 {num_ints[0]:>10d}이다.')
print(f'{names[1]:>10s}의 나이는 {num_ints[0]:^10d}이다.')
# 소숫점 자리수 지정
print(f'{names[2]:>10s}의 숫자는 {num_float:^10.2f}이다.')
👉 결과
홍익 의 나이는 10이다.
파이썬의 나이는 10 이다.
스트링의 숫자는 4321.12 이다.