Python复习1:

一、数据类型

1.数字:int、float、bool

2.字符串:string

3.列表:list

4.集合:set

5.字典:dictionary

二、Test

1.print输出固定格式

python 复制代码
num1=10
str1="hello world"
#输出的固定格式
print("num1=%d" %(num1))
print(f"num1={num1}")
print(f"str1={str1}")
python 复制代码
#06d 整形必须是6位数,如果不足6位,就用0补齐
student_no=1
print("student_no=%6d" %student_no)
print("student_no=%06d" %student_no)

2.查看变量的类型:type(变量名)

python 复制代码
print("type(num1)=%s"%(type(num1)))
print(f"type(num1)={type(num1)}")
print(f"type(str1)={type(str1)}")
#python语言在定义变量时,不需要指定变量的数据类型,python会根据变量值,自动实现变量类型

3.列表类型 特点:有序,可重复,可扩展

python 复制代码
name=["张三","李四","王五","赵六"]
print(f"name={name}  type(name)={type(name)}")

4.元组类型 特点:有序,可重复

python 复制代码
name=("张三","李四","王五","赵六")
print(f"name={name}  type(name)={type(name)}")

5.集合 特点:无序,不可重复,可扩展

python 复制代码
name={"张三","李四","王五","赵六"}
print(f"name={name}  type(name)={type(name)}")

6.字典 key->value 键值对

python 复制代码
stu_dict={"id":1,"name":"张三","age":18,"score":100}
print(f"stu_dict={stu_dict},type(stu_dict)={type(stu_dict)}")

三、input输入,返回类型都是string类型的

python 复制代码
#input是一个阻塞函数,返回类型都是string类型的
name=input("请输入姓名")
age=input("请输入年龄")
print("name=%s,age=%s" % (name,age))
#print("name=%s,age=%d" % (name,age))  会报错

四、循环函数

复制代码
python没有i++

while循环(循环增量不要忘记)

python 复制代码
i=0
while i < 10:
    #print(i,end='\t')
    print("i=%d" %i ,end='\t')
    i+=1  #python没有i++

for循环

其中range函数的stop不包含那个值

python 复制代码
#Python内置函数:range(start,stop(不包含),step)
#作用:生成指定范围的序列

#查看range函数生成的序列中的每一个数值
#for 循环变量 in range
for i in range(1,11,1):
    print(i,end='\t')

五、随机数

python 复制代码
#随机数 random

import random

#使用random模块调用randomint(a,b)方法,会得到a~b之间的任意随机整数
num=random.randint(1,3)
print("num=%d"%(num))
相关推荐
小小测试开发38 分钟前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想1 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波2 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
copyer_xyf2 小时前
Python 异常处理
前端·后端·python
麻雀飞吧3 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy3 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.3 小时前
【01】Python 机器学习
开发语言·python
为爱停留3 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python
l1t4 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦4 小时前
Python 搭建简易HTTP服务
开发语言·python·http