【Python】随机数和 列表 python3随机数
off999 2024-12-20 17:55 17 浏览 0 评论
Python 随机模块方法
randint(a, b):
返回介于 a 和 b 之间(含两者)的随机整数。如果 a > b,这也会引发 ValueError。
import random
random_int = random.randint(1,10)
print(random_int)
>>
PS \Python> python .\random_int.py
4
PS \Python> python .\random_int.py
10
PS \Python> python .\random_int.py
3
可以创建自己的模块并导入它。
#my_module.py
pi = 3.1415
#random_int.py
import random
import my_module
random_int = random.randint(1,10)
print(random_int)
print(my_module.pi)
>>
PS \Python> python .\random_int.py
8
3.1415
生成 Random 浮点数:
与生成整数类似,也有一些函数可以生成随机浮点序列。
- random.random() -> 返回介于 [0.0 到 1.0] 之间的下一个随机浮点数
- random.uniform(a, b) -> 返回一个随机浮点数 N,使得 a <= N <= b 如果 a <= b,b <= N <= a 如果 b < a。
- random.随机。expovariate(lambda) -> 返回与指数分布对应的数字。
- random.gauss(mu, sigma) -> 返回与高斯分布相对应的数字。
其他分布也有类似的函数,例如 Normal Distribution、Gamma Distribution 等。
import random
random_int = random.randint(1,10)
print(random_int)
random_flot = random.random()
print(random_flot)
>>
1
0.041635438063850505
项目 1:爱情评分
import random
print("\n")
print("-------Welcome to Love Calculator-------\n")
name1 = input("What is your name?\n")
name2 = input("What is their name?\n")
love_score = random.randint(1, 100)
print(f"Love score of {name1} and {name2} is {love_score}")
>>
-------Welcome to Love Calculator-------
What is your name?
Lisa
What is their name?
Jack
Love score of Lisa and Jack is 34
项目 2:正面或反面
import random
random_side = random.randint(0, 1)
if random_side == 1:
print ("Heads")
else:
print("Tails")
>>
PS \Python> python.exe .\heads_tails.py
Tails
PS \Python> python.exe .\heads_tails.py
Heads
列表:
检查此列表 : 数据结构
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
>>
pear
apple
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
fruits[2] = "Cheery" #CHNAGE NAME
print(fruits)
>>
pear
apple
['orange', 'apple', 'Cheery', 'banana', 'kiwi', 'apple', 'banana']
可以将其附加到列表中:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.append("Nid")
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid']
扩展列表:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.extend(["AP", "gy"])
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid', 'AP', 'gy']
检查列表的长度
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(len(fruits))
项目 3:谁来支付
names = input("Enter the names\n")
name_list = names.split(", ")
import random
num_item = len(names)
random_choice = random.randint(0, num_item - 1)
print(names[random_choice])
嵌套列表
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
vegetables = ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']
fridge = [fruits, vegetables]
print(fridge)
>>
[['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'], ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']]
项目 4:藏宝图
line1 = [" ", " "," "]
line2 = [" ", " "," "]
line3 = [" ", " "," "]
map = [line1, line2, line3]
print("\nHiding your treasure! X marks the spot.")
print("There are three rows and coloumes\n")
postion = input("Where do you want to put the treasure?")
letter = postion[0].lower()
abc = ["a", "b", "c"] #Possiable entry
letter_index = abc.index(letter)
#Checking if in "letter" (we entry), that is there in the list or not
#This will give us number
number_index = int(postion[1]) - 1
#Whatever position we entered, -1 because list stars from [0]
map[number_index][letter_index] = "X"
print(f"{line1}\n{line2}\n{line3}")
>>
Hiding your treasure! X marks the spot.
There are three rows and coloumes
Where do you want to put the treasure?B3
[' ', ' ', ' ']
[' ', ' ', ' ']
[' ', 'X', ' ']
项目 5:石头剪刀布游戏
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
game_images = [rock, paper, scissors]
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))
if user_choice >= 3 or user_choice < 0:
print("Number is Invalid, You lose!")
else:
print(game_images[user_choice])
computer_choice = random.randint(0, 2)
print("Compute Choose:")
print(game_images[computer_choice])
if user_choice == computer_choice:
print ("It's a tie!")
elif (user_choice == 0 and computer_choice == 2) or \
(user_choice == 1 and computer_choice == 0) or \
(user_choice == 2 and computer_choice == 1):
print ("You win!")
else:
print ("You lose!")
>>
What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.
2
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Compute Choose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
It's a tie!
相关推荐
- 用Python编制生成4位数字字母混合验证码
-
我们登录一些网站、APP的时候经常会有验证码,这个为了防止有人不停的去试探密码,还有发送短信验证之前,输入验证码就可以减少误点,错误操作等等。可以提高安全性,我们可以生成数字,也可以生成字母,也可...
- Python电子发票管理工具4:前后端业务逻辑实现
-
用一系列文章介绍如何用python写一个发票管理小工具。在前面的文章中前端页面和后端框架已经实现,本文将介绍功能实现的代码。数据库操作使用sqlalchemy操作sqlite数据库。sqlalchem...
- 【代码抠图】4行Python代码帮你消除图片背景
-
在修图工具满天飞的年代其实仍然还有很多人不会扣图(比如我),在很多需要去除某些照片上面的背景的时候就会很难受,所以今天就给不会扣图的小伙伴们来带一个简单的代码扣图教程,只需要4行代码,不用再多了。准备...
- Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
Python3.14重磅更新!UUIDv6/v7/v8强势来袭,别再用uuid4()啦!为什么说UUID升级是2025年Python开发者的必学技能?在当今互联网应用中,UU...
- 殊途同归 python 第 4 节:有趣的键值对(字典)
-
字典数据的突出特点就是“键”和“值”,前文已经简单介绍过,本文来聊聊关于字典的几个高级玩法。1.函数打包后,通过键来调用globalf1,f2a={"k1":f1,"k2...
- 更有效地使用 Python Pandas 的 4 个技巧
-
一个简单而实用的指南照片由simonsun在Unsplash上拍摄Pandas是一个用于数据分析和操作任务的非常实用且功能强大的库。自2019年以来,我一直在使用Pandas,它始终能够为我...
- 4.python学习笔记-集合(python里面集合)
-
1.关于集合集合是一类元素无序不重复的数据结构,常用场景是元素去重和集合运算。python可以使用大括号{}或者set()函数创建集合,如果创建一个空集合必须用set()而不是{},因为{}是用来表示...
- python生成4种UUID(python随机生成uuid)
-
总结了一份python生成4种UUID的代码:UUID用4种uuid生成方法:uuid1:基于时间戳由MAC地址、当前时间戳、随机数字。保证全球范围内的唯一性。但是由于MAC地址使用会带来安全问题...
- 你不知道的4种方法:python方法绘制扇形
-
1说明:=====1.1是问答中的我的一个回答。1.1因为问答中没有代码块的,所以我改为这里写文章,然后链接过去。1.24种方法:turtle法、OpenCV法、pygame法和matplot...
- 30天学会Python编程:4. Python运算符与表达式
-
4.1运算符概述4.1.1运算符分类Python运算符可分为以下几大类:4.1.2运算符优先级表4-1Python运算符优先级(从高到低)运算符描述示例**指数2**3→8~+-按位取...
- 这3个高级Python函数,不能再被你忽略了
-
全文共1657字,预计学习时长3分钟Python其实也可以带来很多乐趣。重新审视一些一开始并不被人们熟知的内置函数并没有想象中那么难,但为什么要这么做呢?今天,本文就来仔细分析3个在日常工作中或多或少...
- beautifulSoup4,一个超实用的python库
-
一.前言我们在学习python爬虫的时候,数据提取是一个常见的任务。我们一般使用正则表达式,lxml等提取我们需要的数据,今天我们介绍一个新的库beautifulSoup4,使用它您可以从HTML和...
- AI指导:打造第一个Python应用(4)(python ai开发)
-
眼瞅着迈过几个里程碑,与目标越来越近。尽管过程中照旧因返工而心焦,而欣喜与急躁比例,是喜悦运大于焦虑。从初次熟悉智能大模型,尝试编程起步,不定期进行复盘反思,这是小助手指导编程的第四篇。复盘以为记。需...
- wxPython 4.2.0终于发布了(wxpython安装教程)
-
wxPython是Python语言的跨平台GUI工具包。使用wxPython,软件开发人员可以为他们的Python应用程序创建真正的本地用户界面,这些应用程序在Windows、Ma...
- 《Python学习手册(第4版)》PDF开放下载,建议收藏
-
书籍简介如果你想动手编写高效、高质量并且很容易与其他语言和工具集成的代码,本书将快速地帮助你利用Python提高效率。本书基于Python专家的流程培训课程编写,内容通俗易懂。本书包含很多注释的例子和...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)