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))
相关推荐
西柚小萌新1 小时前
【Python爬虫基础篇】--4.Selenium入门详细教程
爬虫·python·selenium
橘猫云计算机设计1 小时前
springboot基于hadoop的酷狗音乐爬虫大数据分析可视化系统(源码+lw+部署文档+讲解),源码可白嫖!
数据库·hadoop·spring boot·爬虫·python·数据分析·毕业设计
YOULANSHENGMENG2 小时前
linux 下python 调用c++的动态库的方法
c++·python
SsummerC2 小时前
【leetcode100】零钱兑换Ⅱ
数据结构·python·算法·leetcode·动态规划
一眼青苔2 小时前
切割PDF使用python,库PyPDF2
服务器·python·pdf
电商数据girl2 小时前
产品经理对于电商接口的梳理||电商接口文档梳理与接入
大数据·数据库·python·自动化·产品经理
三道杠卷胡3 小时前
【AI News | 20250424】每日AI进展
人工智能·pytorch·python·语言模型·github
T糖锅G4 小时前
小白自学python第二天
python
满怀10154 小时前
【OpenCV图像处理实战】从基础操作到工业级应用
图像处理·人工智能·python·opencv·计算机视觉·编程入门
AI视觉网奇4 小时前
四元数转旋转矩阵
人工智能·pytorch·python