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

運行結果截圖:

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

代碼:
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)運行:

題目[3]:凱撒加密:

原理功能:
通過把字母移動一定的位數(shù)來實現(xiàn)加解密
明文中的所有字母從字母表向后(或向前)按照一個固定步長進行偏移后被替換成密文。
例如:當步長為3時,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))
運行:

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


題目[6]:統(tǒng)計英文句子“Life is short, we need Python."中各字符出現(xiàn)的次數(shù)。
代碼和運行結果:
#去空格,轉化為list,然后再轉化為字典
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ù)返回當前字符的個數(shù)
? ? dict[w] = c ? #針對字符w,用c表示其個數(shù),存儲在字典
print(dict) ? #輸出字典
題目[7]:輸入一句英文句子,輸出其中最長的單詞及其長度。
提示:可以使用split方法將英文句子中的單詞分離出來存入列表后處理。
代碼和運行結果:
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] ?#找到最大長度的單詞對應單詞
print("最長的單詞是:“{}” , 里面有 {} 個字母".format(C[0],maxlen))
到此這篇關于python基本數(shù)據(jù)類型介紹的文章就介紹到這了,更多相關python基本數(shù)據(jù)類型內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python中的基本數(shù)據(jù)類型講解
- Python基礎知識+結構+數(shù)據(jù)類型
- Python常用數(shù)據(jù)類型之列表使用詳解
- Python基本數(shù)據(jù)類型及內置方法
- Python數(shù)據(jù)類型及常用方法
- Python的五個標準數(shù)據(jù)類型你認識幾個
- Python語言內置數(shù)據(jù)類型
- python入門課程第四講之內置數(shù)據(jù)類型有哪些
- Python的內置數(shù)據(jù)類型中的數(shù)字
- Python中基礎數(shù)據(jù)類型 set集合知識點總結
- Python中的基本數(shù)據(jù)類型介紹
相關文章
使用Python+Flask開發(fā)博客項目并實現(xiàn)內網(wǎng)穿透
Flask是一個使用python編寫的輕量級Web框架,這篇文章我們將使用這個框架編寫一個屬于自己的博客網(wǎng)站!并教你如何通過使用內網(wǎng)穿透工具處理項目,讓本地的項目可以在公網(wǎng)訪問,感興趣的可以了解一下2021-11-11
Python學習筆記之函數(shù)的參數(shù)和返回值的使用
這篇文章主要介紹了Python學習筆記之函數(shù)的參數(shù)和返回值的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Python實現(xiàn)銀行賬戶資金交易管理系統(tǒng)
這篇文章主要介紹了Python銀行賬戶資金交易管理系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01

