Python Base——Part 2

Python Base——Part 2

今天要给大家带来的依旧是关于Python的一些基础知识

本次分享的主要内容包括:

在Python中数的加减乘除运算、数的下划线、列表简介(索引)、修改列表中的元素、从列表中添加及删除元素、对列表中的元素进行排序

#2.4 数
#对整数进行加(+)减(-)乘(*)除(/)运算
a=2+3+(2+3)*5+8/4 #将任意两个数相除时,结果总是浮点数,即便这两个数都是整数且能整除
print(a)
b= 2 + 3 + (2 + 3) * 5 + 8 / 4 #空格不影响Python计算表达式的方式
print(b)
#浮点数:Python将所有带小数点的数称为浮点数
h,j,k=0.1+0.1,0.2*0.2,0.8/0.2
print(h,j,k)
#对于浮点数的运算,结果包含的小数位数可能是不确定的
#所有语言都存在这种问题,就现在而言,忽略多余的小数位数即可
p=1+0.2+3
print(p)
#无论是哪种运算,只要有操作数是浮点数,Python默认得到的都是浮点数,即便结果原本为整数也是如此
#同时给多个变量赋值:用逗号将变量名分开
x,y,z=1,2,3
d=x+y**2+z #Python用**表示乘方运算
print(d)
#数的下划线:当数很大时,可以使用下划线将其中的数字分组,使其更易清晰易读
billion=100_000_000
print(billion) #当打印这种使用下划线的数时,Python不会打印其中的下划线
#常量:常量类似于变量,但其值在程序的整个生命周期内保持不变
#Python没有内置的常量类型,但Python程序员会使用大写来指出应将某个变量视为常量
MAX_CONNECTIONS=5000
#在代码中,要指出将某个特定的变量视为常量,可将其字母全部大写
#练习2.8
x,y,z,t,h=4+4,10-2,2**3,16/2,2*4
print(x,y,z,t,h)
print(4+4,10-2,2**3,16/2,2*4)
a=12
my_favor=f"my favorite number is {a}."
print(my_favor.title())
name="paddi wow"
my_lover=f"my favorite person is {name}!"
print(my_lover.title())
import this #Python之禅
#第三章:列表简介
#在Python中,用方括号([])表示列表,并用逗号分分隔其中的元素
letter=['a','b','c','d']
print(letter)
#索引从0开始,而不是从1开始
#访问列表元素:查找列表letter中第一个元素
print(letter[0]) #注意:Python从0开始排列,第一个字母对应的位置是0,而不是1
#访问列表中第三个元素
print(letter[2])
name=['padd wo','paddi wo','padi wow','wow']
print(name[0].title())
print(name[-1].upper()) #返回列表最后一个元素
print(name[-2].upper()) #返回列表倒数第二个元素
#使用列表中的各个值
lover=f"My favorite person is {name[0].title()}!!!"
print(lover)
me=f"My name is {name[1].title()}!!!"
print(me)
#3.1 练习
name=['paddi wo','pad wow','paddi wowww','love you','loves']
print(name[0])
print(name[1])
print(name[-1])
print(f"{name[0].title()}, how are you?")
way=['bicycle','bus','subway','taxi']
print(f"I would like to go to work by {way[-1]}.")
#修改列表元素
name[-2]="love you so much"
print(name)
#在列表末尾添加元素
name.append("miss you")
print(name)
love=[]
love.append("paddi paddi")
love.append("loves")
love.append("wow wow")
print(love)
#在列表中插入元素:指定要插入元素的索引和值即可
love.insert(2,"most")
print(love)
#从列表中删除元素
del love[2] #注意:删除元素和插入不一样,直接是del然后后面是空格
print(love)
#使用方法pop()删除元素:pop弹出一个元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#弹出一个元素并将其重新命名:删除列表末尾的元素
#pop()的特点是,弹出一个元素并且还可以使用这个元素,与前面的直接从列表中删除元素不同
popped_motorcycles=motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
print(f"The last motocycle I owned is {motorcycles.pop()}.")
#弹出任意一个元素:指定该元素的索引和位置即可
like=motorcycles.pop(0)
print(like)
print(f"I like {like.title()} best!")
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
print(f"I like {motorcycles.pop(1).title()} best!!!")
#注意:每当使用pop()时,被弹出的元素就不再在列表中了
#del和pop():如果要从列表中删除一个元素且不再以任何方式使用它,就使用del语句;如果要在删除元素后还能继续使用它,就使用方法pop()
#根据值删除元素
motorcycles.remove("honda")
print(motorcycles)
#使用remove()从列表中删除元素时,也可以接着使用它的值
motor=['honda', 'yamaha', 'suzuki', 'ducati'] 
expensive='ducati'
motor.remove(expensive)
print(f"The reason why I don't like {expensive.title()} is:\n\t {expensive.title()} is too expensive that I can't pay for it!!!")
print(motor)
#3.4 练习
list_today=[]
list_today.append("grandpa")
list_today.append("grandma")
list_today.append("lisa")
list_today.append("bill")
print(list_today)
invite=f"Can you come and participate the party tonight, {list_today[0]}? I'm looking forward to your arrival!"
print(invite)
popped_list=list_today.pop(-1)
print(popped_list)
list_today.append("professor")
print(list_today)
inviteyou=f"Can you come and participate the party tonight? \n\tName: {list_today[-1]}\nI'm looking forward to your arrival!"
print(inviteyou)
goodnews=f"I found a bigger dining-table:\n\tLocation:\n\tBeijing HGJRJD\nI'm looking forward to your arrival!"
print(goodnews)
print(list_today)
list_today.insert(2,"you")
list_today.insert(0,"he")
list_today.append("it")
print(list_today)
print(f"Sorry, {list_today.pop(0).title()}, you can't attend this party.")
print(f"Sorry, {list_today.pop(3).title()}, you can't attend this party.")
print(f"Sorry, {list_today.pop(2).title()}, you can't attend this party.")
print(f"Sorry, {list_today.pop(3).title()}, you can't attend this party.")
print(f"Sorry, {list_today.pop(2).title()}, you can't attend this party.")
print(list_today)
print(f"{list_today[0].title()}, Congratulations! Welcome to this party tonight!!!")
print(f"{list_today[1].title()}, Congratulations! Welcome to this party tonight!!!")
del list_today[1]
del list_today[0] #只有del是用的[],而insert,append,pop都是用的()
print(list_today)
#组织列表
#sort()对列表进行永久排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("The original list is:")
print(cars)
print("The list after permanent sort is:")
cars.sort()
print(cars)
#sorted()对列表进行临时排序
print("The list after temporary sort is:")
print(sorted(cars))
print(sorted(cars,reverse=True)) #向函数sorted()传递参数reverse=True,从而按与字母相反的顺序显示列表
#reverse()反转列表元素的排列顺序,它并不是按与字母顺序相反的顺序排列列表元素
cars.reverse()
print()
#reverse()永久性地修改列表元素的排列顺序,但可再次调用reverse()让列表随时恢复到原来的排列顺序
cars.reverse()
print()
#len()获取列表的长度
len(cars)
print(len(cars))
#练习3.8
location=['wuhan','chongqing','yunnan','sichuan','xizang','shanghai']
print(location)
print(sorted(location)) #对列表进行临时排序
print(location)
print(sorted(location,reverse=True)) #对列表进行临时排序,且顺序为与字母相反的顺序
print(location)
location.reverse() #将列表反转过来
print(location)
location.reverse()
print(location)
location.sort()
print(location)
location.sort(reverse=True) #对列表进行永久排序,且顺序为与字母相反的顺序
print(location)
print(len(list_today)) #列表的长度
print(len(location))
print(f"I want to go to {len(location)} places when get my master degree.")

在这一部分,比较有意思的是Python之禅(import this),“Simple is better than complex”,还有很多很多,有时间真的应该好好看一看,研究研究,学习一下。

今天的分享到此结束。欢迎大家莅临的另外一个草稿箱(公众号:统计小菜椒)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值