728x90
lec.01. 변수(Variable)¶
- int, float, str, bool(True, False)
- "+ - * / ** // % "
- type()
- or and not
- 숫자 + 숫자 == 가산(덧셈)
In [21]:
num1 = 1
num2 = 2
print(num1 + num2)
3
- 글자 + 글자 == 두 글자 합친다
In [23]:
num1 = '가나다'
num2 = '라마'
print(num1 + num2)
가나다라마
In [24]:
num1 = 1.74
num2 = 3.12
print(num1 + num2)
4.86
In [25]:
print(1.74 + 3.12)
4.86
In [32]:
1.74 + 3.12
Out[32]:
4.86
In [29]:
1.74 + 3.12
'가나다'+'라마'
Out[29]:
'가나다라마'
In [30]:
print(1.74 + 3.12)
print('가나다'+'라마')
4.86
가나다라마
In [34]:
type(0.74)
Out[34]:
float
In [35]:
type(10)
Out[35]:
int
In [38]:
type('가나다')
Out[38]:
str
In [39]:
type("가나다")
Out[39]:
str
In [40]:
2 * 2 * 2
Out[40]:
8
In [41]:
2**3
Out[41]:
8
- 나누기 몫 나머지
In [47]:
# 주석 : 나누기 몫 나머지
13/5 , 13//5, 13%5
Out[47]:
(2.6, 2, 3)
- (bool)논리 : True, False
In [50]:
print(True)
print(False)
True
False
In [52]:
type(True)
Out[52]:
bool
In [53]:
# and or not
print(True and True)
True
In [54]:
print(False and False)
False
In [55]:
print(True or True)
True
In [56]:
print(False or False)
False
In [57]:
print(True or False) #둘 중 하나라도 True이면 True
True
In [58]:
print(not True)
False
- == (같다) = (대입)
In [62]:
print(3 == 2) , print(3 != 2)
False
True
Out[62]:
(None, None)
In [ ]:
num = 3 # 3값을 num에 넣어라
In [63]:
print( ((3 > 0) or (-5 > 0)) and ((4 > 8) or ( 3 < 0)) )
# True or False False False
# True and False
False
In [ ]:
- 역슬러쉬 특수 표현
In [69]:
# \기호 : 글자기호
string4 = "This is a \"single\" quotation test"
print(string4)
This is a "single" quotation test
In [67]:
# \n : 줄바꿈
print("abc\nabc")
abc
abc
In [68]:
# \t : 탭
print("abc\tabc")
abc abc
- 문자열
In [70]:
# + 합치다
print("ABC" + "DEF")
ABCDEF
In [72]:
# * 반복하다
print('ABC' * 3)
ABCABCABC
In [73]:
print("ABC" - 1) # 에러
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10604/2432741626.py in <module>
----> 1 print("ABC" - 1) # 에러
TypeError: unsupported operand type(s) for -: 'str' and 'int'
리스트(List), 튜플(Tuple), 딕셔너리(Dictionary), ..... 셋(Set)¶
[] () {} {}
In [86]:
num1 = 1
num2 = 2
num3 = 3
num_list = [1,2,2,3,3]
num_tupl = (1,2,2,3,3)
num_set = {1,2,2,3,3} #distinct
In [87]:
print(num_list, type(num_list))
[1, 2, 2, 3, 3] <class 'list'>
In [88]:
print(num_tupl, type(num_tupl))
(1, 2, 2, 3, 3) <class 'tuple'>
In [89]:
print(num_set, type(num_set))
{1, 2, 3} <class 'set'>
In [123]:
num_dict = {'n1':1 , 'n2':2 , 'n3':3 }
print(num_dict, type(num_dict))
print(num_dict['n3'])
{'n1': 1, 'n2': 2, 'n3': 3} <class 'dict'>
3
In [4]:
num_dict = {100:'홍길동' , 200:'아무개'}
print(num_dict, type(num_dict))
print(num_dict[100])
{100: '아무개'} <class 'dict'>
아무개
* 숫자번째(index) 는 0번째 부터 시작
In [106]:
num_list = [1,2,3,4,5]
print(num_list)
print(num_list[:])
print(num_list[0]) # 변수이름[숫자] : 숫자번째(index) 값만 출력
print(num_list[3])
print(num_list[-5])
print(num_list[0:3]) #슬라이싱 [s:e] 0<= ? <3 0,1,2번째 --> 123
print(num_list[:-1]) #슬라이싱 [s:e] 0<= ? <끝 0,1,2,3번째 --> 1234
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
1
4
1
[1, 2, 3]
[1, 2, 3, 4]
In [ ]:
In [112]:
str_list = ['ab','c','d',77, True, 0.14]
print(str_list, type(str_list))
['ab', 'c', 'd', 77, True, 0.14] <class 'list'>
In [6]:
str_list = [ 'ab' ,
'c' ,
'd' ,
77 ,
True ,
0.14 ,
(1,2,3),
[11,22,33],
{'k1':100, 'k2':200},
{1,2}
]
print(str_list, type(str_list))
print(str_list[6])
print(str_list[6][1])
print(str_list[7][0])
print(str_list[8]['k1'])
['ab', 'c', 'd', 77, True, 0.14, (1, 2, 3), [11, 22, 33], {'k1': 100, 'k2': 200}, {1, 2}] <class 'list'>
(1, 2, 3)
2
11
100
In [ ]:
In [132]:
Out[132]:
[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo',
'reason': 'API_KEY_INVALID',
'domain': 'googleapis.com',
'metadata': {'service': 'youtube.googleapis.com'}}]
In [133]:
quiz = {
"error":
#----------------------------
{
"code": 400,
"message": "API key not valid. Please pass a valid API key.",
"errors": [
{
"message": "API key not valid. Please pass a valid API key.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "API_KEY_INVALID",
"domain": "googleapis.com",
"metadata": {
"service": "youtube.googleapis.com"
}
}
]
} #----------------------------
}
# print(quiz)
In [137]:
print( quiz["error"]['code'] )
print( quiz["error"]['errors'][0]['reason'] )
print( quiz["error"]["details"][0]['metadata']['service'] )
400
badRequest
youtube.googleapis.com
In [58]:
quiz = {"error": {"ddd": 111,
"sss": 222,
"errors": [
11 ,
{
"message": "aa",
"domain": "bb",
"reason": "cc"
}
]
}}
In [60]:
quiz["error"]["errors"][1]["domain"]
Out[60]:
'bb'
'Python > 문법' 카테고리의 다른 글
파이썬 - 입력과 출력 (0) | 2021.12.29 |
---|---|
파이썬 For if while 자세히 (0) | 2021.12.29 |
Chapter04 조건문, 반복문 (0) | 2021.12.28 |
Chapter03-6 집합 (0) | 2021.12.26 |
Chapter03-5 딕셔너리 (0) | 2021.12.26 |
댓글