输入

单个类型数据

1
2
y=eval(input())#函数智能化处理
print(y,type(y))

多个类型数据输入

1
2
d,e,f=map(int,input("以逗号隔开:").split(","))
# 全部转成整形数,并在输入时用,来分辨。

将一个数据分开

1
2
3
4
5
6
>>> a=[]
>>> a=input().split("@")
dsdaddsd@afafasfad@affgsafaaff@afsa
>>> a
['dsdaddsd', 'afafasfad', 'affgsafaaff', 'afsa']
>>>

将一个数据拆开成多个数据

1
2
3
4
5
6
7
8
9
>>> str=input("以空格隔开")
以空格隔开10 20 30
>>> a,b,c=str.split(" ",2)# 2是分三段
>>> a
'10'
>>> type(a)
<class 'str'>
>>>

输出

1
2
3
4
5
(1)一般方式:
print(输出项1[,输出项2,…,输出项n,sep=分隔符,end=结束符])
print("Hello World" )
print("Hello World",sep=' ',end='\n',file=sys.stdout,flush=False)

1
2
3
4
5
6
注:
y,m,d=map(float,input().splir("/"))
print(round(y,4))
print(m)
print(round(d,4))
# 四舍五入过短不执行
1
2
3
4
(2)格式字符方式:
print(”其他输出项 %格式字符” %变量名)
print("he is %d years old" % (25))
print("name:%s \t weight:%.1f"%("cl",70.11))
1
2
3
4
5
(3)print+format方式:
print(‘ {[参数编号或参数名][:格式限定符]}’.format(参数或变量))
print('{0},{1}'.format('Leo',32))
print('{},{},{}'.format('Leo','boy',32))
print('{name},{sex},{age}'.format(age=32,sex='male',name='Leo')
1
2
3
4
因此上面的注可以改造
print("%.4f"%y)
print("{:.4f}".format(m))
print(round(d))
1
2
3
4
T=float(input("请输入摄氏温度:"))
F=32+T*1.8
print("%.1f摄氏度=%f华氏度"%(T,F))#.1f可化为g
print("{:.1f}摄氏度={}华氏度".format(T,F))

导入

  1. 用import导入模块

    1
    2
    命令::import 模块1[.类名  as    别名][,模块2 [.
    类名 as 别名]]……

    eg:

    1
    2
    3
    import math,random 
    math.log2(8)
    random.randint(1,10)
    1
    2
    import matplotlib.pyplot as plt
    plt.scatter(5,5,s=50,color='r')
  2. from…inport命令

    1
    2
    3
    from 模块  import 类 或 *(全部)
    form math import *
    print(2*pi)

math库

1
2
3
4
5
6
7
8
math库中的常用函数
pi 常数π(近似值) sin(x)正弦函数
e 常数e(近似值) cos(x)余弦函数
fabs 求绝对值 tan(x)正切函数
trunc(x) 将一个浮点数结尾为整数 ceil(x)大于等于x的最小整数
factorial(x)求x的阶乘 floor(x)小于等于x的最大整数
pow(x,y)求x的y次方sqrt(x) 求x的平方根

1
2
3
4
5
6
7
eg:
求点到直线的距离
from math import *
a,b,c=map(float,input("输入a,b,c,逗号为间隔").split(","))
x0,y0=map(float,input("输入x0,y0,逗号间隔").split(","))
d=(a*x0+b*y0-c)/sqrt(a*a+b*b)
print("距离=%.4f"%d)

time库

计算机中时间的表示从“1970年1月1日00:00:00” ( unix 时间点 )开始,以毫秒(1/1000 秒)进行 计算

1
2
3
4
import time
b=int(time.time())
b//365//24//60//60+1970
#结果为2024

分段函数

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,15, 1000)# 从0-15找1000个点
i1 = [1 if (i<5) else 0 for i in x]
i2 = [1 if (5<=i<10) else 0 for i in x]
i3 = [1 if (i>=10) else 0 for i in x]
y = np.cos(x)* i1 + x * i2 + np.sin(x)*i3
plt.plot(x,y)
plt.show( )

循环:range

1
2
3
4
5
( 起点,终点(不包含),步长)
range(start=0,stop,step=1)
(1)start参数表示计数开始,默认值为0;
(2)stop参数表示计数结束,但不包括stop的值;
(3)step参数表示步长,默认值为1。

re

base64

今天做到了base64的题目,但是对python的base64不是很理解

