思维导图
python的发展史
- 1989年,Guido (龟叔) 为 ABC 语言写插件,插件他发现比ABC好 Monty Python 喜剧团;
- 1990年,发布python第一个版本;
- 2001年,2.x;
- 2013年,python3.0;
tiobe
编程语言 :
类型
- C , Java ,C++ ,C# ,shell ,JavaScript ,Matlab
- 面向对象语言
- 面向过程语言
- 面向函数编程
- 编译性语言:Windoes写C了一个qq软件,并不能在linux或ios上执行
- 解释性语言:可以跨平台,用python写一个APP,只需要写一份,在所有平台都可以运行。
python的优缺点
优点 :
- 创始人说的 :简单 优雅 明确
缺点 :
- 速度慢
- http://www.baidu.com/xxx.html
wget xxx.html
分析
python C
1h+10ms 1h+1msipython安装
第一个python脚本
#################
#!/usr/bin/python
#coding:utf-8
print "hello"
print "哈哈"
#################
[root@localhost ipython]# vim hello.py
[root@localhost ipython]# python hello.py
hello
哈哈
[root@localhost ipython]# chmod +x hello.py
[root@localhost ipython]# ./hello.py
hello
哈哈
解释器问题
#可移植性的对比,如果你的代码移植到的机器,python没安装在usr/bin/python用第二种
#!/usr/bin/python
#!/usr/bin/env python
中文问题
coding:utf-8
coding=utf-8
encoding:utf-8
encoding=utf-8
编码格式:
ASCII:美国人发明里计算,一个字节(8位)去存储一个英文字符 2^8=256;
Unicode:2个字节(16位)存储一个字符 2^16=65536;
GB2312:
utf-8:可变程度,在编程过程中,英文多,中文少。如果是英文字符,就用一个
字节去存储,如果是中文,用三个字节去存储;
python的IDE工具
- sublime
- eslipse
- pycharm
pycharm快捷键和技巧
- ctrl+alt+s:设置
- alt+insert:新建(文件,目录,包)
- ctrl+s:保存
- Delete:删除(文件,目录,包)
- ctrl +e
- ctrl+ / :快速注释(或取消)一行或多行
- ctrl+z:撤回
- shift+alt+N:快速搜索
- crtl+d:快速复制
- ctrl+X:删除
- shift+delete:删除
二 :变量 输入输出
实验
1:求平均成绩
########################################
course1=input(':')
course2=input(':')
course3=input(':')
average = (course1+course2+course3)/3
print average
if average > 60:
print "jige"
else:
print "bujige"
#######################################
SUM=0
for i in range(1,4):
grade=input("grade:")
SUM+=grade
average=SUM/i
print average
if average > 60:
print "jige"
else:
print "bujige"
########################################
优化
#######################################
SUM=0
for i in range(1,4):
grade=input("grade:")
SUM+=grade
average=SUM/i
# print average
if average > 60:
print "jige %d" %(average)
else:
print "bujige",average
#####################################
2:写主机配置
hostname=raw_input("please input your hostname: ")
ip=raw_input("please input your ip: ")
used_year=input("please input your used_year: ")
CPU=raw_input("please input your CPU: ")
Menmory=raw_input("please input your Menmory: ")
Manager_name=raw_input("please input your manager_name: ")
if used_year <10:
print "主机名:",hostname
print "ip:",ip
print "used_year:",used_year
print "CPU:",CPU
print "Menmory:",Menmory
print "Manager_name:",Manager_name
print "主机名:{} ip:{} used_year:{} CPU:{} Menmory:{} Manager_name:{}".format(hostname,ip,used_year,CPU,Menmory,Manager_name)
else:
print "该机器使用年限太久"
####################################################
优化
#!/usr/bin/python
#coding:utf-8
hostname=raw_input("please input your hostname: ")
ip=raw_input("please input your ip: ")
used_year=input("please input your used_year: ")
CPU=raw_input("please input your CPU: ")
Menmory=raw_input("please input your Menmory: ")
Manager_name=raw_input("please input your manager_name: ")
if used_year <10:
print """ 电脑配置信息
{hostname}
{ip}
{used_year}
{CPU}
{Menmory}
{Manager_name}""".format(hostname=hostname,ip=ip,used_year=used_year,CPU=CPU,Menmory=Menmory,Manager_name=Manager_name)
else:
print "该机器使用年限太久"
3:简单乘法表
#!/usr/bin/env python
#coding: utf-8
print" 乘法口诀表"
for i in range(1,10):
for j in range(1,i+1):
print "{}*{}={}".format(i,j,i*j),
print""
4:登陆认证
#!/usr/bin/env python
#coding: utf-8
username="root"
password="westos"
num=0
while num!=3:
a=raw_input("please input your username:")
if a==username:
numb=0
while numb!=3:
b=raw_input("please input your password:")
if b==password:
print"login ok"
exit()
else:
print"password is not ok"
numb=numb+1
else:
print"user is not exist"
num=num+1
print "count is bigger than 3"
[root@foundation60 daima]# python useradd.py
please input your username:root
please input your password:westos
login ok
[root@foundation60 daima]# python useradd.py
please input your username:root
please input your password:redhat
password is not ok
please input your password:redhat
password is not ok
please input your password:redhat
password is not ok
please input your username:root
please input your password:westos
login ok
[root@foundation60 daima]# python useradd.py
please input your username:root
please input your password:ee
password is not ok
please input your password:ee
password is not ok
please input your password:ee
password is not ok
please input your username:ww
user is not exist
please input your username:root
please input your password:westos
login ok