python基础

1、数学操作符

** :指数 2**3=8

%:取余数 22%8=6

// :取商 22%8=2

/: 除法 22/8=2.75

2、字符串连接

字符串+数值类型,会报错,因python不能自动完成转换,如‘alice’+42,报错can't convert 'int' object to str implicitly.  正常代码为:'alice'+str(42)

字符串*整数n,表示字符串复制n次,如'alice'*5='alicealicealicealicealice'。

如果字符串*浮点数m,则会报错can't multiply sequence by non-int of type 'float'.

因此字符串*数值n,这里的n只能是整数。

3、转换函数

str():数值转为字符串,如str(29)='29'

int():字符串转为整型,如int('29')=29

float():字符串转换为浮点型,如float('29')=29.0

4、range(start,end,step):start是开始值,end是结束值,step是步长

如range(0,10,2)表示从0到9,步长为2的数

5、退出程序 sys.exit()

6、返回值None,表示没有值,首字母必须大写

7、global:修改全局变量

8、异常处理

try:

        业务语句

except  异常:

        异常处理语句

9、数组[]

spam=['hello','hi','howdy','heyas']

spam[start:end]:start是开始下标;end是结束下标,不包含end的值。end为负数

del:删除下标处的值,如del spam[2]

remove:从列表中删除值,如spam.remove('howdy')

index():传入值返回下标,如spam.index('hello')=0

sort():列表中的排序(‘ASCII字符排序’),reverse=True表示逆序;reverse=False表示正序

10、元组()

list():转换为列表

tuple():转换为元组

11、字典{}

字典:键-值对形式

keys:字典中的键

values:字典中的值

items():字典中的键值

get():检查字典中有无该键

setdefault():给某个键设置一个默认值

如:spam={‘name’:'pooka','age':5}

spam.setdefault('color','white')

12、漂亮打印pprint

pprint():打印

如:

import ppint

message='It was a bright cold day in April,and the clocks were striking thirteen.'

count={}

for character in message:

        count.setdefault(character,0)

        count[character]=count[character]+1

pprint.pprint(count)

13、原始字符串

字符串开始前加r,成为原始字符串,如print(r'That is Carol\'s cat.')  

打印That is Carol\'s cat.

14、字符串函数

startswith():以某字符串开始

endswith():以某字符串结束

rjust(数值n):右对齐,左侧插入空格,如'hello'.rjust(2),打印'  hello'

rjust(数值n,字符):右对齐,左侧插入指定字符,如'hello'.rjust(2,'*'),打印'**hello'

ljust():左对齐,右侧插入空格,如'hello'.ljust(2),打印'hello  '

center():居中对齐,左右侧插入空格,如'hello'.center(2),打印'  hello  '

strip():删除左右侧空格

rstrip():删除右侧空格

lstrip():删除左侧空格

15、剪贴板pyperclip

copy():复制

paste():粘贴

import pyperclip

pyperclip.copy('hello world')

pyperclip.paste()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值