Python 基础Day03

函数的参数

函数是绝大多数编程语言中都支持的一个代码的“构建块”,但是Python中的函数与其他语言中的函数还是有很多不太相同的地方,其中一个显著的区别就是Python对函数参数的处理。在Python中,函数的参数可以有默认值,也支持使用可变参数,所以Python并不需要像其他语言一样支持函数的重载

常用数据结构

##列表排序

list1 = ['orange','apple','zoo','internationallization','blueberry']
list4 = sorted(list1,key=len)
print(list4)

list1 = ['orange','apple','zoo','internationallization','blueberry']
list4 = sorted(list1,key=lambda x:len(x))
print(list4)

注:和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表。

使用元祖

我们把多个元素组合到一起就形成了一个元组,所以它和列表一样可以保存多条数据。

def main():
    # 定义元组
    t = ('骆昊', 38, True, '四川成都')
    print(t)
    # 获取元组中的元素
    print(t[0])
    print(t[3])
    # 遍历元组中的值
    for member in t:
        print(member)
    # 重新给元组赋值
    # t[0] = '王大锤'  # TypeError
    # 变量t重新引用了新的元组原来的元组将被垃圾回收
    t = ('王大锤', 20, True, '云南昆明')
    print(t)
    # 将元组转换成列表
    person = list(t)
    print(person)
    # 列表是可以修改它的元素的
    person[0] = '李小龙'
    person[1] = 25
    print(person)
    # 将列表转换成元组
    fruits_list = ['apple', 'banana', 'orange']
    fruits_tuple = tuple(fruits_list)
    print(fruits_tuple)


if __name__ == '__main__':
    main()

注:(1)元组中的元素是无法修改的,事实上我们在项目中尤其是多线程环境
(2)元组在创建时间和占用的空间上面都优于列表。

使用集合

在这里插入图片描述
注:Python中允许通过一些特殊的方法来为某种类型或数据结构自定义运算符(后面的章节中会讲到),上面的代码中我们对集合进行运算的时候可以调用集合对象的方法,也可以直接使用对应的运算符。

使用字典

字典是另一种可变容器模型,类似于我们生活中使用的字典,它可以存储任意类型对象,与列表、集合不同的是,字典的每个元素都是由一个键和一个值组成的“键值对”,键和值通过冒号分开。

dict_ = {1:100,'Joker':'value'}


# for i in dict_:
#     value = dict_[i] # .get('Joker','None') 适用于字典动态变化的时候
#     print(value)

# 同时获取键值对
# for key,value in dict_.items():
#     print(key,value)
# 获取键名
print(dict_.keys())
# 获取键值
print(dict_.values())

注:字典的一切操作都是通过键名操作键值!!!;字典是无序的,没有索引操作,除非转换成有序字典,
补充:当你要在其他的py文件中引用本文件中的任何东西;如果你不想要在其他文件中运行该文件中的某些代码;你就可以用"if name == “main”:"

练习1 计算器做加法

def jisuanqi(*args):
    sum = 0
    xx = 0
    while xx !='st':
        xx = input('数字[输入st停止]')
        if xx != 'st':
            sum += float(xx)
            print(sum)
    
    def start():
        jisuanqi()
    start()
    
结果显示如下:
数字[输入st停止]1
1.0
数字[输入st停止]4
5.0
数字[输入st停止]4
9.0
数字[输入st停止]st

练习2 随机点名

import random
def dianming():
    names = ["杨宁","吴蓉","白露","吴东","李彦婷","段晓磊","张晨照","李璐"]
    begin= random.randint(0,len(names)-1)
    print (names[begin])
def start():
    dianming()

    
start()

结果显示如下:
点到的名字为:
李璐

练习3 判断是否是数字

def Num():
    num = float(input('请输入:>>>'))
    num1 = int(num)
    num2 = str(num1)
    if 48<=ord(num2)<=57:
        print("它是一个数字")
    else:
        print("它不是一个数字")
    def start():
        Num()
    start()
    
结果显示如下:
请输入:>>>4
它是一个数字

练习4 删除所有空格