1
2
3
4
import base64
origin = '5Mc58bPHLiAx7J8ocJIlaVUxaJvMcoYMaoPMaOfg15c475tscHfM/8=='
biao = str.maketrans('qvEJAfHmUYjBac+u8Ph5n9Od17FrICL/X0gVtM4Qk6T2z3wNSsyoebilxWKGZpRD','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
print(base64.b64decode(origin.translate(biao)))

这题是这样做出来的,我们分析下base64的简单用法

1
2
3
4
5
6
7
8
9
10
11
base64.b64encode(data, altchars=None):将bytes-like对象编码为base64格式的bytes。可以通过altchars参数指定替代字符集。

base64.b64decode(s, altchars=None, validate=False):将base64格式的bytes解码为原始数据。可以通过altchars参数指定替代字符集,validate参数用于指定是否验证输入数据的有效性。

base64.b64encode() 和 base64.b64decode() 方法是最常用的用于base64编码和解码的函数。

base64.urlsafe_b64encode(data) 和 base64.urlsafe_b64decode(s):与上述函数类似,但使用URL和文件名安全的字符集进行编码和解码。

base64.standard_b64encode(data) 和 base64.standard_b64decode(s):使用标准的base64字符集进行编码和解码。

base64.encodebytes(s) 和 base64.decodebytes(s):这些函数与 b64encode() 和 b64decode() 类似,但是它们接受和返回的是bytes对象而不是字符串对象。

下面是maketrans的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
maketrans() 是 Python 中用于创建字符映射表的方法之一。它通常与字符串对象的 translate() 方法一起使用,用于对字符串中的字符进行替换。

用法:
python
translation_table = str.maketrans(x, y, z)
其中,参数 x、y 和 z 都是长度相等的字符串,分别表示需要进行映射替换的字符集合。

x:需要替换的字符集合。
y:用于替换的字符集合。
z:可选参数,用于指定需要从字符串中删除的字符集合。
返回值:
maketrans() 方法返回一个字典,表示字符的映射关系,可以传递给 translate() 方法使用。

示例:
python
# 创建字符映射表
translation_table = str.maketrans('aeiou', '12345', 'xyz')

# 使用映射表进行字符串替换
result = 'hello world'.translate(translation_table)

print(result) # 输出: 'h2ll4 w3rld'

在这个示例中,maketrans('aeiou', '12345', 'xyz') 创建了一个字符映射表,将字符串中的元音字母 'aeiou' 分别替换为 '12345',并将字符 'xyz' 删除。然后,'hello world'.translate(translation_table) 使用这个映射表对字符串进行了替换。

接下来是translate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
translate() 方法是 Python 中字符串对象的一个内置方法,用于根据给定的映射表对字符串中的字符进行替换或删除操作。

用法:
python
new_string = string.translate(translation_table)
其中,string 是要进行替换操作的字符串,translation_table 是一个映射表,可以通过 maketrans() 方法创建。

参数:
translation_table:一个字典,表示字符的映射关系。这个字典通常是通过 maketrans() 方法创建的。
返回值:
translate() 方法返回一个新的字符串,该字符串是根据映射表对原始字符串进行替换或删除后得到的结果。

示例:
python
# 创建字符映射表
translation_table = str.maketrans('aeiou', '12345')

# 使用映射表进行字符串替换
result = 'hello world'.translate(translation_table)

print(result) # 输出: 'h2ll4 w4rld'
在这个示例中,我们首先使用 maketrans() 方法创建了一个字符映射表,将字符串中的元音字母 'aeiou' 分别替换为 '12345'。然后,我们使用 translate() 方法根据这个映射表对字符串 'hello world' 进行了替换操作,得到了新的字符串 'h2ll4 w4rld'。

find

今天做了一道逆向,发现除了遍历还有一种函数更快的找到字符串

在Python中,你可以使用find()方法或者index()方法来在一个字符串中寻找另一个字符串的值。

  1. find(substring, start, end)方法返回子字符串第一次出现的位置,如果没有找到则返回-1。其中,substring是你要查找的子字符串,start是搜索的起始位置(默认为0),end是搜索的结束位置(默认为字符串的长度)。
1
2
3
4
string = "hello world"
substring = "world"
position = string.find(substring)
print(position) # 输出:6
  1. index(substring, start, end)方法也返回子字符串第一次出现的位置,但如果没有找到则会抛出一个ValueError异常。
1
2
3
4
5
6
7
string = "hello world"
substring = "world"
try:
position = string.index(substring)
print(position) # 输出:6
except ValueError:
print("Substring not found")

这两种方法都可以在一个字符串中寻找另一个字符串的值。

通过这个我写出了如下的代码解决问题

原题:

1
2
3
4
5
for i in range(len(flag)):
s1 = ord(flag[i])//17
s2 = ord(flag[i])%17
result += s[(s1+i)%34]+s[-(s2+i+1)%34]
print(result)

逆向

1
2
3
4
5
6
7
8
9
i=0
for m in range (0,len(result),2):
s1=(s.find(result[m]))-i
s2=-(s.find(result[m+1]))-i-1
if s2<0:
s2=s2+34
flag+=chr(s1*17+s2)
i+=1
print(flag)