오지's blog

freeCodeCamp.org의 python immediate 연습 본문

개발노트/Python

freeCodeCamp.org의 python immediate 연습

오지구영ojjy90 2021. 8. 29. 00:19
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())
Comments