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
- 웨일즈
- red hearse
- 우한코로나
- 치앙마이
- Bolton
- 봉준호감독통역사
- 어서와한국은처음이지
- parasite
- wuhan
- 코로나19
- 코로나바이러스
- 조현병
- 최성재
- 신종코로나
- 필리핀사망
- everybody wants you
- cnn
- 봉준호감독통역
- 창궐
- 우한코로나바이러스
- 중국외교부
- 코로나
- 우한
- 전염병
- 미중
- 정은경 본부장님
- 우한 코로나
- 확진자수
- sharonchoi
- 진짜영웅
Archives
- Today
- Total
오지's blog
freeCodeCamp.org의 python immediate 연습 본문
728x90
반응형
# https://www.youtube.com/watch?v=HGOBQPFzWKo&t=20597s
# 1. list
# list slicing [start_index:end_index:jump]
# list comprehension
num = [i for i in range(1, 11)]
print(num)
slnum1 = num[2:5:1]
print(slnum1)
slnum2 = num[2:5:2]
print(slnum2)
slnum3 = num[::]
print(slnum3)
# =============================================================================== #
# 2. tuple
mytuple = ("helloworld", 123, "pharmacist")
print(f"{type(mytuple), mytuple}")
mytuple = ("helloworld")
print(f"{type(mytuple), mytuple}")
mytuple = ("helloworld", )
print(f"{type(mytuple), mytuple}")
mytuple = tuple(["helloworld", 123, "pharmacist"])
print(mytuple)
item = mytuple[-1]
print(item)
# tuple is immutable so called, cannot be modified after initializing
# mytuple[0] = "greencross"
# mytuple.append("helloworld")
# print(mytuple)
for item in mytuple:
print(item)
if "greencross" in mytuple:
print("yes")
else:
print("no")
if "helloworld" in mytuple:
print("yes")
else:
print("no")
mytuple = ('a', 'p', 'p', 'l', 'e')
print(f"length of mytupe: {len(mytuple)}")
print(f"count: {mytuple.count('p')}")
print(f"index: {mytuple.index('p')}")
#unpacking
mytuple = "kelly", 32, "programmer", "single", 8000, "Seoul"
name, age,*info, city = mytuple
print(f"type of mytuple {type(mytuple)}")
print(f"name: {name}")
print(f"age: {age}")
print(f"info: {info}")
print(f"city: {city}")
import sys
print(sys.getsizeof(mytuple), "bytes")
# https://www.pythonpool.com/tuple-comprehension/
numtuple = (i for i in range(1, 11))
print(numtuple)
print(sys.getsizeof(numtuple), "bytes")
numtuple = tuple((i for i in range(1, 11)))
print(numtuple)
print(sys.getsizeof(numtuple), "bytes")
# tuple is more efficient than list
import timeit
print(timeit.timeit(stmt="[0, 1, 2, 3, 4]", number=1000000))
print(timeit.timeit(stmt="(0, 1, 2, 3, 4)", number=1000000))
# =============================================================================== #
# 3. Dictionary: key-value pairs, unordered, mutable(modifiable)
mydict = {"name": "abraham", "age":40, "occupation":"programmer"}
mydict["hobby"] = "reading"
print(mydict)
mydict.popitem()
print(mydict)
try:
print(mydict["salary"])
except:
print("Error")
for key in mydict.keys():
print(f"key: {key}")
for value in mydict.values():
print(f"value: {value}")
for item in mydict.items():
print(item)
# real copy by using builtin copy function
mydict_copy = mydict.copy()
mydict["email"] = "hello@world.com"
print(f"mydict: {mydict}")
print(f"mydict_copy: {mydict_copy}")
print("\n")
mydict_copy3 = dict(mydict)
mydict["email"] = "hello@world.com"
print(f"mydict: {mydict}")
print(f"mydict_copy3: {mydict_copy3}")
print("\n")
mydict_copy2 = mydict
mydict["email"] = "hello@world.com"
print(f"mydict: {mydict}")
print(f"mydict_copy2: {mydict_copy2}")
print("\n")
mydict2 = {"name": "John", "age":60, "email":"my@email.com"}
mydict3 = {"name": "Biden", "city":"Palo Alto", "salary":10000}
mydict2.update(mydict3)
print(mydict2)
mydict4={2:4, 4:16, 16:256}
print(mydict4[16])
tup = (6,7)
dic1 = {tup: 13}
print(dic1)
# TypeError: unhashable type: 'list'
# lis = [6,7]
# dic2 = {lis: 13}
# print(dic2)
# =============================================================================== #
# 4. Sets: unordered, mutable, no duplicates
myset1 = (1, 4, 9, 16)
print(myset1)
myset2 = set("helloworld")
print(myset2)
myset3 = {}
print(type(myset3))
myset4 = set()
print(type(myset4))
myset4.add(1)
myset4.add(2)
myset4.add(4)
myset4.add(8)
print(myset4.pop())
for x in myset4:
print(x)
if 1 in myset4:
print("yes")
odds = {1, 3, 5, 7, 9}
evens = {0, 2, 4, 6, 8}
primes = {2, 3, 5, 7}
u1 = odds.union(evens)
print(u1)
i1 = odds.intersection(evens)
print(i1)
i2 = odds.intersection(primes)
print(i2)
diff1 = odds.difference(primes)
print(diff1)
diff2 = odds.symmetric_difference(primes)
print(diff2)
# =============================================================================== #
# 5. String ordered, immutable, text representation
mystring1 = "Hello World"
substring = mystring1[2:7]
print(substring)
print(mystring1[::-1])
mystring2 = ' Hello World '
# strip function: remove spaces
print(f" original: {mystring2} , strip: {mystring2.strip()}")
# find function: first index of character
print(mystring2.find('o'))
mystring3 = 'how do you do?'
print(mystring3.split())
'개발노트 > Python' 카테고리의 다른 글
파이썬 패키지 빌드하고 PyPi에 올리기 (0) | 2021.10.16 |
---|---|
WARNING - State of this instance has been externally set to None. Taking the poison pill. (0) | 2021.09.07 |
진료행위정보서비스 크롤러 개발중 해결한 selenium을 이용한 년월 선택문제 (0) | 2021.07.04 |
pymysql.err.Error: Already closed 에러발생 해결과정 (0) | 2021.04.09 |
python에서 super()와 부모클래스이름(parent class name)의 차이점 (0) | 2021.04.07 |
Comments