第一周作業(rayco)

rayco 第一周作業

第一次-課后習題

a = 10
b = 3

c = a/b-a
print(c, type(c))

c = a/b*a
print(c, type(c))

c = 0.1*a//b-a
print(c, type(c))

c = a//b+a%b
print(c, type(c))

第二次-字符串練習題

一、定義字符串變量

1.請定義三個字符串a,b,c值分別為 I,like, python

2.請將上面三個變量合并輸出 'I like python'

a = 'I'
b = 'like'
c = 'python'
d = a+' '+b+' '+c
print(d)

二、定義一個變量 s= ' sdghHhf '

1.請先將變量s的空白符去掉 賦值給新變量s1 打印輸出

2.請分別將s1變為全部大寫(命名s2),小寫(命名s3),打印輸出s2,s3

3.請查找s1中h最先出現的位置 賦值給s4 打印輸出

s = ' sdghHhf '
s1 = str.strip(s)
print('s1=', s1)
s2 = str.upper(s1)
s3 = str.lower(s1)
print('s2=', s2)
print('s3=', s3)
s4 = s1.find('h')
print('s4=', s4)

三、定義一個變量x='I {} pyhon'

請用兩種方法將x中的字符串{}修改為 like 并分別賦值給兩個變量x1,x2 打印輸出

x = 'I {} pyhon'
x1 = x.replace('{}', 'like')
x2 = x.format('like')
print(x1)
print(x2)

四、定義一個變量capital='人民幣100萬元'

1.請打印一下capital的長度

2.請用python語言判斷capital是否是數字

capital = '人民幣100萬元'
print(len(capital))
print(capital.isdigit())

第三次 列表、元組、集合練習題

一、定義列表:list1 = ['life','is','short'],

list2 = ['you','need','python']

完成以下幾個要求:

1)輸出python及其下標

2)在list2后追加 '!' , 在 'short' 后添加 ','

3)將兩個字符串合并后,排序并輸出其長度

4)將 'python' 改為 'python3'

4)移除之前添加的 '!' 和 ','

list1 = ['life', 'is', 'short']
list2 = ['you', 'need', 'python']
print(list2[2],list2.index('python'))

list2.append('!')
list1.append(',')

list3 = list1 + list2
list3.sort()
print(list3, len(list3))

list3[list3.index('python')]=('python3')

list3.remove('!')
list3.remove(',')

二、自己定義元組并練習常用方法(輸出元組長度、指定位置元素等)

tuple1 = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June')
print(len(tuple1))
print(tuple1[5])

三、定義列表:

list1 = ['life','is','short'],

list2 = ['you','need','python']

list3 = [1,2,3,4,5,3,4,2,1,5,7,9]

完成以下操作:

1)構造集合 list_set1

2)將list1和list2合并構造集合 list_set2

3)輸出兩個集合的長度

4)將兩個集合合并后移除 'python'

5)在合并后的新列表中添加 'python3'

list1 = ['life', 'is', 'short']
list2 = ['you', 'need', 'python']
list3 = [1, 2, 3, 4, 5, 3, 4, 2, 1, 5, 7, 9]

list_set1 = set(list1)

list_set2 = set(list1 + list2)

print(len(list_set1), len(list_set2))

list_set1.update(list_set2)
list_set1.remove('python')

list_set1.add('python3')

第四次 字典 json類型 練習題

1.導入json模塊

import json

2.定義一個空字典dict_a,空字典dict_b

dict_a = {}
dict_b = {}

3.給dict_a 添加3個key a1,a2,a3分別對應的值為b1,b2,b3

dict_a = {'a1': 'b1', 'a2': 'b2', 'a3': 'b3'}

4.獲取dict_a所有的key,命名變量ks,打印輸出ks及ks的數據類型

ks = dict_a.keys()
print(ks, type(ks))

5.打印dict_a所有的value 命名變量vs,打印輸出vs及vs的數據類型

vs = dict_a.values()
print(vs, type(vs))

