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))
相关推荐
Storynone1 天前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
小鸡吃米…1 天前
Python—— 环境搭建
python
io_T_T1 天前
python 文件管理库 Path 解析(详细&基础)
python
渔阳节度使1 天前
SpringAI实时监控+观测性
后端·python·flask
铁手飞鹰1 天前
Visual Studio创建Cmake工程导出DLL,通过Python调用DLL
android·python·visual studio
飞Link1 天前
告别盲目找Bug:深度解析 TSTD 异常检测中的预测模型(Python 实战版)
开发语言·python·算法·bug
7yewh1 天前
jetson_yolo_deployment 02_linux_dev_skills
linux·python·嵌入式硬件·yolo·嵌入式
love530love1 天前
ComfyUI rgthree-comfy Image Comparer 节点无输出问题排查与解决
人工智能·windows·python·comfyui·rgthree-comfy·nodes 2.0·vue 节点
badhope1 天前
Docker从零开始安装配置全攻略
运维·人工智能·vscode·python·docker·容器·github
用户0332126663671 天前
使用 Python 复制 Excel 工作表
python