Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 스트림
- shebang
- constructor
- Inbound
- finalize
- has-a
- 엔드포인트
- identityHashCode
- extends
- pycharm
- 깊은 복사
- lambda
- generic programming
- 셔뱅
- Java
- singletone
- 내부클래스
- Up Casting
- arraycopy
- down casting
- dbeaver
- 얕은 복사
- access modifier
- Stream
- Wrapper class
- 파이참
- parameter group
- node.js
- 자바
- public static final
Archives
- Today
- Total
٩(๑•̀o•́๑)و
sequence types 본문
#0. 시퀀스 자료형 : list, tuple, range, str, bytes, bytearray
#! /usr/local/bin/python3.8
a = [10, 20, 30, 40]
print(0 in a)
print(0 not in a, end='\n\n')
b = tuple("hello")
print('c' in b, end='\n\n')
print(1 in range(3), end='\n\n')
print('h' in 'Hello world')
====Result====
False
True
False
True
False
#! /usr/local/bin/python3.8
a = [10, 20, 30]
b = ['a', 'b','c']
c = a+b
print(c)
====Result====
[10, 20, 30, 'a', 'b', 'c']
#! /usr/local/bin/python3.8
a = list(range(5))+list(range(5, 10))
print(a)
b = tuple(range(5))+tuple(range(5, 10))
print(b)
print('hello'+' '+"world")
print('1+2='+str(1+2))
====Result=====
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
hello world
1+2=3
#! /usr/local/bin/python3.8
a = [10, 20, 30]*3
print(a)
b = (1, 2)*2
print(b)
print(list(range(2))*2)
print(tuple(range(3))*3)
print('Hello '*2)
====Result====
[10, 20, 30, 10, 20, 30, 10, 20, 30]
(1, 2, 1, 2)
[0, 1, 0, 1]
(0, 1, 2, 0, 1, 2, 0, 1, 2)
Hello Hello
b = (1, 2, 3, 4)
print(len(b))
print(len(range(5)))
print(len("hello!",))
print('=======')
str = "안녕"
print(len(str)) #python3에서는 문자 개수. python2.7에서는 바이트수
print(len(str.encode('utf-8'))) #byte
====Result====
4
5
6
=======
2
6
a = [10, 30, 20]
for i in range(len(a)):
print(a[i], end=' / ')
print('\n')
for i in range(1, len(a)+1):
print(a[-i], end=' / ')
print('\n')
b = (1, 2, 3, 5)
for i in range(len(b)):
print(b[i], end=' / ')
print('\n')
r = range(2, 8, 1) #2,3,4,5,6,7
print(r[3]) #앞에서부터 0번
print(r[-3]) #뒤에서 3번
str = 'Hello world!'
print(str[4])
print(str.__getitem__(4)) #[]은 실제로 __getitem__메서드 호출째
print(str[-4])
=====Result====
10 / 30 / 20 /
20 / 30 / 10 /
1 / 2 / 3 / 5 /
5
5
o
o
r
a = [0, 0, 0, 0]
print(a)
a[1] = 1
a[2] = 2
print(a)
b = (0, 0, 0)
#b[1] = 1 TypeError: 'tuple' object does not support item assignment
print(b)
r = range(4) #0,1,2,3
#r[2] = 2 #TypeError: 'range' object does not support item assignment
print(r)
str = "hello world"
#str[1]='E' #TypeError: 'str' object does not support item assignment
print(str)
====Result====
[0, 0, 0, 0]
[0, 1, 2, 0]
(0, 0, 0)
range(0, 4)
hello world
a = [20, 30, 40, 50]
del a[1]
print(a)
b = (1, 2, 3)
#del b[2] #TypeError: 'tuple' object doesn't support item deletion
print(b)
c = range(4) #0,1,2,3
#del c[2] #TypeError: 'range' object doesn't support item deletion
print(list(c))
str = "hello world"
#del str[1] #TypeError: 'str' object doesn't support item deletion
print(str)
====Result====
[20, 40, 50]
(1, 2, 3)
[0, 1, 2, 3]
hello world
'Python' 카테고리의 다른 글
dictionary (0) | 2020.09.09 |
---|---|
sequence types - slice (0) | 2020.09.08 |
list & tuple (0) | 2020.09.08 |
input (0) | 2020.09.04 |
String (0) | 2020.07.28 |