random module
import random
# 方法返回随机生成的一个实数,它在[0,1)范围内
print(random.random())
运行结果:
0.06435148447021877
# 方法返回随机生成的一个整数,这里包括 8
print(random.randint(1, 8))
运行结果:
1
# 返回一个列表,元组或字符串的随机项
print(random.choice('hello'))
运行结果:
l
print(random.choice(['hello', 11, [22]]))
运行结果:
[22]
# 从 list 中随机获取 2 个元素,作为一个列表返回
print(random.sample(['123', 4, [1, 2]], 2))
运行结果:
['123', [1, 2]]
# 随机获取5个验证码
def v_code():
code = ''
for i in range(5):
code += str(random.choice([random.randint(0, 9), chr(random.randint(65, 91))]))
print(code)
v_code()
运行结果:
33NSI
random module的更多相关文章
- Python从题目中学习:random() module
最近在给公司培训Python,布置了一道题: ----------------------------------------------------------------------------- ...
- python的random模块
As an example of subclassing, the random module provides the WichmannHill class that implements an a ...
- Python3模块-random、hashlib和base64
random模块 random.random()用于生成一个浮点数x,范围为0 =< x < 1 import random >>>print(random.random ...
- Random numbers
Most computer programs do the same thing every time they execute, given the same inputs, so they are ...
- python 中的metaclass和baseclasses
提前说明: class object 指VM中的class 对象,因为python一切对象,class在VM也是一个对象,需要区分class对象和 class实例对象. class instance ...
- Brief Tour of the Standard Library
10.1. Operating System Interface The os module provides dozens of functions for interacting with the ...
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习
# Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...
- 用JSON-server模拟REST API(二) 动态数据
用JSON-server模拟REST API(二) 动态数据 上一篇演示了如何安装并运行 json server , 在这里将使用第三方库让模拟的数据更加丰满和实用. 目录: 使用动态数据 为什么选择 ...
- 转:Python获取随机数(英文)
Random - Generate pseudo-random numbers Source code: Lib/random.py This module implements pseudo-ran ...
随机推荐
- Java集合list,map,set区别及遍历
1.1 List.Set.Map基本区别 1.List,Set都是继承Collection接口,Map不是. 2.List:LinkedList.ArrayList.Vector Set :HashS ...
- tp框架 php ajax 登陆
html 文件 <form class="form-signin loginform" role="form"> <input type=&q ...
- 逆袭之旅DAY20.XIA.选择结构
2018-07-16 18:50:49 本章目标: 基本if选择结构 逻辑运算符 多重if选择结构 嵌套if选择结构 什么是if选择结构: if选择结构是根据条件判断之后再做处理 import ja ...
- freemarker学习 (servlet + freemarker -> Struts2+freemarker -> springMVC+freemarker)
什么是freemarker? freemarker类似于jsp,但不是jsp!怎么说呢?freemarker文件后缀是.ftl,它不像jsp本质是servlet,它将构建模板.解析模板.使用模板分离开 ...
- 多War包合并,jetty测试
模块间的相互依赖引用配置在pom.xml中加入要依赖的模块即可<dependency> <groupId>com.exayong</groupId> & ...
- 进阶ES6 点滴认知
1.let--不允许重复声明 根据http://es6.ruanyifeng.com/#docs/let 的例子,我竟然 报格式错误 说实话,我也没见过函数这样的写法......然后我就随意加了函数名 ...
- 终止TTask.Run启动的线程
unit Unit15; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- Getting started with 3G | ip.access nano3G+OpenBSC+Osmocom-bb Part 1
English Version could be find at Osmocom.org https://osmocom.org/projects/cellular-infrastructure/wi ...
- MYSQL MyISAM与InnoDB对比
1. 区别: (1)事务处理: MyISAM是非事务安全型的,而InnoDB是事务安全型的(支持事务处理等高级处理): (2)锁机制不同: MyISAM是表级锁,而InnoDB是行级锁: (3)sel ...
- PhoneGap Vs AppCan
首先在写这篇文章前,必须先申明一下,本人是技术出身,对HTML技术及手机客户端都有过编程经验,只是出于工作岗位的变动,便没有再具体代码工作,以下文章涉及的中间件的基本代码实现及前期的API使用,都是自 ...