python - 시퀀스 자료형

girean·2020년 11월 19일
0

python 익히기

목록 보기
7/8
post-thumbnail
# 시퀀스(Sequence) : 문자열, 리스트, 튜플 등의 인덱스를 가지는 자료형
string = "Hello World"
list = ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
tuple = ('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
print(string[0:5]) # Hello
print(list[0:5]) # ['H', 'e', 'l', 'l', 'o']
print(tuple[0:5]) # ('H', 'e', 'l', 'l', 'o')
for i in list:
    print(i)

string2 = ", python"
print(string + string2) # Hello World, python

# len(시퀀스 자료형) : 시퀀스 자료형의 길이를 출력하는 함수
print(len(string + string2)) # 19

# 시퀀스는 반복문 등에서 사용될 수 있음
list = [1, 2, 3, 4, 5]
print(4 in list) # True
print(6 in list) # False

if 3 in list:
    print("3을 포함하고 있습니다.") # 3을 포함하고 있습니다.
profile
Developer

0개의 댓글