Python基本的函数和方法
Python基本的函数和方法
huskyKKK
略
展开
-
python-math模块
math模块导入import math模块1.abs(x)返回绝对值print(abs(-10))2.math.fabs(-10)返回浮点型的绝对值print(math.fabs(-10))3.math.ceil(x)向上取整print(math.ceil(4.5))4.math.floor(x)向下取整print(math.floor(4.5))5.round(number,ndigits=n)对number保留n位小数,四舍五入的,默认整数四舍五入print(ro原创 2020-08-19 09:41:30 · 429 阅读 · 0 评论 -
python-三角函数
三角函数导入import math模块1.cos()需借助 π,cos(45°) 应该表示成 cos(π/4)print(math.cos(math.pi/4))print( math.sqrt(2)/2 )2.sin()sin(45°) 应该表示成 sin(π/4)print( math.sin( math.pi/4 ) )3.tan()tan(45°) 应该表示成 tan(π/4)print( math.tan( math.pi/4 ) )4.math.degrees()原创 2020-08-19 14:44:47 · 3440 阅读 · 0 评论 -
python-随机数模块
随机数模块导入import random和import math模块1.random.choice(seq)从seq取一个随机数print(random.choice(range(11)))从一个list 中随机取数据names = ['A','B','张三','jack']print(random.choice(names))2.random.random()返回一个 0-1 的随机数print(random.random())取一个 1-50的随机数0-1 乘以50 0-原创 2020-08-19 14:39:40 · 224 阅读 · 0 评论 -
python-日期函数
日期函数导入模块:import time1.获取时间戳时间戳是指格林威治时间自1970年1月1日(00:00:00 GMT)至当前时间的总秒数print(time.time())2.获取时间元组localtime = time.localtime()print(localtime)3.格式化时间localtime = time.localtime()print( time.asctime())4.自定义格式化时间 str format timelocaltime = time.原创 2020-08-18 21:31:07 · 1184 阅读 · 0 评论 -
python-datetime模块
datetime模块导入from datetime import datetimeimport timeimport calendar模块1.自己创建 一个 当前时间对象nowtime = datetime.today()print(nowtime)2.创建一个 指定日期s_time = datetime(2019,10,1,8,0,0)print(s_time)print(type( s_time ) )3.其他import timeprint( time.time() )原创 2020-08-18 21:50:25 · 115 阅读 · 0 评论 -
pyhton-字符串对象的函数
字符串对象的函数1.string.capitalize( )将string对象的首字母大写print('hello'.capitalize())2.string.center( width ,‘自定的填重符号,默认空格’)string.center( width ,‘自定的填重符号,默认空格’)print('ziaoming'.center(30,'-'))string.ljust(width) 返回 string 占 width 宽度,左对齐string.rjust(width) 返回原创 2020-08-19 17:13:27 · 279 阅读 · 0 评论 -
python-str和内置函数
str和内置函数1.转义字符转义字符\,\ 在字符结尾的时,表示续行符print('"')print("'")print('\'')str1='hello'\ 'seri'print(str1)输出反斜杠print('\\ ')\b 退格 相当于按了一下 BackSapce\000 代表空 ’ ’\n 换行\t 制表符 ,就是Tab\v 纵向制表符2.格式化字符串的符号%s 格式化str类型%d 格式化int类型%f 格式化float类型原创 2020-08-19 15:03:59 · 202 阅读 · 0 评论 -
python-列表的方法
列表的方法某个班级 有个 小组,分别group = '张三,李四,王五,赵六'.split(',') # ['张三', '李四', '王五', '赵六']他们去上体育课从动漫中来个 鸣人 ,增加到队伍尾部list.append(obj) 将obj 增加打list 的结尾group.append('鸣人')print(group)鸣人,觉得不够热闹,用来分身术,一次性 又 增加 3个鸣人到结尾list.extend( seq ) 将 seq 中的对象一次性都放入 list 结尾gro原创 2020-08-20 11:40:45 · 325 阅读 · 0 评论 -
python-集合的方法
集合的方法1.set.add()set1 = {'a','b',1,2}set1.add(3)print(set1)2.set.copy()复制的是值,不是地址set2= set1.copy()set23.set.clear()set1.clear()print(set1)print(set2)4.x.difference(y)集合的差集 ,我们可用来 做 数据的不同数据的 处理x = {'a','b','c',1,2,3}y = {'a',1,'d'}print(原创 2020-08-18 20:43:45 · 189 阅读 · 1 评论 -
python-字典的方法
字典的方法1.字典转strdict1={'a':1,'b':20,'c':'300'}str1=str(dict1)print(str1)2.字符串转字典eval( str ) 会将 str 当成 python 代码执行,得到什么就是什么strdict="{'a':10,'b':200,'c':300}"dict2=eval(strdict)print(dict2)...原创 2020-08-18 20:25:23 · 126 阅读 · 0 评论