栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

day2-python-基础语法复习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

day2-python-基础语法复习

第一题:函数内修改同名全局变量需要使用global关键字进行声明,否则报错:UnboundLocalError

# python在局部不可以改变全局变量,编译不通过
num = 1
def fn():
    global num
    num += 1
    return lambda:print(num)
x = fn()
x() #------->2

第二题:join()将两个字符串用指定的符号连接成新的字符串

res = []
for i in 'python':
    if i == 'h':
        continue
    res.append(i)
print(res)#----------->['p', 'y', 't', 'o', 'n']
print(' '.join(res))#------->p y t o n
print(','.join(res))#-------->p,y,t,o,n

第三题:第一次遇见sort带参数的用法

list = ['apple','lemon','pear','peach']
def fn(x):
    return x[::-1]
list.sort(key = fn,reverse = True)
print(list)#------->['pear', 'lemon', 'peach', 'apple']

第四题:mysql连接数据库

mydb = mysql.connector.connect(
    host = "locasthost",
    user = "root",
    passwd = "123456",
    database = "test"
)
# 获取游标操作
mycursor = mydb.cursor()
mycursor.excute("select * from users")
myresult = mycursor.fetchall()
print(myresult)

第五题

counter = 1
def dolotsofstuff():
    global counter
    for i in(1,2,3):
        counter += 1
dolotsofstuff()
print(counter)#-------------->4

第六题

t1 = (1,2,3)
t2 = t1[:]
print(t1 is t2)#---------->True

list1 = [1,2,3]
list2 = list[:]
print(id(list1)==id(list2))#---------->False

s1  ='123'
s2 = '123'
print(s1 is s2)#---------->True

a = 123
b = 123
print(a is b)#---------->True
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号