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))
相关推荐
张张123y4 小时前
后端第一课:输入 URL 后发生了什么?我用手写 Socket 把全链路跑通了
python·websocket·ai·socket·url·后端开发
全栈弄潮儿6 小时前
Python实战第7期:字典与集合
后端·python·编程语言
2603_965148116 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化
xixiaoyunya7 小时前
Python 异步编程入门:用 asyncio 轻松实现并发任务
开发语言·python·php
把我格子衫拿来7 小时前
Dice 很高就够了吗?分割评价中的 IoU、表面距离与 HD95
python·图像分割·模型评估·医学影像·dice
曲径海盗7 小时前
【Python 学习第 31 天】复习:变量与数据类型 等 3 题
python·学习笔记·编程入门
Data_Journal7 小时前
如何绕过 Cloudflare:网页抓取的最佳方法
网络·python
我命由我123458 小时前
智能助手通用问答边界
人工智能·python·学习·ai·学习方法·ai编程·智能体
Angeldream1239 小时前
第 4 章 医学影像预处理
图像处理·人工智能·python·医学影像·医学影像处理
Web3&Basketball9 小时前
实测Gemini4Argon:ERQA 87%登顶长程任务榜
数据库·python·大模型·多模态·gemini·长上下文·推理优化