您的位置:首页 > 编程语言 > Python开发

Datawhale ----7 days for Python(任务一)

2019-01-03 21:51 295 查看

学习内容1

1.使用anaconda搭建python的环境(跳过第一章,去网络上搜索anaconda3的教程)

Anaconda3详细安装使用教程及问题总结

2.完成系统变量的设置,通过在命令行输入python3检验一下python3有没有装成功。

python 3.6

3.解释什么是变量,变量命名规则。(第二章)

每个变量都存储了一个值 ——与变量相关联的信息
1.变量名只能包含字母、数字和下划线。
2.变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为message_1,但不能将其命名为1_message。
3.变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。
4.不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print (请参见附录A.4)。
5.变量名应既简短又具有描述性。例如,name比n好,student_name比s_n好,name_length比length_of_persons_name好。
6.慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。

4.了解字符串这一数据类型,通过书和搜索整理字符串的方法,包括大小写的转换,合并字符串,删除空白等(strip())(第二章)

name = 'liberty wave' 	#定一个字符串
print(name.title())    	#输出首字母大写	Liberty Wave
print(name.upper())    	#转换为大写字母	LIBERTY WAVE
print(name.lower())		#转换为小写字母	liberty wave
first_name = '  liberty  '
last_name = '  wave'#定义新的字符串
print(first_name +' '+ last_name) #拼接字符串  '  liberty    wave'
print(first_name.lstrip()) #删除左侧空白 'liberty  '
print(first_name.rstrip()) #删除右侧空白 '  liberty'
print(first_name.strip())  #删除两侧空白 liberty

5.了解基本的转义字符和格式化字符,并整理成文字或者代码。(第二章)

转义字符
\(在行尾时)	                 续行符
\\	                       反斜杠符号
\'	                         单引号
\"	                         双引号
\a                            响铃
\b	                     退格(Backspace)
\e	                          转义
\000                      	   空
\n	                          换行
\v	                       纵向制表符
\t	                       横向制表符
\r	                          回车
\f	                          换页
\oyy	        八进制数yy代表的字符,例如:\o12代表换行
\xyy	        十进制数yy代表的字符,例如:\x0a代表换行
格式化字符
%c                   格式化字符及其ASCII码
%s                       格式化字符串
%d                         格式化整数
%u                       格式化无符号整型
%o     				   格式化无符号八进制数
%x                     格式化无符号十六进制数
%X                 格式化无符号十六进制数(大写)
%f                格式化浮点数字,可指定小数点后的精度
%e                   用科学计数法格式化浮点数
%E                作用同%e,用科学计数法格式化浮点数
%g                        %f和%e的简写
%G                     %f 和 %E 的简写
%p                 用十六进制数格式化变量的地址

6.了解数字类型,整数和浮点数。(第二章)

print(1+2-3*4/6)  #整数运算
print('3.0/2')    #浮点数运算

7.如何添加注释(第二章)

print('libertywave')   #使用#注释一行代码
'''
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
#使用三引号注释多行代码

学习内容2

1.了解列表数据类型。(第三章)

列表 由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系。
colors = ['red','blue','yellow','green'] #创建一个颜色的列表,包含四个元素

2.完成列表的增删查改(增加,删除,查找,修改)。(第三章)

colors.append('grey')  #末尾插入新的元素
colors.insert(2,'purple')  #插入新的元素且索引值为2
del colors[3] #删除索引值为3的元素
colors.pop()  #弹出最后一个元素且可以引用
colors[2]  #查找索引为2的元素

3.了解列表的排序,比较sorted和sort两个函数。(第三章)

colors = ['red','blue','yellow','green']
colosr.sort()    #永久性原地修改列表顺序
print(colors)    #['blue', 'green', 'red', 'yellow'],反转顺序添加参数reverse=True
print(sorted(colors))  #['blue', 'green', 'red', 'yellow']  不影响原列表顺序,
print(colors)           #['red','blue','yellow','green']

4.了解列表的方法,sort(),reverse()等。(附加题:了解sort()函数里的key参数,并实现一个列表的排序[[“a”,1],[“b”,0],[“c”,3]],要求变成[[“b”,0],[“a”,1],[“c”,3]])。(第三章)

reverse()反转列表元素的排列顺序,并且单次使用是永久性的
color = ['red','blue','yellow','green']
color.reverse()   #原地反转
print(color)
['green', 'yellow', 'blue', 'red']

lis = [['a',1],['b',0],['c',3]]
def takesecond(lis):          #定义一个函数返回列表的第二个元素
return lis[1]
lis.sort(key=takesecond)      #根据第二个元素进行排序(升序)
print(lis)
[['b', 0], ['a', 1], ['c', 3]]
lis.sort(key=takesecond,reverse=True)   #根据第二个元素进行排序(降序序)
[['c', 3], ['a', 1], ['b', 0]]

5.了解循环,实现简单的遍历列表。(第四章)

colors = ['red','blue','yellow','green']
for color in colors:   #编码规范,注意冒号
print(color)
red
blue
yellow
green

6.了解range函数,并实现创建列表,解析列表。(第四章)

nums = range(100)  #左闭右开区间,不包括100,数字为1-99
nums_list = list(nums)  #转换为列表
nums_pre = [num**3 for num in range(5)]  #列表解析式
[0, 1, 8, 27, 64]

7.了解切片并使用切片。(第四章)

colors = ['red','blue','yellow','green']
print(clors[:2])   #提取前两个元素
print(colors[:])   #提取所有元素
print(colors[2:])  #提取第二个及之后的所有元素
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: