导语

为了几个星期后的python程序设计大赛,也为了利用python方便化办公一下,处理一下一些简单的文件,更有为了让自己去适应下no subtitle的英语环境学习,我就去看了一点关于python的英语教学视频,里面的事情很简单的,但是类似一个项目,也很有趣。这里就纪录一下我学习python中的一些新的心得

python的Bulit-InFuntion

python中的Bulit-inFunction包含了许许多多的库函数,类似C语言中的stdio.h,比如最为经常使用的便是python的print函数,打印一些基本内容等等之类的

举例子时间:print函数

demo

1
2
3
4
5
6
7
8
9
import time
import random
print("---------效果--------")

print("Loading",end = '')# end 结束符 默认是\n
for i in range(10):
print(".",end = '',flush =True)//flush,强制刷新流
time.sleep(random.random())//random 生成随机数字0-1的 sleep(程序休眠一段时间) 随机休眠一段时间
pass

输出如下图

输出结果注解:

这是一张类似载入动画的效果,类似加载的进度条,可以利用这个print函数实现

具体函数解析,参考python的官方文档,反正有四个参数,得需要研究一段时间的

举例子时间,随机数模块

demo

1
2
3
4
5
6
7
8
9
10
11
import random
import time
import sys
print(random.randint(1,9))'''random.randint(a,b) 返回整数 从a->b'''
for line in range(10):
print(random.randint(1,9),end ='\n',flush = True )
time.sleep(0.5)
pass
# help(random)

print(sys.path)

输入结果如图

输出结果解析

类似上一张的图片的,这里使用的python中的random模块生成随机数,是伪随机数。通过随机生成1-9的整数,输出界面上,一共生成十个。具体更多文档,查看python的官方文档

Python中的一些数据结构

里面有tuple,list 和set等等,这里简单介绍下我用过的一些数据结构以及用法

tuple

先举个例子

demo:

1
2
game_tuple = (0,0,0,0,0,0,0,0,0,)
print(type(game_tuple))

输出:

1
<class 'tuple'>

解析:

有brackets (圆括号)的便是tuple,就那么简单,tuple里面可以包含了很多种类型的事务,不仅包括int型,还有字符串型的,都有,还有其他类型的。比如刚刚的game_tuple = (“1”,2,3,4) 也是tuple

tuple的一些可使用的内置函数

这里先搁置

list

同样举个例子

demo:

1
2
game_list = [[0,0,0],[0,0,0],[0,0,0]]
print(type(game_list))

输出

1
<class 'list'>

解析:

这就是与tuple中的一些不同之处便是这里的使用的是方括号(Square brackets)同样,也可以是各种类型,包括但不限于int整形和字符串型

list可是使用的一些内置函数

举例子

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
game_list = [[0,0,0],["1",0,0],[0,0,0]]

game_list.insert(0,[0,0,1]) '''插入[0,0,1]到list的第一个元素中'''
print(game_list)

game_list.insert(len(game_list),[0,2])'''[0,2]插入list中的最后一个元素中'''
print(game_list )

game_list.append([0,0,"append"])'''上述命令相同,直接把[0,0,"append"]插入到append中'''
print(game_list )

game_list.pop(4)'''清除第四个元素并返回其值 类似栈里面的pop'''
print(game_list)

game_list.clear()'''清空这个list'''
print(game_list)

输出:对应五个输出

1
2
3
4
5
[[0, 0, 1], [0, 0, 0], ['1', 0, 0], [0, 0, 0]]
[[0, 0, 1], [0, 0, 0], ['1', 0, 0], [0, 0, 0], [0, 2]]
[[0, 0, 1], [0, 0, 0], ['1', 0, 0], [0, 0, 0], [0, 2], [0, 0, 'append']]
[[0, 0, 1], [0, 0, 0], ['1', 0, 0], [0, 0, 0], [0, 0, 'append']]
[]

解析:

insert(i,x) insert an item at a given position,the first argument is the index of the element before which to insert,

append(x): add one number called x to the end of list

remove(x) remove the first item from list whose value is equal to x

pop(i) remove the item at the given position in the list and return it

clear() Remove all items from the list. Equivalent to del a[:].

等等之类的

set

举例子:

demo:

1
2
3
game_sets = {'banana','orange','apple'}
# print("game_sets posture:"+type(game_sets))
print(type(game_sets))

输出:

1
<class 'set'>

输出解析:

这就是里面最具有代表性的东西了,还有括号,只不过是中括号而已了,这种清况一样是可以用来区分tuple和set 和list的工具,这种便是集合(set)了。而且里面的元素也可以是任意类型的

set中可自带的函数

举例子

demo

1
2
3
4
5
6
7
test_sets1 = {0,1,2,3,4,5}
test_sets2 = {5,6,7,8,9,10}

print("a-b:",test_sets1 - test_sets2)
print("a&b:",test_sets1 & test_sets2)
print("a|b:",test_sets1 | test_sets2)
print("a^b:",test_sets1 ^ test_sets2)

输出结果:

1
2
3
4
a-b: {0, 1, 2, 3, 4}
a&b: {5}
a|b: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
a^b: {0, 1, 2, 3, 4, 6, 7, 8, 9, 10}

解析

这里面分别对应了与或三种集合判定的方式,可以看到与 必须是两个同时存在的才会显示出来 类似并集,而非或则是 有A或者是有B 或者是两个都有的 全部返回,类似交集,而a^b 则是交集减并集。

减号特殊一些 就是在A有的 全部显示 如果B也有A的元素,将里面的B的共同元素不显示

python函数

举例子

1
2
3
4
5
6
7
8
def test_function(one,two):
return one+two

def test_function_default(one = 1, two = 2):
return one + two

print(test_function(1,2))
print(test_function_default())

输出:

1
2
3
3

解析

可以看到 function名分别为test_function,one和two分别是参数,参数可以制定默认的,如果不指定,一定要有相应的参数。另外 注意:只有同类型的数字可以相加,不同类型的不行

python处理异常机制

例子:

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def test_errorHandling(first = 1,last = 2):
try:
print(first + last)
pass
except Exception as e:
print("Wrong Input")
raise
else:
pass
finally:
pass
pass
'''测试一 正常情况'''
test_errorHandling(1,5)
'''测试一 错误输入'''
test_errorHandling(1,"5")

输出

1
2
3
4
5
6
7
8
6
Traceback (most recent call last):
Wrong Input
File "H:\All_program_workplace\Python\programmer\test7_errorHandling.py", line 16, in <module>
test_errorHandling(1,"5")
File "H:\All_program_workplace\Python\programmer\test7_errorHandling.py", line 3, in test_errorHandling
print(first + last)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

解析:

出错之后 抛出异常(自己写的wrong input) 具体内容 我还没有深入学习到 今天就先写到这里

未完待续

python3官方中文文档

参考资料:官方中文文档