python基本數(shù)據(jù)類型練習(xí)題
題目[1]:格式輸出練習(xí)。在交互式狀態(tài)下完成以下練習(xí)。

運(yùn)行結(jié)果截圖:

題目[2]:格式輸出練習(xí)。在.py的文件中完成以下練習(xí)

代碼:
num = 100
print('%d to hex is %x' % (num,num))
print('%d to hex is %X' % (num,num))
print('%d to hex is %#x' % (num,num))
print('%d to hex is %#X' % (num,num))
from math import pi
print('value of Pi is: %.4f' % pi)
students = [{'name':'zhangsan','age':20},
? ? ? ? ? ?{'name': 'lisi', 'age': 19},
? ? ? ? ? ?{'name': 'wangwu', 'age': 19}]
print('name: %10s, age: %10d' % (students[0]['name'],students[0]['age']))
print('name: %-10s, age: %-10d' % (students[1]['name'],students[1]['age']))
print('name: %10s, age: %10d' % (students[2]['name'],students[2]['age']))
for student in students:
? ? print('%(name)s is %(age)d years old' % student)運(yùn)行:

題目[3]:凱撒加密:

原理功能:
通過把字母移動一定的位數(shù)來實(shí)現(xiàn)加解密
明文中的所有字母從字母表向后(或向前)按照一個(gè)固定步長進(jìn)行偏移后被替換成密文。
例如:當(dāng)步長為3時(shí),A被替換成D,B被替換成E,依此類推,X替換成A。
代碼:
import string #ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' #ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def kaisa(s, k): ? ? lower = string.ascii_lowercase ? ? upper = string.ascii_uppercase ? ? before = string.ascii_letters ? ? after = lower[k:] + lower[:k] + upper[k:] + upper[:k] ? ? table = ''.maketrans(before,after) ? ? return s.translate(table) s = 'Python is a great programming language. I like it!' print(kaisa(s,3))
運(yùn)行:

- 1)用字典記錄下其豆瓣評分,并輸出字典;
- 2)現(xiàn)又新出了兩部影片及其評分(中國機(jī)長: 7.0,銀河補(bǔ)習(xí)班: 6.2),將此影評加入1)中的字典中,同時(shí)輸出字典中所有的影片名稱。
- 3)現(xiàn)找出2)中的字典中影評得分最高的影片。
代碼和運(yùn)行結(jié)果:
1>
films = {'肖申克的救贖':9.7, '摔跤吧!爸爸':9.0,
? ? ? ? ?'阿甘正傳':9.5,'我和我的祖國':8.0,
? ? ? ? ?'哪吒之魔童降世':8.5, '千與千尋':9.3,
? ? ? ? ?'瘋狂動物城':9.2,'攀登者':6.5}
print(films)
2>
films_new = {'中國機(jī)長':7.0,'銀河補(bǔ)習(xí)班':6.2}
films.update(films_new) #字典中元素的插入 dict.update()函數(shù)
print("所有影片名稱: ", films.keys())
題目[5]:編程實(shí)現(xiàn):生成2組隨機(jī)6位的數(shù)字驗(yàn)證碼,每組10000個(gè),且每組內(nèi)不可重復(fù)。輸出這2組的驗(yàn)證碼重復(fù)個(gè)數(shù)。
代碼和運(yùn)行結(jié)果:
import random
code1 = [] ? #存儲校驗(yàn)碼列表
code2 = []
t = 0 ? #標(biāo)志出現(xiàn)重復(fù)校驗(yàn)碼個(gè)數(shù)
dict={}
#第一組校驗(yàn)碼
for i in range(10000):
? ? x = ''
? ? for j in range(6):
? ? ? ? x = x + str(random.randint(0, 9))
? ? code1.append(x) ? # 生成的數(shù)字校驗(yàn)碼追加到列表
#第二組校驗(yàn)碼
for i in range(10000):
? ? x = ''
? ? for j in range(6):
? ? ? ? x = x + str(random.randint(0, 9))
? ? code2.append(x) ? # 生成的數(shù)字校驗(yàn)碼追加到列表
#找重復(fù)
for i in range(len(code1)):
? ? for j in range(len(code2)): ? # 對code1和code2所有校驗(yàn)碼遍歷
? ? ? ? if (code1[i] == code2[j]):
? ? ? ? ? ? t = t+1 ? ?#如果存在相同的,則t+1
? ? if t > 0:
? ? ? ? dict[code1[i]] = t ?# 如果重復(fù)次數(shù)大于0,用t表示其個(gè)數(shù),存儲在字典
#輸出所有重復(fù)的校驗(yàn)碼及其個(gè)數(shù)
for key in dict:
? ? print(key + ":" + str(dict[key]))截取幾張:


