美国人初学Python100个题目之七

今天来点新花样,先复制一些英文如下:
Python Program to Generate a Random Number
In this example, you will learn to generate a random number in Python.

To understand this example, you should have the knowledge of the following Python programming topics:

Python Input, Output and Import
Python Random Module

To generate random number in Python, randint() function is used. This function is defined in random module.`# Program to generate a random number between 0 and 9

import random

print(random.randint(0,9))`

This returns a number N in the inclusive range [a,b], meaning a <= N <= b, where the endpoints are included in the range.

大凡有点基础的Python学习者,都使用过,或者至少知道这个又名而实用的模块,姑且叫它随机模块,我们用的最多的可能就是从一个正整数区间随机产生一个正整数数字,这个用于很多场合,比如下棋猜先,猜数字游戏,掷色子等都可以用这个模仿。
当然,还有随机选出列表中的一个元素,代码如下:

import random
list1 = [3,5,6,2,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UncleMark编程

获取价值后的回馈,谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值