Python基础1

基本语法

1、for循环语句

for i in range(n):

2、判断语句

if i:

elif j:

else:

3、while循环语句

while i:

4、字符串格式化,槽;

5、and or not;//与,或,非

6、异常处理

try:
  
  except:

else:

  finally:

7、全局变量

global n

常用函数

turtle

1、停止画笔,且不关闭画布

import turtle
turtle.done()

2、画笔抬起

import turtle
turtle.pu()
#turtle.penup()两种写法效果相同

3、画笔落下

import turtle
turtle.pd()
#turtle.pendown()两种写法效果相同

4、画笔宽度

import turtle
turtle.pensize()
#turtle.width()两种写法效果相同

5、画笔颜色

import turtle
turtle.pencolor("blue") #参数为字符串,RGB的小数值,RGB的元组值

6、画布大小和初始位置

import turtle
turtle.setup(500, 500, 0, 0) #长度,宽度,横坐标(左上角),纵坐标(左上角)
turtle.done()

7、画笔前进后退

import turtle
turtle.fd(100)#正值为前进,负值为后退
#turtle.forward(100)两种方法效果一样
turtle.done()

8、画笔弧形运动,默认圆心在画笔左侧半径距离

import turtle
turtle.circle(100, 360,3) #参数为半径、角度和所画弧线的边数,角度默认为360度,边数默认为一边形即圆弧
turtle.done()

9、画笔转向

import turtle
turtle.seth(90) #参数为绝对角度,逆时针,不前进
#turtle.setheading(90)两种方法效果一样
turtle.done()

10、画笔左转右转

import turtle
turtle.left(45) #左转,参数为绝对角度,逆时针,不前进
turtle.right(45) #右转
turtle.done()

divmod

同时输出商和余数

print(divmod(10, 8))

pow

pow(x,y[,z]) #x的y次方,可对z取模

range

range(N) #产生0到N-1的整数序列,共N个
range(M,N) #产生M到N-1的整数序列,共N-M个

round

round(x[,d]) #四舍五入,d可控制位数

max,min

max(a1,a2,a3…an) #取最大
min(b1,b2,b3…bn) #取最大

类型转换

int(x)
float(y)
complex(z)
str(a)
hex(b)
oct(c)
chr(d)
ord(e)
str(f)

abs

abs(x) #取绝对值

字符串

1、str.lower(),str.upper();//转为小写或大写

2、str.split();//分隔

3、str.count();//计数

4、str.replace(old,new);//替换

5、str.center();//居中

6、str.strip();//去除两侧字符

7、str.join();//增加字符

8、len();//统计长度

time

1、time.time();//获得一个时间(秒),从1970年1月1日0:0:0开始到现在;

2、time.ctime();//简易时间

3、time.gmtime();//获得计算机友好的时间表示

4、time.stiftime();//时间格式化 %Y//年份 %m//月份 %b//月份名称 %B//月份名称缩写 %d//日期 %A//星期 %a//星期缩写 %H//24小时制 %h//12小时制 %p//上或下午 %M//分钟 %   S//秒

5、time.strptime();//将字符串变成时间

6、time.perf_counter();//程序计时,计算差值

7、time.sleep();//休眠

randon

1、random.seed();//随机数种子

2、random.random();//产生随机数

3、random.randint();//产生整数

4、random.randrange();

5、random.getrandbits();//生成一个K比特长的随机整数

6、random.uniform();//生成随机小数

7、random.choice();//从序列中随机选择一个元素

8、random.shuffle();//打乱排序

lambda

定义函数

PyInstaller

安装:cmd->pip install pyinstaller

使用:cmd->pyinstaller -F <文件名.py>

1、-h;//帮助

2、--clean;//清除无用文件

3、-D,--onedir;//默认值,生成dist文件夹

4、-F,--onefile;//在dist文件夹中只生独立的打包文件

5、-i <图标文件名.ico>;//指定打包文件所用图标文件;

集合类型

1、set();//建立集合

2、s.add();//增加元素

3、s.discard();//移除元素

4、s.remove();//同上,会报错

5、s.clear();//移除所有元素

6、s.pop();//随机取出一个元素

7、s.copy();//复制

8、len(s);//集合数目

9、x (not)in s;//判断

序列类型

1、x (not)in s;//判断

2、s+t;//连接

3、s*n(n*s);//复制

4、len(s);//长度

5、min(s)max(s);

6、s.index();//第一次出现字符的位置

7、s.count();//计数

元组类型

(),Tuple();//定义,其他同序列

文件

1、open(文件名,打开模式);

2、.close();//关闭文件

3、.read();//读取全部文件

4、.readline();//读取一行文件

5、.readlines();//读取多行

列表类型

1、list();//定义

2、del s[];//删除

3、s*=n;//重复

4、s.append();//增加

5、s.clear();//删除

6、s.copy();//复制

7、s.insert();//插入

8、s.pop();//取出元素

9、s.remove();//删除第一个元素

10、s.reserve();//反转

字典类型

1、dict();//定义

2、del s[];//删除

3、x in s;//判断

4、d.keys();//返回键的信息

5、s.values();//返回值的信息

6、s.items();//返回键值对信息

7、s.get();//返回键值

8、s.pop();//取出键值

9、s.popitem();//随机取出

10、s.clear();//删除所有;

11、len(s);//计数

jieba库

1、安装:cmd->pip install jieba

2、jieba.lcut(s);//精确模式

3、jieba.lcut(s,cut_all = true);//全模式

4、jieba.lcut_for_search(s);//搜索引擎模式

5、jieba.add_word(s);//增加分词

type

返回其类型

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值