蓝桥杯Python组知识点

一、基础知识

1.基本输入输出

# 单行输入
num1 = input()  # 返回一个字符串
num2 = int(input())  # 将字符串转为整型
a, b, c = map(int, input().split())  # 一行输入多个数字,并以空格分开
lst1 = list(map(int, input().split()))  # 一行输入多个数字,并以空格分开,返回一个列表
print(num1)
print(num2)
print(a,b,c)
print(lst1)

#多行输入
lst12 = [int(input()) for _ in range(3)]
print(lst12)
lst13 = [list(map(int, input().split())) for _ in range(3)]
print(lst13)

# 输入
12
45
12 23 34
56 66 77
1
2
3
1 2 3
4 5 6
7 8 9
# 输出
12
45
12 23 34
[56, 66, 77]
[1, 2, 3]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

2.字符列表连接

lst2 = ['hello', 'world']
print(''.join(lst2))

# 输出
helloworld

3.字母的大小写转换

str1 = 'hello'
str2 = 'WORLD'
print(str1)
print(str1.upper())
print(str2)
print(str2.lower())

# 输出
hello
HELLO
WORLD
world

4.匿名函数lambda

def square(a):
    return a * a
print(list(map(square, [1, 2, 3])))
print(list(map(lambda x: x*x, [1, 2, 3])))
lst3 = [[2, 4],
     [8, 9],
     [4, 5],
     [5, 10]]
print(sorted(lst3, key=lambda x: x[0]))

# 输出
[1, 4, 9]
[1, 4, 9]
[[2, 4], [4, 5], [5, 10], [8, 9]]

5.进制转换

print(hex(16), oct(16), bin(16))
# 输出
0x10 0o20 0b10000

6.字符与整型之间的转换

print(chr(97), ord('a'))
# 输出
a 97

7.格式化保留小数点后几位小数

num3 = 3.1415926
print(f'{
     num3:.3f}')
# 输出
3.142

8.列表排序

lst4 = [3, 1, 45, 67, 21]
lst5 = sorted(lst4, reverse=True)  # sorted()返回一个新的排序后的列表
print(lst4)
print(lst5)
lst4.sort(reverse=True) # .sort()直接对原来的列表进行重新的排序
print(lst4)

# 输出
[3, 1, 45, 67, 21]
[67, 45, 21, 3, 1]
[67, 45, 21, 3, 1]

9.str的内建函数

str3 = 'hello world'  # 要注意这些内置函数都是返回一个新的字符串对象,并不是对原本的字符串进行修改
str4 = 'HELLO WORLD'
str5 = 'Hello World'
print(str3.upper())  
print(str4.lower())
print(str5.swapcase())  # 字符串大小写互换
print(str3.capitalize())  # 字符串首个字母大写
print(str3.title())  # 字符串每个单词首个字母大写
print(str3.find('l', 0, -1))  # 在指定范围内搜索第一个匹配的字符并返回其索引,若没有匹配项则返回-1
print(str3.index('l'))  # 搜索第一个匹配的字符并返回其索引,若没有匹配项则报错
print(str3.rfind('l', 0, -1))  # 从后往前找第一个匹配的字符并返回其索引,若没有匹配项则返回-1
print(str3.count('ll'))  # 统计子字符串在字符串中出现的次数
print(str3.replace('l', '*', 3))  # 用新字符替换旧字符指定次数
print(str3.strip('h'))  # 删除字符串首尾指定的字符,只能删除字符串首尾的字符
print(str3.split())  # 以指定的字符为分隔符,将字符串分割成一个列表
print('*'.join(str3))  # 用指定的字符将字符串连接成一个新的字符串
print(str3)
print(str4)
print(str5)

# 输出
HELLO WORLD
hello world
hELLO wORLD
Hello world
Hello World
2
2
9
1
he**o wor*d
ello world
['hello', 'world']
h*e*l*l*o* *w*o*r*l*d
hello world
HELLO WORLD
Hello World

10.list的内建函数

lst14 = [1, 2, 3]
lst15 = [3, 4, 5]
print(lst14 + lst15)  # 列表拼接
str5 = 'hello world'
lst14.append(str5)  # 将str5当作一个元素添加
print(lst14)
lst14.extend(str5)  # 将str5的每一个字符当作一个元素添加
print(lst14)
lst14.insert(3, 'hello')  # 在指定为索引位置添加元素
print(lst14)
lst14.remove('l')  # 删除第一个匹配的元素
print(lst14)
print(lst14.index('l'))

二、常用内置模块

1.阶乘factorial

<
  • 20
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学习、天天向上。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值