【Python】随机数和 列表

摘要:random.random-> 返回介于 [0.0 到 1.0] 之间的下一个随机浮点数random.uniform(a, b) -> 返回一个随机浮点数 N,使得a

randint(a, b):

返回介于 ab 之间(含两者)的随机整数。如果 a > b,这也会引发 ValueError。

import randomrandom_int = random.randint(1,10)print(random_int)>>PS \Python> python .\random_int.py4PS \Python> python .\random_int.py10PS \Python> python .\random_int.py3

可以创建自己的模块并导入它。

#my_module.pypi = 3.1415#random_int.pyimport randomimport my_modulerandom_int = random.randint(1,10)print(random_int)print(my_module.pi)>>PS \Python> python .\random_int.py83.1415

生成 Random 浮点数:

与生成整数类似,也有一些函数可以生成随机浮点序列。

random.random -> 返回介于 [0.0 到 1.0] 之间的下一个随机浮点数random.uniform(a, b) -> 返回一个随机浮点数 N,使得 a 如果 a 如果 b random.随机。expovariate(lambda) -> 返回与指数分布对应的数字。random.gauss(mu, sigma) -> 返回与高斯分布相对应的数字。

其他分布也有类似的函数,例如 Normal Distribution、Gamma Distribution 等。

import randomrandom_int = random.randint(1,10)print(random_int)random_flot = random.randomprint(random_flot)>>10.041635438063850505import randomprint("\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?LisaWhat is their name?JackLove score of Lisa and Jack is 34import randomrandom_side = random.randint(0, 1)if random_side == 1: print ("Heads")else: print("Tails")>>PS \Python> python.exe .\heads_tails.pyTailsPS \Python> python.exe .\heads_tails.pyHeads

检查此列表 : 数据结构

fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']print(fruits[2])print(fruits[-2])>>pearapplefruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']print(fruits[2])print(fruits[-2])fruits[2] = "Cheery" #CHNAGE NAMEprint(fruits)>>pearapple['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))names = input("Enter the names\n")name_list = names.split(", ")import randomnum_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].lowerabc = ["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 numbernumber_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 coloumesWhere do you want to put the treasure?B3[' ', ' ', ' '][' ', ' ', ' '][' ', 'X', ' ']import randomrock = ''' ' ____) (_____) (_____) 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 >What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.2 ' ____)____ ______) ) (____)---.__(___)Compute Choose: ' ____)____ ______) ) (____)---.__(___)It's a tie!

来源:自由坦荡的湖泊AI一点号

相关推荐