【蓝桥杯】 蓝桥杯Python必备基础知识

输入输出

python 复制代码
#读取int类型数据
x = int(input())
#读取float类型数据
x = float(input())
#读取string类型数据
x = input()
#读取多个数据
x, y = map(int, input().split())  #其他基本类型同理
#读取一行的数据存放到数组种
int_list = [int(i) for i in input().split()]   #其他基本类型同理

基本函数

python 复制代码
#char转ASCII码
x = ord('a')  #97
#ASCII码转char
x = chr(97)  #a
#除法,去商的整数部分
a = 10.0
b = 4.0
c = a // b  #2.0

string的使用

遍历string
python 复制代码
s = "abcde"
for i in s:
    print(i)
    #do something

运行结果如下所示

list的使用

list的创建
python 复制代码
int_list = list()  #第一种方式
int_list = []  #第二种方式
str_list = list('string')  #['s', 't', 'r', 'i', 'n', 'g']
#创建定长list的两种方式
int_list = [0] * 26  #第一种方式
int_list = [0 for i in range(26)]  #第二种方式
list的使用函数
python 复制代码
int_list = [1, 2, 3, 4, 5, 4, 3]

#直接访问下标
int_list[0]  #1
#计算元素在list里的总个数
int_list.count(5)  #1
#查找元素在list的第一个位置(没有的话会抛出异常)
int_list.index(3)  #2
#向list末尾加入元素
int_list.append(2)  #[1, 2, 3, 4, 5, 4, 3, 2]
#向list末尾加入序列
int_list.extend([2, 1])  #[1, 2, 3, 4, 5, 4, 3, 2, 1]
#清空list
int_list.clear()  #[]
#插入数据到指定位置
int_list.insert(7, 2)  #[1, 2, 3, 4, 5, 4, 3, 2]
#删除元素在list的第一个位置(没有的话会抛出异常)
int_list.remove(3)  #[1, 2, 4, 5, 4, 3]
#指定位置删除元素(默认为最后一个)
int_list.pop(0)  #[2, 3, 4, 5, 4, 3]
int_list.pop()  #[1, 2, 3, 4, 5, 4]
#反转list
int_list.reverse()  #[3, 4, 5, 4, 3, 2, 1]
#对list进行排序
int_list.sort()  #升序,[1, 2, 3, 3, 4, 4, 5]
int_list.sort(reverse=True)  #降序,[5, 4, 4, 3, 3, 2, 1]
#对list进行临时排序,赋值给新的list
int_list_tmp = sorted(int_list)  #升序,[1, 2, 3, 3, 4, 4, 5]
int_list_tmp = sorted(int_list, reverse=True)  #降序,[5, 4, 4, 3, 3, 2, 1]
#判断元素是否在tuple中
flag1 = 1 in int_tuple  #True
flag1 = 6 in int_tuple  #False
#统计list的长度
len = len(int_list)  #7

tuple的使用

元组没什么用,看看就好
元组的特点:
  1. 与列表相似,不同之处就在于元组的元素不能被修改和删除。
  2. 列表使用的是中括号"\[\]",元组使用的是小括号"()"。
  3. 列表属于可变类型,元组属于不可变类型。
  4. Python内部对元组进行了大量的优化,访问喝处理速度都比列表快。
tuple的创建
python 复制代码
int_tuple = ()  #第一种方式
int_tuple = tuple()  #第二种方式
int_tuple = tuple(1, 2, 3)  #第二种方式,创建不为空的tuple

int_list = [1, 2, 3]
int_tuple = tuple(int_list)  #第三种方式
tuple的使用
python 复制代码
int_tuple = (1, 2, 3)

#直接访问下标
int_tuple[0]  #1
#计算元素在tuple里的总个数
int_tuple.count(1)  #1
#查找元素在tuple的第一个位置
int_tuple.index(1)  #0
#判断元素是否在tuple中
flag1 = 1 in int_tuple  #True
flag1 = 4 in int_tuple  #False
#统计tuple的长度
len = len(int_set)  #3

