TIL6 : Python 기본 문법 복습

김시은·2022년 8월 2일
0

Today I Learned

목록 보기
4/9

Python

아래와 같이 변수 여러 개를 동시에 선언하는 것이 가능하다

>>>a,b,c,d,e = 1,2,3,4,5
>>>print(a,b,c,d,e)
1 2 3 4 5

f스트링도 한 번 더 복습

>>> a, b = 28, 35
>>> f"I'm {a} years old, and my husband is {b}
"I'm 28 years old, and my husband is 35"

형변환(CASTING)은 느낌상 가능할 것 같은 것들은 가능하다

#string을 int로 변환하는 것은 안됨!
>>> str = "hello world"
>>> str = int(str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello world'
#하지만 string타입으로 저장한 숫자를 int로 변환하는 것은 가능!
>>> str = "28"
>>> str = int(str)
>>> str
28

슬라이싱 복습! :앞은 이상, :뒤는 미만!

>>> square = [1,2,3,[4,5]]
>>> sqaure[2:]
[3, [4, 5]]
>>> sqaure[:2]
[1, 2]
>>> square[1:3]
[2, 3]

Concatenation
이런 것도 된다

>>> "I said " + ("Hey " * 2) + "Hey!"
'I said Hey Hey Hey!'

기존 리스트를 연장해서 새로운 리스트를 만드는 것도 +를 이용하여 가능하다.

>>> a1=a+[6,7]
>>> print(a1)
[1, 2, 3, 4, 5, 6, 7]
profile
데이터분석가를 꿈꾸어요

0개의 댓글