a  = ' a b c de f  g '
   b = ''
   for i in a:
       if i != "":
           b+=i
   print(b)
   
结果显示如下: abcdefg

练习5 找出所有的网址

import requests

response = requests.get('http://news.baidu.com/')
response.encoding = 'utf8'
HTML = response.text

for line in HTML.split('\n'):
    if '<a' in line and 'http' in line:
        splits = line.split('"')
        for s in splits:
            if 'http' in s and 'background-image' not in s:
                print(s)
结果显示如下:
https://www.baidu.com/
http://tieba.baidu.com/
https://zhidao.baidu.com/
http://music.baidu.com/
http://image.baidu.com/
http://v.baidu.com/
http://map.baidu.com/
http://wenku.baidu.com/
http://news.baidu.com/
http://www.xinhuanet.com/politics/xxjxs/2019-08/15/c_1124877659.htm
http://m.news.cctv.com/2019/08/15/ARTIhsSY2cvhZcoZUl9c4xBz190815.shtml
http://tv.cctv.com/2019/08/15/VIDEuY2Vcv1uniK1O4Lkjlha190815.shtml?spm=C31267.PFsKSaKh6QQC.S71105.11
http://www.xinhuanet.com/2019-08/15/c_1124881494.htm
http://www.xinhuanet.com/politics/70nltzb1/index.htm
https://wap.peopleapp.com/article/4489728/4365112?from=singlemessage
http://www.xinhuanet.com/fortune/2019-08/16/c_1124881640.htm
http://opinion.huanqiu.com/editorial/2019-08/15308261.html
https://3w.huanqiu.com/a/78fa3c/7Pbh9eyuVNe?agt=8
http://www.xinhuanet.com/politics/2019-08/16/c_1124883270.htm
https://3w.huanqiu.com/a/b0dac5/7PbFkaqYpQ4?agt=8
https://3w.huanqiu.com/a/3458fa/7Pb4YiAUBxK?agt=8
http://baijiahao.baidu.com/s?id=1642001738395746326
https://3w.huanqiu.com/a/776501/7PbClXpoBMs?agt=8
http://baijiahao.baidu.com/s?id=1642012998744211683
https://3w.huanqiu.com/a/a4d1ef/7PbNLDOHv1u?agt=8
https://3w.huanqiu.com/a/fdf32f/7PbaRzzvfvG?agt=8
https://3w.huanqiu.com/a/16c2d1/7PbHUf9MFUs?agt=8
https://3w.huanqiu.com/a/0c789f/7Pbm7blhEC4?agt=8
https://3w.huanqiu.com/a/26ef70/7PbF4IxuXZK?agt=8
https://3w.huanqiu.com/a/b6423a/7PbCqVTKyNW?agt=8
https://3w.huanqiu.com/a/21eee3/7PbIe4x2WJO?agt=8
https://3w.huanqiu.com/a/c36dc8/7Pbi2ggV4di?agt=8
https://baijiahao.baidu.com/s?id=1642008743084968657&wfr=content
http://baijiahao.baidu.com/s?id=1642007153930572730
http://baijiahao.baidu.com/s?id=1642003062684039493
https://3w.huanqiu.com/a/c36dc8/7PbF1leQuha?agt=8
https://baijiahao.baidu.com/s?id=1642008870218401821&wfr=content
https://3w.huanqiu.com/a/24d596/7PbKnn4FNTi?agt=8
https://3w.huanqiu.com/a/c36dc8/7PbbJLYhmaA?agt=8
https://3w.huanqiu.com/a/1b9fb2/7PbHKLLOhhu?agt=8
http://baijiahao.baidu.com/s?id=1642010323265072987
http://baijiahao.baidu.com/s?id=1642008369348774343
https://3w.huanqiu.com/a/dc5aff/7PbGjoqhYC4?agt=8
http://baijiahao.baidu.com/s?id=1641993275094980044
https://3w.huanqiu.com/a/a4d1ef/7PbJZtTDgqs?agt=8
https://3w.huanqiu.com/a/5e2a9b/7PbCZ0LYr16?agt=8
https://3w.huanqiu.com/a/23f012/7PbELKQiDba?agt=8
https://baijiahao.baidu.com/s?id=1642011457173146004&wfr=content
https://3w.huanqiu.com/a/0c789f/7PbEShbB58k?agt=8
http://www.qstheory.cn/zt2019/llxjj/index.htm
http://www.qstheory.cn/zt2017/xcgcdd19djs/index.htm
https://www.baidu.com/s?wd=%E3%80%8A%E6%B1%82%E6%98%AF%E3%80%8B%E5%8F%91%E8%A1%A8%E4%B9%A0%E8%BF%91%E5%B9%B3%E9%87%8D%E8%A6%81%E6%96%87%E7%AB%A0
https://www.baidu.com/s?wd=70%E5%B9%B4%E6%9D%A5%E4%B8%AD%E5%9B%BD%E5%9F%8E%E9%95%87%E5%8C%96%E7%8E%87%E5%A4%A7%E5%B9%85%E6%8F%90%E5%8D%87
https://www.baidu.com/s?wd=%E9%A6%99%E6%B8%AF%E4%BA%8B%E6%80%81
https://www.baidu.com/s?wd=%E6%96%B9%E8%A8%80%E8%A1%A8%E6%83%85%E5%8C%85
https://www.baidu.com/s?wd=%E8%8B%B9%E6%9E%9CiPhone%2011
https://www.baidu.com/s?wd=%E4%BC%A0%E9%98%BF%E9%87%8C%E6%94%B6%E8%B4%AD%E7%BD%91%E6%98%93%E8%80%83%E6%8B%89
https://www.baidu.com/s?wd=%E6%AD%A6%E7%A3%8A%E8%BD%B0%E6%AC%A7%E6%88%98%E9%A6%96%E7%90%83
https://www.baidu.com/s?wd=%E5%8D%93%E5%B0%942-1%E9%B2%81%E8%83%BD
https://www.baidu.com/s?wd=%E6%AF%94%E5%88%A9%E6%97%B6%E5%BE%81%E6%94%B6%E5%A2%9E%E5%80%BC%E7%A8%8E
https://www.baidu.com/s?wd=%E5%8D%B0%E5%B7%B4%E4%BA%A4%E7%81%AB
http://baijiahao.baidu.com/s?id=1641978760576181286
http://baijiahao.baidu.com/s?id=1641978760576181286
http://baijiahao.baidu.com/s?id=1641943579143052166
http://baijiahao.baidu.com/s?id=1641943579143052166
http://baijiahao.baidu.com/s?id=1641937828081895727
http://baijiahao.baidu.com/s?id=1641937828081895727
http://baijiahao.baidu.com/s?id=1641976608020263487
http://baijiahao.baidu.com/s?id=1641985159753105639
http://baijiahao.baidu.com/s?id=1641983649597429759
http://baijiahao.baidu.com/s?id=1641949428136238077
http://baijiahao.baidu.com/s?id=1641977475383353511
http://baijiahao.baidu.com/s?id=1641982379643450827
http://baijiahao.baidu.com/s?id=1641975034385243379
http://baijiahao.baidu.com/s?id=1641940266650010666
http://baijiahao.baidu.com/s?id=1641934568893547537
http://baijiahao.baidu.com/s?id=1641937095454389099
http://baijiahao.baidu.com/s?id=1641933377621821077
http://baijiahao.baidu.com/s?id=1641925748967465399
http://baijiahao.baidu.com/s?id=1641923942123887857
http://baijiahao.baidu.com/s?id=1641918655684126673
http://baijiahao.baidu.com/s?id=1641918279416532676
http://baijiahao.baidu.com/s?id=1641941697708888008
http://baijiahao.baidu.com/s?id=1641978929124552748
http://baijiahao.baidu.com/s?id=1641978929124552748
http://report.12377.cn:13225/toreportinputNormal_anis.do
http://downpack.baidu.com/baidunews_AndroidPhone_1014720b.apk
https://itunes.apple.com/cn/app/id482820737
https://www.baidu.com/duty/wise/wise_secretright.html
http://net.china.cn/chinese/index.htm
http://www.cyberpolice.cn/wfjb/
http://www.bjjubao.org/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值