The fourth day learning summary
一、for 循环
循环就是重复做某件事,for循环是python提供第二种循环机制(第一种是while循环),理论上for循环能做的事情,while循环都可以做。
目的:之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁。
代码示例:
for i in range(1, 11):
print('第', {i}, "次循环")
# 嵌套循坏
for i in range(1, 11):
for j in range(1, 11):
print('第', {i}, "次循环", {j}, "次循环")
二、range函数
1.传递一个参数时
当只传入一个参数时,表示生成从 0 开始、小于该参数的整数序列。例如:
>>> range(5)
#range(0, 5)
例子中,range(5) 表示生成一个从 0 开始、小于 5 的整数序列。需要注意的是,实际上 range() 函数返回的是一个 range 类型的对象,并不是一个列表,但在大多数情况下可以像列表一样使用。
2.传递两个参数时
当传入两个参数时,表示生成从 start 开始、小于 stop 的整数序列。例如:
>>> range(2, 5)
range(2, 5)
这个例子中,range(2, 5) 表示生成一个从 2 开始、小于 5 的整数序列。
3.传递三个参数时
当传入三个参数时,表示生成从 start 开始、每次增加 step、小于 stop 的整数序列。例如:
>>> range(0, 10, 2)
range(0, 10, 2)
例子中,由于 start=5、stop=2、step=1,因此无法生成任何数字。
三、while 循环
while循环是一种在满足特定条件的情况下重复执行一段代码的控制结构。它允许我们在不知道确切循环次数的情况下执行代码,只要条件保持为真,代码块就会一遍又一遍地执行。
代码示例:
count = 0
while count <= 10:
print('请求异步接口!')
# status返回我们想要的,就跳出
if count == 5:
break
count += 1
else:
print('调用结束!共调用', count, '次')
四、count函数
count() 是Python的内置函数,可以「统计」字符串里指定「字符」或指定字符串出现的「次数」。
五、content参数
content是一个在处理文件或网络请求时常见的参数。
content通常用于表示文件或网络请求的内容。它可以是文本、二进制数据或者其他类型的数据。根据具体的使用场景和需求,我们可以对content进行读取、写入、处理等操作。
代码示例:
content = '''
count = 0
while count <= 10:
print('请求异步接口!')
# status返回我们想要的,就跳出
if count==5:
break
count += 1
else:
print('调用结束!共调用',count,'次')
'''
print(content)
六、字符串转义
代码示例:
name = 'Will'
age = 24
print('i am ' + name + ',i am age ' + str(age))
注意:在输出结果的时候,因为'age'为整数,需要给'age'前加上str,使之进行强制转换。
七、列表
1.列表的使用
代码示例:
list_demo = [1, 2, 3, 4, 45]
print(list_demo)
2.列表中'下标'介绍
注意:下标的值,要从0开始计算,即0=list中1,1=list中的2.
# [1, 2, 3, 4, 45]————>>list
# 0 1 2 3 4
list_demo = [1, 2, 3, 4, 45]
print('下标3的值:', list_demo[2])
3.向列表中添加元素(append)
将元素添加到列表末尾
代码示例:
list_demo = [1, 2, 3, 4, 45]
list_demo.append(111)
print(list_demo)
4.向指定位置添加元素(insert)
将元素添加到list中指定位置
代码示例:
list_demo = [1, 2, 3, 4, 45]
list_demo.insert(0, 'tmp')
print(list_demo)
5.对元素进行修改(update)
对list中指定位置元素进行修改
代码示例:
list_demo = [1, 2, 3, 4, 45]
list_demo[0] = 'update'
print(list_demo)
6.删除元素(remove)
根据元素值进行删除
代码示例:
list_demo = [1, 2, 3, 4, 45]
list_demo.remove('update')
print(list_demo)
7.判断元素是否在集合中
代码示例:
list_demo = [1, 2, 3, 4, 45]
list_demo[0] = 'update'
if 'update' in list_demo:
print('在这个列表里')
else:
print('不在列表中')
8.找到元素的索引位置
代码示例:
list_demo = [1, 2, 3, 4, 45]
print('找到元素的索引位置:', list_demo.index(4))
9.查看列表里有多少个111
代码示例:
list_demo = [1, 2, 3, 4, 45]
print('查看列表里有多少个111: ', list_demo.count(111))
10.输出列表长度
代码示例:
list_demo = [1, 2, 3, 4, 45]
print('输出列表长度: ', len(list_demo))
11.清空列表(clear)
删除列表所有元素
代码示例:
list_demo.clear()
print(list_demo)
八、元组(tuple)
元组是元素的有序集合,可以通过下标访问,元组中的元素不可更改
代码示例:
tuple_demo = (1, 3, 45, 66)
print(tuple_demo)
#遍历元组
for t in tuple_demo:
print(t)
九、字典
字典由花括号{ }中的一系列键值对表示。
1.定义字典
代码示例:
dict_demo = {'username': 'xiaoqiang', 'password': '111111'}
2.字典取值
代码示例:
print('username:', dict_demo['username'])
3.修改字典的值
代码示例:
dict_demo['username'] = 'user_one'
print(dict_demo)
4.判断字典里是否包含某键值
代码示例:
if 'username' in dict_demo:
print('dict_demo key username')
5.# 获取所有键(keys)
代码示例:
#key = username & password
dict_demo = {'username': 'xiaoqiang', 'password': '111111'}
print(dict_demo.keys())
6.获取所有值(values)
代码示例:
values = xiaoqiang & 111111
dict_demo = {'username': 'xiaoqiang', 'password': '111111'}
print(dict_demo.values())
7.字典的遍历
代码示例:
dict_demo = {'username': 'xiaoqiang', 'password': '111111'}
for key, value in dict_demo.items():
print('key ->', key, 'value ->', value)
8.取值写法
代码示例:
dict_demo = {'username': 'xiaoqiang', 'password': '111111'}
print(dict_demo.get('username'))
十、时间操作
1. 获取当前时间(datetime)
代码示例:
date_time = datetime.datetime.now()
print(datetime.datetime.now())
2.获取当前日期(date:年月日)
代码示例:
date_time = datetime.datetime.now()
print(date_time.date())
3.获取当前时间(time:时分秒)
代码示例:
date_time = datetime.datetime.now()
print(date_time.time())
4.格式化时间(strftime)
代码示例:
date_str = date_time.strftime("%Y-%m-%d %H:%M:%S")
print(date_str)
5.日期加法:new_date = date_time + datetime.timedelta(days=1) # 获取后天的日期
代码示例:
new_date = date_time + datetime.timedelta(days=1)
print(new_date)
6.日期减法:old_date = date_time - datetime.timedelta(days=2) # 获取前天的日期
代码示例:
old_date = date_time - datetime.timedelta(days=2)
print(old_date)
十一、文件操作
1.写入文件操作
覆盖模式(“w”):打开文件并将内容写入文件,如果文件存在,则覆盖原有内容。如果文件不存在则创建一个新的文件。
代码示例:
with open('file.txt', 'w') as f:
f.write("hello test")
2.读文件操作
读写模式(“r+”):打开文件供读取和写入,如果文件存在,则覆盖原有内容。如果文件不存在,将抛出FileNotFoundError异常。
代码示例:
with open('file.txt', 'r') as f:
content = f.read()
print(content)
十二、def函数的使用
Python 中的函数可以接受多个参数,其中包括位置参数、默认参数、可变参数和关键字参数等不同类型的参数。
1.位置参数
位置参数是指按照参数的位置顺序传递的参数,也称为必选参数。当函数被调用时,需要按照函数定义中的参数列表依次传递相应数量的位置参数。
代码示例:
#def 是定义函数的关键字,say_hello 是函数的名称,name 是参数
#在下面的示例中,定义了一个函数 say_hello,它接受一个位置参数 name,用于向用户发送个性化的问候消息。然后,我们调用 say_hello 函数,并传递不同的参数值,从而输出不同的问候消息。
def say_hello(name):
print('hello',name)
say_hello('ALEX')
代码示例:
#def 是定义函数的关键字,add 是函数的名称,x,y 是两个参数
# 加法,有返回值例子
def add(x,y):
#函数执行完毕后,可以使用 return 语句来返回一个值(x+y)
return x+y
print(add(1, 2))
The fourth day learning summary的更多相关文章
- 20162314 《Program Design & Data Structures》Learning Summary Of The Ninth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Ninth Week ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Seventh Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Seventh Wee ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Fifth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Fifth Week ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Second Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Second Week ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The First Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The First Week ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Eleventh Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eleventh We ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Tenth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Tenth Week ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Eighth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eighth Week ...
- 20182320《Program Design and Data Structures》Learning Summary Week9
20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...
- 程序猿学英语—In July the English learning summary
七月的英语学习更加曲折,七月初.查看更多,周六和周日可以保证每天两小时的英语教室学习.周一到周 五一般是上完晚自习九点多来机房学习英语,学习一个小时十点多再回宿舍. 考完试,回家待了五天,不好意思的说 ...
随机推荐
- springboot下载文件 范围下载
springboot下载文件 范围下载 关键词:springboot,download,Range,Content-Range,Content-Length,http code 206 Partial ...
- ES13 中11个令人惊叹的 JavaScript 新特性
前言 与许多其他编程语言一样,JavaScript 也在不断发展.每年,该语言都会通过新功能变得更加强大,使开发人员能够编写更具表现力和简洁的代码. 小编今天就为大家介绍ES13中添加的最新功能,并查 ...
- Solution -「YunoOI 2007」rfplca
Description Link. Given is a rooted tree with the \(\sf1\)-th node as the root. The tree will be giv ...
- 5分钟入门 next13
上半年vercel 推出了nextjs13 这个大版本,刚好最近有个c端的项目,所以就用了这个框架来写,技术体系基本也是文档提到的 tailwindcss + ts + swr + ssr ,总的来开 ...
- mpi转以太网连接300PLC与施耐德 Quantum PLC 通讯
S7300 PLC转以太网无需编程与施耐德 Quantum PLC modbusTCP通信 方案介绍: 西门子300PLC转以太网不需要编写程序通过兴达易控MPI-ETH-XD1.0与施耐德 Quan ...
- Nacos启动报错:Please set the JAVA_HOME variable in your environment, We need java(x64) jdk8 or later
可能原因: 1.JDK版本过低(应不低于1.8) 2.未设置jdk环境变量(可能性低) 3.jdk环境变量设置不适配nacos(博主就是这个原因) 解决方案: 1.直接在startup.cmd文件中设 ...
- C中code关键字
单片机C语言code是什么作用? code的作用是告诉单片机,我定义的数据要存储在ROM(程序存储区)里面,写入后就不能再更改,其实是相当与汇编里面的寻址MOVC(好像是),因为C语言中没办法详细描述 ...
- Use Closures Not Enumerations
http://c2.com/ Use Closures Not Enumerations I was really disappointed when this turned out not to ...
- 揭秘计算机指令执行的神秘过程:CPU内部的绝密操作
计算机指令 从软件工程师的角度来看,CPU是执行计算机指令的逻辑机器.计算机指令可以看作是CPU能够理解的语言,也称为机器语言. 不同的CPU能理解的语言不同.例如,个人电脑使用Intel的CPU,苹 ...
- Ansible与Ansible部署
Ansible与Ansible部署 Ansible简介: Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩.它融合了众多老牌运维工具的优点,Pubbet ...