Python入門小案例

2021-08-13 04:36:07 字數 2285 閱讀 4082

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 = false

class user:

def __init__(self, name):

self.name = name

f = fish()

fish.eat(f, none)

print(f.hungry)

fish.eat(f, 'earthworm')

f.eat('eatworm')

print(f.hungry)

u = user('zhangjun')

print(u.name)

def test2():

count = 0

count = count + 1

count += 1

count -= 1

count = count * 10

count *= 10

name = 'blob'

# return name

x = y = z = 1

print(x, y, z)

x, y, z = 1, 2, 'a string'

print(x, y, z)

x, y = y, x

print('%d mile is the same as %f km' % (x, y))

test2()

print('**' * 5)

pystr = 'zhangjun'

print('%s is cool' % (pystr))

# 元組tuple

# 身有殘疾的唯讀列表

atuple = ('robert', 77, 93, 'try')

print(atuple[1:3])

print(type(atuple))

# atuple[1] = 5

for x in atuple:

print(x)

# 列表

# 左閉右開,

# -1表示最後乙個元素

alist = [1, 2, 3, 'a string']

print(alist[0])

print(alist[2:])

print(alist[:3])

alist[1] = 5

print(alist)

for x in alist:

print(x)

字典:

# 字典 key-value 鍵值對

dict1 =

print(dict1)

print(type(dict1))

namekey = 'name'

dict2 =

print(dict2)

# 1表示乙個key

dict3 =

print(dict3)

# typeerror: unhashable type: 'list' --hash表示雜湊演算法

# dict1 =

# print(dict1)

# 訪問字典的內容 、 讀寫 、刪鍵del

# del dist['name'] del dist

# 字典-物件導向的方法 dict.clear()、dict.keys()、dict.values()

Python 入門小案例

1 合法識別符號規則 數字 下劃線 字母構成 避開關鍵字 不能用數字開頭 避開內建電池中的函式 2 樣例 非法 以字母開頭 1f a 1f a.isidentifier 輸出 false樣例 合法 fhdaksh a fhdaksh a.isidentifier 輸出 true item 1 for...

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...