題目[6]:統(tǒng)計(jì)英文句子“Life is short, we need Python."中各字符出現(xiàn)的次數(shù)。
代碼和運(yùn)行結(jié)果:
#去空格,轉(zhuǎn)化為list,然后再轉(zhuǎn)化為字典
str = ?'Life is short, we need Python.'
list = []
list2 = []
dict={}
i= 0
for w in str:
? ? if w!=' ':
? ? ? ? list.append(w)
#將str字符串的空格去掉放在list列表
for w in list:
? ? c = list.count(w) ? #用count()函數(shù)返回當(dāng)前字符的個(gè)數(shù)
? ? dict[w] = c ? #針對字符w,用c表示其個(gè)數(shù),存儲在字典
print(dict) ? #輸出字典
題目[7]:輸入一句英文句子,輸出其中最長的單詞及其長度。
提示:可以使用split方法將英文句子中的單詞分離出來存入列表后處理。
代碼和運(yùn)行結(jié)果:
test0 ?= 'It is better to live a beautiful life with all one''s ' \
? ? ? ? ?'strength than to comfort oneself with ordinary and precious things!.'
test1 = test0.replace(',','').replace('.','') ?#用空格代替句子中“,”的空格和“。”
test2 = test1.split () ? #將英文句子中的單詞分離出來存入列表
maxlen = max(len(word) for word in test2) ? #找到最大長度的單詞長度值
C=[word for word in test2 if len(word)== maxlen] ?#找到最大長度的單詞對應(yīng)單詞
print("最長的單詞是:“{}” , 里面有 {} 個(gè)字母".format(C[0],maxlen))
到此這篇關(guān)于python基本數(shù)據(jù)類型介紹的文章就介紹到這了,更多相關(guān)python基本數(shù)據(jù)類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中的基本數(shù)據(jù)類型講解
- Python基礎(chǔ)知識+結(jié)構(gòu)+數(shù)據(jù)類型
- Python常用數(shù)據(jù)類型之列表使用詳解
- Python基本數(shù)據(jù)類型及內(nèi)置方法
- Python數(shù)據(jù)類型及常用方法
- Python的五個(gè)標(biāo)準(zhǔn)數(shù)據(jù)類型你認(rèn)識幾個(gè)
- Python語言內(nèi)置數(shù)據(jù)類型
- python入門課程第四講之內(nèi)置數(shù)據(jù)類型有哪些
- Python的內(nèi)置數(shù)據(jù)類型中的數(shù)字
- Python中基礎(chǔ)數(shù)據(jù)類型 set集合知識點(diǎn)總結(jié)
- Python中的基本數(shù)據(jù)類型介紹
相關(guān)文章
Python類中的裝飾器在當(dāng)前類中的聲明與調(diào)用詳解
這篇文章主要介紹了Python類中的裝飾器在當(dāng)前類中的聲明與調(diào)用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python實(shí)現(xiàn)比較兩個(gè)列表(list)范圍
這篇文章主要介紹了Python實(shí)現(xiàn)比較兩個(gè)列表(list)范圍,本文根據(jù)一道題目實(shí)現(xiàn)解決代碼,本文分別給出題目和解答源碼,需要的朋友可以參考下2015-06-06
Pandas修改DataFrame列名的兩種方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Pandas修改DataFrame列名的兩種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Pandas具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
PyCharm如何配置SSH和SFTP連接遠(yuǎn)程服務(wù)器
這篇文章主要介紹了PyCharm如何配置SSH和SFTP連接遠(yuǎn)程服務(wù)器,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

