乔帮主:
学习编程不一定做开发工程师
它会教会你一种思考方式
编程是一种技能和工具
是你理解计算机运行的有效途径
扎克伯格:
非开发工程师对编程有一些抵触,把Python当成一个工具就可以了
Python语言的特点:
Python2.x已经是遗产
Python3.x才是未来
Python最流行的发行版Anaconda
Python官方文档
iPython
Jupyter notebook
sublime text
Pycharm
Pip
Python的基本书写规范
#这是我的第一个Python程序
import time #我导入了一个时间模块
print(time.time()) #在屏幕上打印距1970年1月一号至今度过了多少秒
if 10>9:
#这行需要缩进,缩进就是空4个空格
print("10大于9")
>>> type(8)
<class 'int'>
>>> type('2')
<class 'str'>
>>> type('极客时间')
<class 'str'>
>>> type("极客")
>>> int('8')
8
>>> str(123)
'123'
>>> bool(0)
False
比特:线路速度计算单位
字节:计算机存储数据的单位
我们经常要判断一个文件要多久才能完成传输,可以写一个计算器来计算传输时间
a = 123
"a"是变量名称
“=”为变量赋值
“123”为变量得到的值
b = a
将a的值赋给b
临时变量 单独一个字符
正规命名:意义明确的单词命名
驼峰命名:bandWidth
大驼峰:BandWidth
下划线:band_width
合法命名:开头 数字字母
中间 数字字母下划线
输入生日的年月
输出生肖 星座
序列
是指它的成员都是有序排列的,通过访问下标来访问他的成员
成员关系操作符(in not in)
连接操作符(+) 序列+序列
重复操作符() 序列整数
切片操作符([:]) 序列[0:整数]
# print(chinese_zodiac[0:12])
# print(chinese_zodiac[-1])
year = 2018#将数字赋值给一个变量
print(year % 12)
#通过数字与12的模在字符串的位置判断生肖
print(chinese_zodiac[year%12])
#判断成员关系
print('狗' in chinese_zodiac)
print('狗' not in chinese_zodiac)
print(chinese_zodiac + chinese_zodiac)
print(chinese_zodiac + 'abc')
print(chinese_zodiac * 3)
>>> (4)>(5)
False
>>> (1,20)>(2,20)
False
>>> a = (1,3,5,7,9)
>>> b = 4
>>> filter(lambda x : x<b,a) #取出小于4的元素
<filter object at 0x000001B22C3EE240>
>>> list(filter(lambda x : x<b,a)) #取出小于4的元素l
[1, 3]
>>> list(filter(lambda x : x<b ,a)) #取出小于4的元素l
[1, 3]
>>> b = 6
>>> list(filter(lambda x : x<b ,a)) #取出小于6的元素l
[1, 3, 5]
>>> len(list(filter(lambda x : x<b ,a))) #取出小于6的元素个数
3
zodiac_name = (u"摩羯座",u"水瓶座",u"双鱼座",u"白羊座",u"双子座",u"巨蟹座",u"狮子座",u"处女座",u"天秤座",u"天蝎座","射手座")
zodiac_days = ((1,20),(2,19),(3,21),(4,21),(5,21),(6,22),(7,23),(8,23),(9,23),(10,23),(11,23),(12,23))
(month,day) = (2,15 )
zodiac_day = filter(lambda x:x<(month,day),zodiac_days)
print(zodiac_day)
zodac_len = len(list(zodiac_day))%12
print(zodiac_name[zodac_len])
a_list = ['abc','xyz']
a_list.append('X')
print(a_list)
a_list.remove('xyz')
print(a_list)
if语句和else复合为更复杂的判断
x = "abc"
if x == "abc":
print("x和abc相等")
else:
print("x 和 abc不等")
#记录生肖,根据年份来判断生肖
chinese_zodiac = "鼠牛虎兔龙蛇马羊猴鸡狗猪"
year = int(input("请用户输入年份"))
if((chinese_zodiac[year%12] =='狗')):
print("狗年大吉")
while 表达式:
代码块
for语句
for 迭代变量 in 可迭代对象:
代码块
for i in range(1,13):
print(i)
for year in range(2000,2019):
print("2018的生肖是狗")
for year in range(2000,2019):
print("%
while(True):
print('a')
num = num+1
if num>10:
break
import time
num = 5
while(True):
num = num+1
if num == 10:
continue
print(num)
time.sleep(1)
zodiac_name = (u"摩羯座",u"水瓶座",u"双鱼座",u"白羊座",u"金牛座",u"双子座",u"巨蟹座",u"狮子座",u"处女座",u"天秤座",u"天蝎座","射手座")
zodiac_days = ((1,20),(2,19),(3,21),(4,21),(5,21),(6,22),(7,23),(8,23),(9,23),(10,23),(11,23),(12,23))
(month,day) = (2,15 )
int_month = int(input("请输入月份"))
int_day= int(input("请输入日期"))
for zd_num in range(len(zodiac_days)):
if zodiac_days[zd_num] >= (int_month,int_day ):
print(zodiac_name[zd_num])
break
elif int_month == 12 and int_day >23:
print(zodiac_name[0])
break
n = 0
while zodiac_days[n] < (int_month,int_day):
if int_month == 12 and int_day>23:
break
n+=1
print(zodiac_name[n])
方案1:字符串
方案2:列表
新的数据类型:映射
在python中叫字典
{“哈希值”:”对象“}
{’length‘:180,‘width’:80}
zodiac_name = (u"摩羯座",u"水瓶座",u"双鱼座",u"白羊座",u"金牛座",u"双子座",u"巨蟹座",u"狮子座",u"处女座",u"天秤座",u"天蝎座","射手座")
zodiac_days = ((1,20),(2,19),(3,21),(4,21),(5,21),(6,22),(7,23),(8,23),(9,23),(10,23),(11,23),(12,23))
(month,day) = (2,15 )
chinese_zodiac = "鼠牛虎兔龙蛇马羊猴鸡狗猪"
cz_num = {}
for i in chinese_zodiac:
cz_num[i] = 0
z_num = {}
for i in zodiac_name:
z_num[i] = 0
while True:
#用户输入年月日
year = int(input("请输入年份"))
month = int(input("请输入月份"))
day= int(input("请输入日期"))
n = 0
while zodiac_days[n] < (month,day):
if month == 12 and day>23:
break
n+=1
print(zodiac_name[n])
#输出生肖和星座
print("%s 年的生肖是 %s"%(year,chinese_zodiac[year%12]))
cz_num[chinese_zodiac[year%12]]+=1
z_num[zodiac_name[n]]+=1
#输出生肖和星座的统计信息
for each_key in cz_num.keys():
print("生肖 %s 有 %d个"%(each_key,cz_num[each_key]))
for each_key in z_num.keys():
print("星座 %s 有 %d个"%(each_key, z_num[each_key]))
#从 1 到 10 所有偶数的平方
alist = []
for i in range(1,11):
if(i%2 == 0):
alist.append(i*i)
print(alist)
#列表推导式
blist = [i*i for i in range(1,11) if(i%2) == 0]
print(blist)
zodiac_name = (u"摩羯座",u"水瓶座",u"双鱼座",u"白羊座",u"金牛座",u"双子座",u"巨蟹座",u"狮子座",u"处女座",u"天秤座",u"天蝎座","射手座")
z_num = {}
for i in zodiac_name:
z_num[i] = 0
z_num = {i:0 for i in zodiac_name}
print(z_num)
文件内建函数和方法
# encoing = utf-8
# 将雄安说主要任务记录在文件中
file1 = open("name.txt","w")
file1.write('诸葛亮')
file1.close()
file2 = open("name.txt")
#file2.read()
print(file2.read())
file2.close()
file3 = open("name.txt",'a')
file3.write("刘备")
file3.close()
逐行处理
file5 = open("name.txt")
for line in file5.readlines():
print(line)
print('**********')
file6= open('name.txt')
print(file6.tell())
file6.read(1)
print(file6.tell())
file6.seek(0,0)
print(file6.tell())
file6.read(1)
print(file6.tell())
异常是在出现错误时采用正常控制流以外的动作
异常处理的一般流程是:检测到错误,引发异常,对异常进行捕获操作
try:
<监控异常>
except Exception[,reason]:
<异常处理代码>
finally:
<无论异常与否都要执行的操作>
i = j
print(())
a= '123'
print(a[4])
d= {'a':1,'b':2}
print(d['c'])
year = int(input("int year"))
try:
year = int(input("input year:"))
except ValueError:
print("年份要输入数字")
a= 123
a.append()
try:
print(1/0)
except ZeroDivisionError as e:
print('0不能做除数 %s'%e)
try:
raise NameError('helloError')
except NameError:
print('my custom error')
try:
a = open('name.txt')
except Exception as e:
print(e)
finally:
a.close()
函数是程序逻辑进行结构化的一种编程方法
函数的定
def函数的名称()
代码
return 需要返回的内容
import re
def find_item(hero):
with open('sanguo.txt',encoding = 'GB213') as f:
data = f.read().replace('\n','')
name_num = re.findall(hero,data)
print("主角%s出现%s次"%(hero,len(name_num)) )
return len(name_num)
#读取人物信息
name_dict = {}
with open('name.txt') as f:
for line in f:
names = line.split('|')
for n in names:
name_num = find_item(n)
name_dict[n] = name_num
def func(a,b,c):
print('a = %s' %a)
print('b = %s' %b)
print('c = %s' %c)
func(1,2,3)
#取得参数的个数
#变长参数
def howlong(first,*other):
print(1+len(other))
howlong(1,2,2)
def true():
return True
def add(x,y):
return x+y
from functools import reduce
a = [1,2,3,4,5,6,]
b= list(filter(lambda x:x>2,a))
print(b)
a= [1,2,3]
c=list(map(lambda x:x,a))
print(c)
d = reduce(lambda x,y:x+y,[2,3,4],1)
print(d)
for i in zip((1,2,3),(4,5,6)):
print(i)
def fun():
num1 = 1
num2 = 2
return (num1+num2)
def sum(a):
def add(b):
return a+b
return add
num1 = fun()
num2 = sum(2)
print(num2(4))
print(type(num1))
print(type(num2))
def counter(FIRDT=0):
cnt = [FIRDT]
def add_one():
cnt[0]+=4
return cnt[0]
return add_one()
num1 = counter()
print(num1)
print(num1)
print(num1)
def a_line(a,b):
def arg_y(x):
return a*x+b
return arg_y
line1 = a_line(3,5)
print(line1(10))
print(line1(20))
line2 = a_line(2,3)
print(line2(12))
import time
print(time.time())
def timer(func):
def wrapper():
start_time = time.time()
func()
stop_time = time.time()
print("运行时间是%f秒"%(stop_time-start_time))
@timer
def i_can_sleep():
time.sleep(3)
i_can_sleep
def tips(func):
def nei(a,b):
print('start')
func(a,b)
print('stop')
return nei
@tips
def add(a,b):
print(a+b)
@tips
def sub(a,b):
print(a-b)
print(add(1,2))
def new_tips(argv):
def tips(func):
def nei(a,b):
print('start %s'%argv)
func(a,b)
print('stop')
return nei
return tips
@new_tips('add')
def add(a,b):
print(a+b)
@new_tips('sub')
def sub(a,b):
print(a-b)
print(add(1,2))
print(sub(1,2))
fd = open('name.txt')
try:
for line in fd:
print(line)
finally:
fd.close()
with open('name.txt') as f:
for line in f:
print(line)
模块是在代码量很大以后,为了将需要重复使用的代码组织在一起,这部分代码可以附加到现有的程序中