一.Python介绍
1用途
1)WEB开发
最火的Python web框架Django, 支持异步高并发的Tornado框架,短小精悍的flask,bottle, Django官方的标语把Django定义为the framework for perfectionist with deadlines(大意是一个为完全主义者开发的高效率web框架)
2)网络爬虫
爬虫领域,Python几乎是霸主地位,ScrapyRequestBeautifuSoapurllib等,想爬啥就爬啥
3)网络编程
支持高并发的Twisted网络框架, py3引入的asyncio使异步编程变的非常简单。
4)云计算
目前最火最知名的云计算框架就是OpenStack,Python现在的火,很大一部分就是因为云计算
5)人工智能
谁会成为AI 和大数据时代的第一开发语言?这本已是一个不需要争论的问题。如果说三年前,Matlab、Scala、R、Java 和 Python还各有机会,局面尚且不清楚,那么三年之后,趋势已经非常明确了,特别是前两天 Facebook 开源了 PyTorch 之后,Python 作为 AI 时代头牌语言的位置基本确立,未来的悬念仅仅是谁能坐稳第二把交椅。
6)自动化运维
7)金融分析
8)科学运算
9)游戏开发
2.Python种类
CPython,IPython,PyPy,Jython,IronPython
二.Hello World
print("Hello World!")
三.变量
1)格式
#变量名(相当于门牌号,指向值所在的空间),等号,变量值 name='Hantaozi' sex='male' age=18 level=10
2)定义规范
#1. 变量名只能是 字母、数字或下划线的任意组合 #2. 变量名的第一个字符不能是数字 #3. 关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
3)定义方式
#驼峰体 AgeOfHantao = 56 #下划线 age_of_hantao = 56
4)id,type,value
id:可以代表内存地址,type:数据类型,value:值
#=比较的是value;而is比较的id
5)常量
用大写字母表示
6) 两种特殊的赋值方式
#链式赋值 x=y=z=10 #交换两个变量的值 m=10 n=20 m,n=n,m #从数量类型中解压(字符串,元祖,列表,字典) t=('asd','qwe','zxc','rty') # a,b,c,d=t # print(a,b,c,d) a,*_,d=t print(a,d)
四。用户交互
name=input('input your name:') age=input(‘input your age:’)
五.数据类型
1)数字
#int 整型 #float 浮点数
2)字符串
#加引号的字符是字符串类型。str
#1.单双引号一般没有区别,只用于区别字符内部引号。
name=“i'm hantao”
#2.三引号用于多行字符串
info='''
i'm hantao
i like Python
'''
3)列表
#定义在[]中,用逗号隔开的多个多种类型的元素 hobbies = ['movies','game','studying'] print(hobbies[0])
movies # info=['hantao',['movies','game']] print(hobbies[1][0])
movies
4)字典
info={'name':'hantao','sex':'male','age':'18','hobbies':['game','movies']'}
print(info['name'])
hantao
print(info[hobbies][0])
game
5)bool
#布尔值有True,false.
六、格式化输出
#%s字符串占位符:可以接受字符串和数字 print('my name is %s,my age is %s'%('hantaozi',18)) #%d数字占位符:只能接受数字 print('my name is %s,my age is %s'%('hantaozi','18'))#报错
小练习
name=input('your name:') age=input('your age:') sex=input('your sex:') job=input('your job:') print(''' -----------info of %s----------- Name : %s Age : %s Sex : %s Job : %s --------------end---------------- '''%(name,name,age,sex,job))
七、基本运算符
算数运算:
a=3,b=2
运算符 | 描述 | 实例 |
+ | 加 | a+b 5 |
- | 减 | a-b 1 |
* | 乘 | a*b |
/ | 除 | a/b 3 |
% | 取模 | a%b 0 |
** | 幂 | a**b 9 |
// | 整除 | a//b 1 |
比较运算:
a=3,b=2
运算符 | 描述 | 实例 |
== | 等于 | a==b False |
!= | 不等于 | a!=b True |
<> | 不等于 | a<>b True |
> | 大于 | a>b True |
< | 小于 | a<b False |
>= | 大于等于 | a>=b True |
<= | 小于等于 | a<=b False |
赋值运算:
运算符 | 描述 | 实例 |
= | 普通赋值运算 | a=b 将b的值赋给a |
+= | 加法赋值 | a+=b a=a+b |
-= | 剪发赋值 | a-=b a=a-b |
*= | 乘法赋值 | a*=b a=a*b |
/= | 除法赋值 | a/=b a=a/b |
%= | 取模赋值 | a%=b a=a%b |
**= | 幂赋值 | a**=b a=a**b |
//= | 整除赋值 | a//=b a=a//b |
逻辑运算:
and,or,not
身份运算:
#is比较的是id #而==比较的是值
八、流程控制之if...else
练习1:
#用户登录验证
name=input('input your name:') password=input('input your password:') if name=='hantao'and password=='123': print('login seccess') else: print('name nor password is wrong')
练习2:
#根据用户名称设定用户权限 ''' hantao-->超级管理者 liangchaowei-->普通管理者 其他-->普通用户 ''' name=input('输入用户名字:') if name =='hantao': print('超级管理者') elif name =='liangchaowei': print('普通管理者') else: print('普通用户')
练习3:
# 如果:今天是Monday,那么:上班 # 如果:今天是Tuesday,那么:上班 # 如果:今天是Wednesday,那么:上班 # 如果:今天是Thursday,那么:上班 # 如果:今天是Friday,那么:上班 # 如果:今天是Saturday,那么:出去浪 # 如果:今天是Sunday,那么:出去浪 today=input('今天周几?') if today in ['Monday','Tuesday','Wednesday','Thursday','Friday']: print('上班') elif today in ['Saturday','Sunday']: print('出去浪') else: print('请输入周几')
九、流程控制之while循环
while语法:
while 条件: #循环体 continue break
#打印0~10 count=0 while count<=10: print('loop',count) count+=1
#打印0~10的偶数
count=0
while count<=10:
if count%2 == 0:
print('loop',count)
count+=1
break和continue:
#break用于退出本层循环 while True: print "123" break print "456" #continue用于退出本次循环,继续下一次循环 while True: print "123" continue print "456"
循环嵌套:
''' 练习,要求如下: 1 循环验证用户输入的用户名与密码 2 认证通过后,运行用户重复执行命令 3 当用户输入命令为quit时,则退出整个程序 ''' count=0 tag=True while tag: name = input('input your username:') password = int(input('input your password:')) if name=='hantao' and password==123: print('login success') while tag: cmd=input('') if cmd=='Quit': tag=False else: print(cmd) else: print('wrong username') count+=1 if count==3: break
while+else:
#循环正常结束,else有输出 count = 0 while count <= 5 : count += 1 print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------") 输出 Loop 1 Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 循环正常执行完啦 -----out of while loop -----
十.流程控制之for循环
l=(1,3,5,2,7) for l in l: print(l) for i in range(1,10): for j in range(1,i+1): print('%s*%s=%s' %(i,j,i*j),end=' ') print()
练习题:
#1. 使用while循环输出1 2 3 4 5 6 8 9 10 count=1 while count<=10: if count==7: count+=1 continue print(count) count+=1
#2. 求1-100的所有数的和 count=0 sum1=0 while count<=100: count+= 1 sum1+=count print(sum1)
#3. 输出 1-100 内的所有奇数 count=0 while count<100: if count%2==1: print(count) count+=1 else: count+=1
#4. 求1-2+3-4+5 ... 99的所有数的和 count=0 sum1=0 while count<99: count+=1 if count%2==1: sum1+=count else: sum1-=count print(sum1)
''' 5:猜年龄游戏升级版 要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出 ''' name='hantao' count=0 while count<3: if name==input('guess my name:'): print('NB!') break else: count+=1 if count==3: cmd=input('play again?') if cmd =='Y'or cmd=='y': count=0 else: break