set的使用

set的创建
python 复制代码
int_set = {1, 2, 3}  #第一种方式,大括号里面必须有值,否则将会创建dict
int_set = set()  #第二种方式
set的集合运算操作
python 复制代码
int_set1 = {1, 2, 3}
int_set2 = {3, 4, 5}

#a - b 表示:集合 a 中包含而集合 b 中不包含的元素
int_set3 = int_set1 - int_set2  #{1, 2}
#a | b 表示:集合 a 或集合 b 中包含的所有元素
int_set3 = int_set1 | int_set2  #{1, 2, 3, 4, 5}
#a & b 表示:集合 a 和集合 b 都包含了的元素
int_set3 = int_set1 & int_set2  #{3}
#a ^ b 表示:不同时包含集合 a 和集合 b 的元素
int_set3 = int_set1 ^ int_set2  #{1, 2, 4, 5}
set的操作
python 复制代码
int_set = {1, 2, 3}

#判断元素是否在set中
flag1 = 1 in int_set  #True
flag1 = 4 in int_set  #False
#统计set的长度
len = len(int_set)  #3
#向set里面加入元素
int_set.add(4)  #{1, 2, 3, 4}
#向set里面加入序列
int_set.update([4, 5])  #{1, 2, 3, 4, 5}
#删除set中的元素(没有会报错)
int_set.remove(3)  #{1, 2}
#删除set中的元素(没有不会报错)
int_set.discard(3)  #{1, 2}
#随机删除一个元素
int_set.pop()  #移除最后一个元素,但是具有未知性,所以不推荐使用

dict的使用

dict的创建
python 复制代码
int_dict = {}  #第一种方式
int_dict = dict()  #第二种方式
注意:
  1. key不可以重复,否则只会保留第一个。
  2. value值可以重复。
  3. key可以是任意的数据类型,但不能出现可变的数据类型,保证key唯一。
  4. key一般形式为字符串。
dict的操作
python 复制代码
int_dict = {1: 1, 2: 2, 3: 3}

#向dict增加元素
int_dict[4] = 4  #{1: 1, 2: 2, 3: 3, 4: 4}
#删除dict中的元素
del int_dict[3]  #{1: 1, 2: 2}
#查找dict中的元素
value = int_dict[1]  #1
#修改dict中的元素
dict[1] = 2  #{1: 2, 2: 2, 3: 3}
遍历dict
python 复制代码
int_dict = {1: 1, 2: 2, 3: 3}

#遍历dict的key
for i in int_dict:  #第一种方式
    #do something
for i in int_dict.keys():  #第二种方式
    #do something
#遍历dict的value
for i in int_dict.values():
    #do something
#遍历dict的key和value
for i in int_dict.items():  #第一种方式
    print(i[0])  #key
    print(i[1])  #value
for key, value in int_dict.items():  #第二种方式
    print(key)  #key
    print(value)  #value
相关推荐
漂流瓶jz16 小时前
UVA-1615 高速公路 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·贪心算法·贪心·aoapc·算法竞赛入门经典·uva
学无止境小袁同学16 小时前
LlamaIndex Workflows 学习
python·llm·tensorflow·llamaindex·agent开发·多agent·workflows
夊16 小时前
数据结构第四课:栈与队列
数据结构
鲸渔17 小时前
【AI Agent系列课程13-Python 基础13】JSON 读写:结构化数据交换基础
开发语言·python·json·ai编程
凌冰_1 天前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
wuyk5551 天前
Python办公自动化-第01讲-环境搭建与自动化认知
python·自动化·pyqt
高洁011 天前
数字孪生与计算机视觉:让机器看懂现场
人工智能·python·机器学习·transformer·迁移学习
ComPDFKit1 天前
RAG 回答只显示 PDF 文件名?用 Python 保留引用页码
开发语言·python·pdf·rag
yz_aiks1 天前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
2601_967980111 天前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境