python字元型別

2022-10-08 18:15:13 字數 3940 閱讀 3939

#

-*- coding:utf-8 -*-

#auther = 林深見鹿,海藍見鯨

'''dict =

dict['jiao'] = "class5"

print(dict)

print(dict.keys())

print(dict.values())

print(dict.items())

print(dict["bob"])

for k,v in dict.items():

print(k,v)

for k in dict:

print(k,dict[k])

for k in dict.keys():

print(k)

print(len(dict))

''''''

import random

sum_number =

count = 1

for i in range(1,301):

print("總員工號:",sum_number)

for i in range(6):

count += 1

if count <=4:

win1 = random.sample(sum_number, 30)

for j in win1:

# print(j)

sum_number.remove(j)

print("剩餘未中獎人員",len(sum_number))

print(f"第次開獎")

print(f"三等獎中獎名單:")

elif 5 <= count <7:

win2 = random.sample(sum_number, 10)

for k in win2:

# print(j)

sum_number.remove(k)

print("剩餘未中獎人員",len(sum_number))

print(f"第次開獎")

print(f"二等獎中獎名單:")

elif count == 7:

win3 = random.sample(sum_number, 1)

for q in win3:

# print(j)

sum_number.remove(q)

print("剩餘未中獎人員",len(sum_number))

print(f"第次開獎")

print(f"一等獎中獎名單:")

else:

exit("**結束")

''''''

import random

import time

staff_number =

for i in range(300):

print(staff_number)

level = [30,6,3]

for i in range(3):

# print(level[i])

winpople = random.sample(staff_number,level[i])

# print(i)

for p in winpople:

# print(p)

staff_number.remove(p)

print(f"抽中等獎的人是:")

time.sleep(1)

''''''

all_user = [

["趙","北京市順義區"],

["錢","北京市昌平區"],

["孫","北京市密雲區"],

["李","山東省德州市"],

["周","河北省衡水市"],

["吳","天津市南開區"],

["鄭","上海市長寧區"],

["王","河南省鄭州市"],

["牛","內蒙古呼和浩特"]

]new_list = {}

for i in all_user:

print(i[1][:3])

print(i)

if i[1][:3] not in new_list:

new_list[i[1][:3]] = [i]

print(new_list[i[1][:3]])

print([i])

print(new_list.keys())

else:

for k in new_list:

print(k,end="\n")

print(new_list[k],end="\n")

''''''

dict = [["bob","class1"],["alex","class2"],["gob","class3"],["joe","class4]"],["class1","class]"]]

# print(dict)

d2 = {}

for i in dict:

# print(i)

# print(i[1][:6])

if i[1][:6] not in d2:

d2[i[1][:6]] = [i]

else:

print(d2)

for k in d2:

print(k,end='\n')

print(d2[k],end='\n')

''''''

f = open(r"c:\\users\\xckj-jiaoweinan\desktop\\1.txt",mode="r+",encoding="utf-8")

data = f.read()

print(data)

f.write("jiao\n")

f.write("jiao1\n")

new = data.replace("jiao1","焦")

print(new)

f.write(new)

f.close()

countsum = data.count("jiao")

print(countsum)

f.seek(0)

f.truncate()

''''''

a = [1,2,3,4,5,6,7,8,9,0]

print(bool(true))

print(all(a))

''''''

all_name = dict(name="bob", age="20", class_number="1班")

print(all_name)

print(dir())

print(locals())

# dir()列印當前程式全部變數名

# locals()列印當前程式全部變數名和變數值

a = list()

print(a)

l = list(range(10))

print(l)

def count(x):

return x ** 2

res = map(count, l) # 並沒有執行 (迭代器),方法只能定義乙個引數,方法需要return

for i in res: # 每迴圈一次,就把列表的元素給count函式執行

print(i)

rex = max(l) #求最大值

rem = min(l) #求最小值

reum = sum(l) #求和

print(rex)

print(rem)

print(reum)

''''''

print(round(1.2331566,3))

a = ["1","2","3"]

b = ["a","b","c"]

print(zip(a,b))

# 兩個列表每個元素配對,只一一對應,多出的元素不顯示配對(py2顯示為none)

for i in zip(a,b):

print(i)

'''

Python判斷字元型別

輸入一行字元,輸出其中空格 數字 中文字元 英文本元和其他字元的個數。str input 請輸入一行字元 n chinese 0letters 0space 0digit 0others 0for c in str if c.isspace space 1elif c.isdigit digit 1...

python判斷unicode字元型別

def is chinese uchar 判斷乙個unicode是否是漢字 if uchar u u4e00 and uchar u u9fa5 return true else return false def is number uchar 判斷乙個unicode是否是數字 if uchar u...

Python字串型別

字串,就是由零個或多個字元組成的有限序列。python中,使用單引號或雙引號包圍起來的單個或多個字元,就可以表示乙個字串。字串中的字元可以是特殊符號 英文本母 中文字元 希臘字母,包括emoji字元等。可以在字串中使用反斜槓 來表示轉義,後面的字元不再是它原來的意義,例如 n不是代表反斜槓和字元n,...