02-python学习之路,02-python之路,python关键字i


python关键字

import keyword

print(keyword.kwlist)

[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

变量类型

  type(变量名 )

  isintance(变量名,类型)

python基本数学函数

max() 求最大值

min() 求最小值

pow() 求n次方 pow(a, b)

round(float) 四舍五入 round(float, n)

import math

help(math) 查看math库的基本方法

例子:

math.ceil(float) 向上取整

math.floor(float) 向下取整

math.modf(float) 返回整数部分与小数部分

math.sqrt(n) 开方

随机数

import random

基本例子:

random.choice(seq) # 从序列中随机选取一个元素 seq: list tuple string

random.randint(1, 10) # 生成1到10的一个整数型随机数

random.random() # 生成0到1之间的随机浮点数 [0, 1)

random.randrange(a, b, n) # 生成从a到b的间隔为n的随机整数 random.randrange([start,] stop[, step])

readom.shuffle(seq) # 将序列seq的元素顺序打乱 sep: list tuple

random.uniform(float1, float2) # 生成 float1 到 float2 之间的随机浮点数,区间可以不是整数 [f1, f2]

python 中 / 与 // 的区别

" / " 表示浮点数除法,返回浮点结果

" // " 表示整数除法,返回一个不大于结果的一个最大整数

格式化输出

占位符

%d

%s

%f

转义字符

\

\n

\t

\r

\\

python 用r‘ ‘ 默认内部的字符串不转义

r‘ \\\\t\n\\‘ windows下路径 re

字符串基本方法

str = eval(str) 将字符串str当成有效的表达式来求值并返回结果

len(str) 返回字符串的长度

str.upper() 将字符串的小写字母转换为大写字母

str.lower()将字符串的大写字母转换为小写字母

str.swapcase() 翻转字符串的大小写

str.capitalize() 将字符串首字母大写, 其它小写

str.title() 每个单词的首字母大写

str.center(width, fillchar) 返回一个指定宽度的居中字符串, fillchar为填充的字符串

str.ljust(width, fillchar)

str.replace()

str.strip()

str.split()

02-python学习之路

评论关闭