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))
相关推荐
Allen_LVyingbo25 分钟前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
智能砖头33 分钟前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
风逸hhh2 小时前
python打卡day58@浙大疏锦行
开发语言·python
烛阴3 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook3 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Gyoku Mint4 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
郭庆汝10 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
思则变13 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
漫谈网络13 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
try2find15 小时前
安装llama-cpp-python踩坑记
开发语言·python·llama