1. 程式人生 > >我寫的第一期作業

我寫的第一期作業

類型 python dict == key fromkeys 重復 blog rate

#!/use/bin/python
# -*- conding:utf-8 -*-

#
# 使用while循環輸入1 2 3 4 5 6 8 9 10
# i = 1
# while i < 11:
# if i == 7:
# pass
# else:
# print(i)
# i = i + 1
#
# 求1-100的所有數的和
# i = 1
# value = 0
# while i <= 100:
# value = value + i
# i = i + 1
# print(value)
#
# 輸出 1-100 內的所有奇數
# i = 1
# while i < 101:
# if i%2 == 1:
# print(i)
# i = i + 1
#
# 輸出 1-100 內的所有偶數
# i = 1
# while i <101:
# if i%2 == 0:
# print(i)
# i +=1

# #求1-2+3-4+5 ... 99的所有數的和
# i = 1
# value = 0
# while i < 100:
# if i%2 == 1:
# value = value + i
# else:
# value = value - i
# i = i + 1
# print(value)


# value = ‘jagkdjklajfklnasdlkgnqwlkj‘
# a = value[0:15:3]
# print(a)

# li = [‘eric‘,‘alex‘,‘tony‘]
# for i ,ele in enumerate(li,100):
# print(i,ele)

# user_tuple = (‘alex‘,‘eric‘,‘tongy‘,[‘張三‘,‘李四‘,‘王五‘])
# user_tuple[3][1] = ‘王麗娟‘
# print(user_tuple)
# li = [‘eric‘, ‘alex‘, ‘tony‘]
# for i in range(0,len(li)):
# print(i+1,li[i])

# for i in range(1,11):
# print(i)


# for i in range(10,0,-1):
# print(i)

# li = (11,)
# print(li)

# ------dict,字典----------

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.clear()
# print(dic)
# v = dic.copy()
# print(v)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.get(‘k1‘)
# print(v)
#
# v = dic[0]
# print(v)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.pop(‘k1‘)
# print(dic)
# print(v)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# k,v = dic.popitem()
# print(dic)
# print(k,v)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.setdefault(‘k3‘,‘v3‘)
# print(dic)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.update({‘k3‘:‘v3‘,‘k4‘:‘v4‘})
# print(dic)

# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.update({‘k1‘:‘v24‘})
# print(dic)


# dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],[1,])
# print(dic)
# dic[‘k1‘] .append(222)
# print(dic)

##額外的功能,字典的k是不可變類型,元組是可以的,字典不能作為字典的key
# dic = {
# ‘k1‘:‘v1‘,
# ‘k2‘:[1,2,3],
# (1,2,):‘1111‘
# }
# print(dic)
#元組可以作為字典的kye

###############set 集合,補課重復的列表 :可變類型#########
# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘}
# s2 = {‘alex‘,‘eric‘,‘tony‘,‘六一‘}
# s1.difference_update(s2)
# print(s1)
# #
# v = s1.symmetric_difference(s2)
# print(v)
# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘}
# # s2 = {‘alex‘,‘eric‘,‘tony‘,‘六一‘}
# s1.discard(‘alex‘)
# print(s1)

# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘}
# s2 = {‘alex‘,‘eric‘,‘tony‘,‘六一‘}
# v = s1.isdisjoint(s2)
# print(v)

############周末作業##########

我寫的第一期作業