python3基礎之「小練習(2)」

2022-08-18 23:06:23 字數 2622 閱讀 2671

(十三)建立乙個你最喜歡歌手的列表。
1

#singer=list()2#

singer=['a','b','c']3#

print(singer)

(十四)建立乙個由元組構成的列表,每個元組包含居住過或旅遊過的城市的經緯度。

1

#s=tuple('1.1','2.2','3.3')2#

print(s)

(十五)建立乙個包含你的不同屬性的字典:身高、最喜歡的顏色和最喜歡的作者等。

1

#zi_dian=4#

print(zi_dian)

(十六)編寫乙個程式,讓使用者詢問你的身高、最喜歡的顏色或最喜歡的作者,並返回上乙個挑戰中建立的字典。

1

#zi_dian=4#

print(zi_dian["height"])5#

print(zi_dian["color"])6#

print(zi_dian["author"])7#

print(zi_dian)

(十七)建立乙個字典,將最喜歡的歌手對映至你最喜歡的歌曲。

1

#singer=3#

print("singer")

(十八)列表、元組和容器只是python 中內建容器的一部分。自行研究python 中的集合.(也是一種容器)在什麼情況下可以使用集合?

:思考一下,集合與list,tuple的區別

(十九)列印字串"camus"中的所有字元。

1

#v="camus"2#

print(v[0])3#

print(v[1])4#

print(v[2])5#

print(v[3])6#

print(v[4])

(二十)編寫程式,從使用者處獲取兩個字串,將其插入字串"yesterday i wrote a[使用者輸入1]. i sent it to [使用者輸入2]!"中,並列印新字串。

1

#c=input("type a str:")2#

d=input("type a str:")3#

a="yesterday i wrote a {}. i sent it to {}!".format(c,d)4#

print(a)

(二十一)想辦法將字串"aldous huxley was born in 1894."的第乙個字元大寫,從而使語法正確。

1

#a="aldous huxley was born in 1894"2#

v=a.capitalize()3#

print(v)

(二十二)對字串"where now? who now? when now?"呼叫乙個方法,返回如下述的列表["where now", "who now", "when now"]。

1

#a="where now? who now? when now? "2#

v=a.split("?")3#

print(v)

(二十三)對列表["the", "fox", "jumped", "over", "the", "fence", "."]進行處理,將其變成乙個語法正確的字串。每個單詞間以空格符分隔,但是單詞fence 和句號之間不能有空格符。(別忘了,我們之前已經學過將字串列表連線為單個字串的方法。)

1

#s=["the", "fox", "jumped", "over", "the", "fence", "."]2#

v=" ".join(s)3#

v=v.strip()4#

print(v)

這是錯誤的方法!

1

#第一種方法

2 fox = ["

the", "

fox", "

jumped

", "

over

", "

the", "

fence

", "."

]3 fox = "

".join(fox)

4 fox = fox[0: -2] + "."

5print

(fox)67

#第二種方法

8 lists = ["

the", "

fox", "

jumped

", "

over

", "

the", "

fence

", "."

]9 f = '

'.join(lists).title()

10 x = f.replace('

.','.'

)11print

(x)1213#

在此感謝的指正

如有錯誤,歡迎指正!

python3 基礎小練習 判斷閏年

一 方法1 while true try year int input 請輸入乙個年份 if year 4 0and year 100 0 or year 400 0 print 是閏年。format year else print 不是閏年。format year break except val...

Python3 函式小練習

1 列印名片程式 輸入姓名,號碼,性別,最後列印出來名片 控制姓名長度為6 20 號碼長度11 性別只能允許輸入男或女 每一樣資訊不允許為空 一 三個迴圈分開寫 deffunc1 while true name input 請輸入姓名,長度6 20 if len name 6and len name...

Python 3個小練習

這裡指定變數和相關要求,只需要根據所給表進行資料整理即可 set highjump set longjump set intersection set highjump.intersection set longjump set union set highjump.union set longju...