第一周作业(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.jianshu.com/u/a987b338c373′字符串内容为自己的首页连接,输出自己的简书id(u/之后的内容–a987b338c373)

str1 = 'http://www.jianshu.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

    原文作者:rayco
    原文地址: https://www.jianshu.com/p/e4eb98943590
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