Python
2025-12-27 13:42:36
发布于:浙江
1.变量:
Python的变量不需要声明,下面是对比
#include<bits/stdc++.h>
using namespace std;
int a=1;
a=1
2.输出:
Python的输出用print函数无需引进类似iostream的库
例子
print("Hello,world")
输出:
Hello,world
3.输入:
Python用input()实现,例子:
a=input()
但input()输入的是字符串,想要其它类型,需要用int()、float()等转换
4.指针:
Python没有指针,但有一个id()函数
例如:
a=5
print(id(a))
输出的是十进制,如果想像C++一样,可以这样:
a=5
print(hex(id(a)))
4.模块:
Python的模块使用import引进,如:
import math
5.数学函数:
import math
math.pi:
math.e:
math.sin(x):
math.cos(x):
math.asin(x):
math.tan(x):
math.log(x,base=math.e):
math.inf:∞
math.sqrt(x):
6.随机函数:
import random
random.randint(a,b):随机a-b
random.randrange(start,stop,step):随机start-stop中公差为step的整数
7.数组
Python没有数组,但有list类型:
a=[1,3.14,"hello",[1,2,3]]
print(a)
print(type(a))
[1,3.14,"hello",[1,2,3]]
<class 'list'>
8.安装第三方库:
在cmd里输入:
pip install 第三方库名
9.时间:
import time
time.time():当前时间从Python发明以来过了多少秒
time.strftime("%Y年%m月%d日 %H:%M:%S")
time.sleep(s)程序暂停s秒
10.list的方法:
a=[1,2,3]
a.append(4)#a变成[1,2,3,4]
a.extend([5,6])#a->[1,2,3,4,5,6]
a.append(3)
a.sort()#a->[1,2,3,3,4,5,6]
11.注释:
Python单行注释用#,多行注释用'''...'''
如:
#pi=3.1415926
'''
3323244
pi=4?
pi!=4!
'''
12.运算符:
a+b:
a-b:
a*b:
a/b:
a**b:
a%b:
a//b:
b1 and b2:b1&&b2
b1 or b2:b1||b2
not b:!b
13.自定义函数:
def funcname():
...
return ...
def funcname(arg1):#可以有多个参数
...
return ...#不一定要这条语句
举一个例子:
def add(a,b):
return a+b
a=int(input())
b=int(input())
print(add(a,b))
14.分支、循环:
if b1:
...
[elif b2:...]
[else:...]
while b1:
...
for i in arr:
...
你现在已经会Python了,快去试试吧!
这里空空如也









有帮助,赞一个