Python 入門小案例

2021-09-29 09:00:11 字數 1683 閱讀 6415

1)合法識別符號規則:數字、下劃線、字母構成;避開關鍵字;不能用數字開頭;避開內建電池中的函式

2)樣例:非法(以字母開頭:1f)

a='1f'

a.isidentifier()

輸出:

false
樣例:合法(fhdaksh)

a='fhdaksh'

a.isidentifier()

輸出:

true
[item[::-1] for item in 'i am a student'.split(' ')]
輸出:

['i', 'ma', 'a', 'tneduts']
a=['ai','julyedu.com','python','ai','python']

print(a)

b=set(a)

for i in b:

c = a.count(i)

print(i,'出現次數',c)

輸出:

['ai', 'julyedu.com', 'python', 'ai', 'python']

python 出現次數 2

julyedu.com 出現次數 1

ai 出現次數 2

s = input ("請輸入:")

if len(s)>3:

if s.endswith("ing"):

s += "ly"

else:

s += "ing"

else:

pass

print (s)

輸出1:(非ing結尾)

請輸入:jdhda

jdhdaing

輸出2:(ing結尾)

請輸入:fighting

fightingly

輸出3:(小於3個字元長度)

請輸入:abc

abc

import random

a = random.sample(range(0,100),10)

b = random.sample(range(0,100),10)

print ("a序列:" ,a)

print ("b序列:" ,b)

bingji = list(set(a)^set(b))

jiaoji = list((set(a).union(set(b)))^(set(a)^set(b)))

print("並集為:",bingji)

print("交集為:",jiaoji)

輸出:

a序列: [41, 78, 43, 76, 7, 83, 8, 6, 73, 67]

b序列: [38, 50, 74, 3, 51, 26, 48, 76, 73, 78]

並集為: [3, 67, 38, 6, 7, 74, 8, 41, 43, 48, 50, 51, 83, 26]

交集為: [73, 76, 78]

Python入門小案例

python入門小案例 print hello def test print mr zhang test if name main print world class fish hungry true def eat self,food if food is not none self.hungry...

Spring AOP之入門小案例

1.定義切面類aspect 增添 component 告訴spring容器掃瞄這個元件 aspect 告知spring這個類是個切面類 兩個註解 定義切面類 aspect component public class loggingadvice 2.定義切點pointcut 定義切點,並定義在哪些地...

python異常小案例

自定義登入系統 自定義異常型別 class nameqes exception pass class pwdques exception pass 定義方法,檢查密碼輸入狀態 def checklogin username,userpwd if len username 3 or len usern...