Python基础(3)标准库函数

Python基础(3)标准库函数

数字运算和数学函数math

num = -5
print(num)
print(abs(num))       #绝对值abs() = 5
print(pow(5,2))       #次方^pow() = 25
print(5**2)           #次方,同上 = 25
print(max(7,9,5))     #which is higher = 9
print(round(3.8),"\n")#四舍五入 = 4

from math import *
print(floor(3.8))     #向下取整 = 3
print(ceil(3.8))      #向上取整 = 4
print(sqrt(36),"\n")  #开根号 = 6.0(保留一位小数)

import math
print(math.cos(math.pi/4))    #计算cos(pi/4)
print(math.log(1024,2),"\n")  #计算log2(1024) = 10
-5
5
25
25
9
4 

3
4
6.0 

0.7071067811865476
10.0 

随机函数random

import random
p = ["a","b","c","d","e","f"]
print(p)
random.shuffle(p)        #打乱p元素顺序
print(p)
print(random.choice(p))             #随机选取p中一个元素
print(random.random())              #随机输出0~1的浮点数
print(random.uniform(1,10))         #随机输出1~10的浮点数
print(random.randrange(0, 101, 2))  #随机选取0~100的偶数
print(random.randint(50,100))       #随机生成50~100整数
print(random.sample(range(100),10)) #随机输出0-99中10个数字
['a', 'b', 'c', 'd', 'e', 'f']
['f', 'd', 'b', 'e', 'a', 'c']
c
0.08818925172489311
8.197367177893891
94
92
[59, 12, 88, 9, 79, 34, 44, 61, 33, 26]

日期和时间time

from datetime import date
now = date.today()
print(now)      #2019-01-28

import time
print(time.time())#时间戳
print(time.localtime(time.time()))
print(time.asctime(time.localtime(time.time())))#格式化时间
print(time.strftime('%Y-%m-%d %H:%M:%S %w-%Z',time.localtime()))
                      #完整日期   时间   星期时区
print("running time is ",time.perf_counter(),"s")
        #程序运行时间,或者 time.process_time()
2019-01-28
1548666578.0288482
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=28, tm_hour=17, tm_min=9, tm_sec=38, tm_wday=0, tm_yday=28, tm_isdst=0)
Mon Jan 28 17:09:38 2019
2019-01-28 17:09:38 1-CST
running time is  0.084361643 s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值