IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> practice2_3 -> 正文阅读

[Python知识库]practice2_3

Day1 记录一些自己学的东西

写的太粗糙了,记得后期改

2-1 简单信息

x=5
x
5

2-2 多条简单信息

y=4
y
4
y=3
y
3
user_name = "Eric"
user_name
'Eric'

2-3 个性化信息

print("Hello "+user_name+", would you like to learn some Python today?")
Hello Eric, would you like to learn some Python today?

2-4 调整名字大小写

print(user_name.lower())
eric
print(user_name.upper())
ERIC
print(user_name.title())
Eric

2-5 名言

famous_person = "Albert Einstein"
message = "A person who never made a mistake never tried anything new."
print(famous_person+' once said, “'+message+'”')
Albert Einstein once said, “A person who never made a mistake never tried anything new.”

2-6 名言2

2-7 剔除人名中的空白

先用’\t’,再用’\n’, 视觉上更明显

name = "\tLi Ming\n"
print(name)
	Li Ming
print(name.lstrip())
Li Ming
print(name.rstrip())
	Li Ming
print(name.strip())
Li Ming

2-8 数字8

print(5+3)
8
print(16/2)
print(4*2)
8.0
8
print(9-1)
8

2-9 最喜欢的数字

number = 8
print("This is my favorite number : "+str(number))
This is my favorite number : 8

3-1 姓名

names = ['zhang san','li si','wang wei']
for name in names:
    print(name.title())
Zhang San
Li Si
Wang Wei

3-2 问候语

for name in names:
    print(name.title()+", have a good day!")
Zhang San, have a good day!
Li Si, have a good day!
Wang Wei, have a good day!

3-3 自己的列表

ways = ['taking bus','riding a bicycle']
for way in ways:
    print(way+" is one of my favorite ways to go to work!")
taking bus is one of my favorite ways to go to work!
riding a bicycle is one of my favorite ways to go to work!

3-4 嘉宾名单

guests = ['zhang san','li si','wang wei']
for guest in guests:
    print(guest.title()+ ", would you like to have dinner with me?")
Zhang San, would you like to have dinner with me?
Li Si, would you like to have dinner with me?
Wang Wei, would you like to have dinner with me?

3-5 修改嘉宾名单

print("Sorry, LiSi doesn't .")
Sorry, LiSi doesn't .
guests[1] = 'li ming'
for guest in guests:
    print(guest.title()+ ", would you like to have dinner with me?")
Zhang San, would you like to have dinner with me?
Li Ming, would you like to have dinner with me?
Wang Wei, would you like to have dinner with me?

3-6添加嘉宾

print("A bigger table was found.")
A bigger table was found.
guests.insert(0,'zhao yi')
guests.insert(2,'dong er')
guests.append('tian wu')
for guest in guests:
    print(guest.title()+ ", would you like to have dinner with me?")
Zhao Yi, would you like to have dinner with me?
Zhang San, would you like to have dinner with me?
Dong Er, would you like to have dinner with me?
Li Ming, would you like to have dinner with me?
Wang Wei, would you like to have dinner with me?
Tian Wu, would you like to have dinner with me?

3-7 缩减名单

print("Sorry, I can only invite two people.")
Sorry, I can only invite two people.

对列表求长度是用函数len(),这里确定范围用的是range()函数,指定个数和列表

for index in range(2,len(guests)):
    guest = guests.pop()
    print(guest.title()+ ", sorry,I can't have dinner with you.")

for guest in guests:
    print(guest.title()+", I feel so glad to have dinner with you!")

del guests[-1]
del guests[0]
print(guests)
Tian Wu, sorry,I can't have dinner with you.
Wang Wei, sorry,I can't have dinner with you.
Li Ming, sorry,I can't have dinner with you.
Dong Er, sorry,I can't have dinner with you.
Zhao Yi, I feel so glad to have dinner with you!
Zhang San, I feel so glad to have dinner with you!
[]

3-8 放眼世界

places = ["xizang","chengdu","dali","qingdao","mohe"]
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']

sorted()函数用于临时排序,所以只能在print时调用,但是sort()方法用于永久排序 反序时传参数reverse = True

print(sorted(places))
['chengdu', 'dali', 'mohe', 'qingdao', 'xizang']
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
print(sorted(places,reverse = True))
['xizang', 'qingdao', 'mohe', 'dali', 'chengdu']
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
places.reverse()
print(places)
['mohe', 'qingdao', 'dali', 'chengdu', 'xizang']
places.reverse()
print(places)
['xizang', 'chengdu', 'dali', 'qingdao', 'mohe']
places.sort()
print(places)
['chengdu', 'dali', 'mohe', 'qingdao', 'xizang']
places.sort(reverse = True)
print(places)
['xizang', 'qingdao', 'mohe', 'dali', 'chengdu']

3-9 晚餐嘉宾

names = ['zhang san','li si','wang wei']
len(names)
3

3-10 尝试各种函数

cities = ['qingdao','beijing','xiamen','chengdu','taian','dali']
print(cities[0])
qingdao
# print(cities[8]) 主动引发索引错误
print(cities[-1])              #通过索引来访问元素,注意负数的使用,有时会带来方便
dali

寻求更加干净整洁的输出

print(cities[0].title())  #首字母大写
Qingdao

注意列表元素的索引是从0开始的

message = cities[3].title() + "'foods are famous in the world!"
print(message)
Chengdu'foods are famous in the world!

元素的增删改

cities[2] = 'tianjin'
print(cities)
['qingdao', 'beijing', 'tianjin', 'chengdu', 'taian', 'dali']
cities.append('mohe')
print(cities)
['qingdao', 'beijing', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
cities.insert(2,'jinan')
print(cities)
['qingdao', 'beijing', 'jinan', 'jinan', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
del cities[2]    #永久删除,且删除的值不可再用
print(cities)
['qingdao', 'beijing', 'jinan', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']
capital = cities.pop(1)  #永久删除,但被删除的元素可以使用
print(capital + " is the capital of China")
beijing is the capital of China
cities.remove('jinan')  # 按值移除,只删第一个出现的,多个重复值时要考虑用循环来删
print(cities)
['qingdao', 'tianjin', 'chengdu', 'taian', 'dali', 'mohe']

排序

print(sorted(cities))
['chengdu', 'dali', 'mohe', 'qingdao', 'taian', 'tianjin']
cities.reverse()
print(cities)
['mohe', 'dali', 'taian', 'chengdu', 'tianjin', 'qingdao']
cities.sort()
print(cities)
['chengdu', 'dali', 'mohe', 'qingdao', 'taian', 'tianjin']
cities.sort(reverse = True)
print(cities)
['tianjin', 'taian', 'qingdao', 'mohe', 'dali', 'chengdu']
len(cities)
6
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-04 12:48:17  更:2021-10-04 12:50:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 20:55:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计