日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:創(chuàng)建值是鍵的平方的字典

創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的老河口網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

寫一個 Python 程序來創(chuàng)建鍵的字典,用一個實際的例子來說明值是鍵的平方。

創(chuàng)建鍵和值的字典的 Python 程序是鍵的平方示例 1

在這個 python 程序中,我們使用 for 循環(huán)從 1 迭代到用戶指定的值。在 Python for 循環(huán)中,我們使用指數(shù)運(yùn)算符為字典賦值。

# Python Program to Create Dictionary of keys and values are square of keys

number = int(input("Please enter the Maximum Number : "))
myDict = {}

for x in range(1, number + 1):
    myDict[x] = x ** 2

print("\nDictionary = ", myDict)

在這個 Python 的例子中,number = 5。

第一次迭代 x 將是 1:1,范圍為(1,6) myDict[x]= x 2 myDict[x]= 1 2 = 1

第二次迭代 x 將是 2:對于范圍(1,6) 中的 2,my dict[x]= 2 2 = 2

對循環(huán)迭代的剩余進(jìn)行同樣的操作

創(chuàng)建從 1 到 n 的鍵的字典的程序,值是鍵的平方示例 2

這個 python 代碼創(chuàng)建鍵和值的字典是鍵的平方是另一種方法。

# Python Program to Create Dictionary of keys and values are square of keys

number = int(input("Please enter the Maximum Number : "))

myDict = {x:x ** 2 for x in range(1, number + 1)}

print("\nDictionary = ", myDict)

將鍵字典和鍵方塊輸出為值

Please enter the Maximum Number : 6

Dictionary =  {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}
>>> 
Please enter the Maximum Number : 9

Dictionary =  {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>> 

分享題目:Python程序:創(chuàng)建值是鍵的平方的字典
文章路徑:http://www.5511xx.com/article/djgspcc.html