6.執行代碼print(dict_a.items()) 觀察輸出結果

print(dict_a.items())

7.將a1和a3對應的值對換

dict_a['a1'] = 'b3'
dict_b['a3'] = 'b1'

8.打印輸出dict_a

print(dict_a)

9.刪除字典dict_a中a1對應的值

dict_a.pop('a1')

10.打印輸出dict_a

print(dict_a)

11.將此時的dict_a數據更新到dict_b

dict_b.update(dict_a)

12.打印dict_b 并觀察a1和a4是否在dict_b中

print(dict_b)

13.a1如不存在dict_b中,輸入以下代碼 a1=dict_b.get('a1') 并打印變量a1

a1 = dict_b.get('a1')
print(a1)

14.將13題變量a1 添加到dict_b中,key為'a1'

dict_b['a1'] = a1

15.a4如不存在dict_b中,將a4對應的值默認為'null',并添加到dict_b中,key為'a4'

a4 = 'null'
dict_b['a4'] = a4

16.打印dict_b及其數據類型

print(dict_b, type(dict_b))

17.將dict_b轉化為json類型 命名為變量 json_c

json_c = json.dumps(dict_b)

18.打印json_c及其數據類型 觀察16題打印結果和18題結果 將不同之處指明

print(json_c, type(json_c))

19.將json_c轉換為字典類型 命名為dict_c 打印輸出 dict_c及其數據類型

dict_c = json.loads(json_c)
print(dict_c, type(dict_c))

5.第五次 字符串分割、索引和切片練習題

1.理解索引這個會在之后經常用到

2.定義字符串、例如:str1 = 'http://www.lxweimin.com/u/a987b338c373'字符串內容為自己的首頁連接,輸出自己的簡書id(u/之后的內容--a987b338c373)

str1 = 'http://www.lxweimin.com/u/d8d31a69dcf8'
print(str1[str1.find('u/')+2:])

3.設s = "abcdefg", 則下列值為

s[3] s[2:4]

s[:5] s[3:]

s[::-1] s[::2]

s = 'abcdefg'
print(s[3], s[2:4], s[:5], s[3:], s[::-1], s[::2])

d cd abcde defg gfedcba aceg

4.定義列表:list1 = [1,2,3,4,5,6,7],則下列值為

list1[3] list1[2:4]

list1[:5] list1[3:]

list1[::-1] list1[::2]

list1 = [1, 2, 3, 4, 5, 6, 7]
print(list1[3], list1[2:4], list1[:5], list1[3:], list1[::-1], list1[::2])

5.定義元組:touple1 = (1,2,3,4,5,6,7),則下列值為

touple1[3] touple1[2:4]

touple1[:5] touple1[3:]

touple1[::-1] touple1[::2]

touple1 = (1, 2, 3, 4, 5, 6, 7)
print(touple1[3], touple1[2:4], touple1[:5], touple1[3:], touple1[::-1], touple1[::2])

6.對之前學習過得集中基本類型及其方法進行復習,重中之重理解索引和切片

6.第六次 邏輯運算練習題

下列表達式邏輯運算后的結果為?(盡量直接思考解答,可以用代碼測試結果)

print(True and True)

True

print(False and True)

False

print(1 == 1 and 2 == 1)

False

print("test" == "test")

True

print(1 == 1 or 2 != 1)

True

print(True and 1 == 1)

True

print(False and 0 != 0)

False

print(True or 1 == 1)

True

print("test" == "testing")

False

print(1 != 0 and 2 == 1)

False

print("test" != "testing")

True

print("test" == 1)

False

print(not (True and False))

True

print(not (1 == 1 and 0 != 1))

False

print(not (10 == 1 or 1000 == 1000))

True 做錯了

print(not (1 != 10 or 3 == 4))

False

print(not ("testing" == "testing" and "Zed" == "Cool Guy"))

True

print(1 == 1 and not ("testing" == 1 or 1 == 0))

True

print(3 == 3 and not ("testing" == "testing" or "Python" == "Fun"))

False

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容