python自學 第五周

2021-09-26 10:46:22 字數 1985 閱讀 1832

pymysql

import pymysql

conn = pymysql.connect(host='192.168.1.7', port = 3306, user='root', passwd='000000', db='yj')

cursor = conn.cursor(cursor=pymysql.cursors.dictcursor) # 執行完畢返回的結果預設元組形式顯示,這裡改為字典

# sql = "create table test(id int, name varchar (20))"

#cursor.execute(sql)

# ret = cursor.execute("insert into test values(1,'alex'),(2,'alvin')")

#返回乙個影響行數

ret = cursor.execute("select * from test")

# print(ret)

#取出結果,顯示結果,這個東西的游標會動

print(cursor.fetchone()) #取第一條

# 游標移動

# cursor.scroll(1,mode='relative') # 相對移動

# cursor.scroll(4,mode='absolute') # 絕對移動

print(cursor.fetchone())

# print(cursor.fetchmany(2)) #取多條

# print(cursor.fetchall()) #取所有

conn.commit()

cursor.close()

conn.close()

生成器函式

# coding=utf-8

import time

'''生成器的yield相當於程式執行在**就會停下,下一次接著繼續,佔記憶體教小

'''def test():

print("開始!")

yield 1

time.sleep(2)

yield 2

time.sleep(2)

yield 3

res = test()

print(res)

print(res.__next__())

print(res.__next__())

消費者模型

# coding=utf-8

import time

# def producer():

# ret =

# for i in range(100):

# time.sleep(0.1)

# return ret

## def consumer(res):

# for index,baozi in enumerate(res):

# time.sleep(0.1)

# print('第個人,吃了'.format(index,baozi))

## res = producer()

# consumer(res)

def consumer(name):

print('我是{},我要開始吃包子了!'.format(name))

while true:

time.sleep(0.5)

baozi = yield

print('{}高興的吃了{}的包子!'.format(name,baozi))

def producer():

for i in range(10):

c1 = consumer('wangwu')

c1.__next__()

c1.send('肉餡{}'.format(i))

producer()

python程式設計第五周

題目內容 依次計算一系列給定字串的字母值,字母值為字串中每個字母對應的編號值 a對應1,b對應2,以此類推,不區分大小寫字母,非字母字元對應的值為0 的總和。例如,colin 的字母值為 3 15 12 9 14 53 輸入格式 一系列字串,每個字串佔一行。輸出格式 計算並輸出每行字串的字母值。輸入...

自學python第五天

python的高階特性 一定要注意一點 越少,開發效率越高 1.切片slice 主要應用於list l abcde l start end step 這個是三種格式的 step就是指的是隔幾個取指 從start到end l start end 正常的從頭到尾 這裡有兩個巧妙的用法 是取第乙個 l 1...

Python自學第五天筆記

知識點回顧 1.list列表 a.建立列表 b.列表的功能 增刪改查 刪 pop 索引 remove 元素 clear 清空 改 reverse 反轉,倒敘 sort 排序 sorted 查 index len max min count 2.for in 迴圈 a.工作原理 b.列表生成器 ran...