1.python的发展和应用:
2.python环境
linux:
交互式:
yum install python -y
python //执行命令
print "hello"
a="hello"
print a
关闭环境,再进去之后就不可识别‘a’
vim,gedit
windows:
note:0xeffbbbf
notepad++
sublime
atom
MAC;自带环境
3,python解释器
cpython:
Jpython:
Ironpython:将python编译成.net
4.python基础
输入输出:print
In [1]: a=123
In [2]: print a
123
In [3]: print b
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-ab3a5d8f1075> in <module>()
----> 1 print b
NameError: name 'b' is not defined
In [4]: print 'b'
b
In [5]: print "b"
b
In [6]: print "100+100"
100+100
In [9]: print '100+100=',100+100
100+100= 200
In [2]: raw_input() //输入一个字符,会立即输出
注意:raw_input输出的默认是字符型,在使用所输出的数据时,要先强制类型转换
5
In [5]: a = raw_input("please input a number :")
please input a number :1
In [6]: print a
1
In [7]: a + 1
[root@localhost ipython]# vim code/myinput.py
/usr/bin/python
#coding:utf-8
#coding=utf-8
#encoding:utf-8 //此五项仅仅需要一个就可以输出中文
#coding=utf-8
#_*_coding:utf-8-*-
a = raw_input ("please inpout a number:a = ")
a = raw_input ("please inpout a number:b = ")
print"结果是 :"int(a) + int(b) =
python变量命名规则:驼峰命名法和下划线命名法,可以使用数字字母下划线组合命令,但是不能以数字开头命名。
练习:在交互模式下,让python计算一月中有多少分钟,可以使用变量dayMounth(每月天数),hoursPerDay(每天小时数),minutesPerDay(每天分钟数),分别显示一月天s\数(JanMinuyes)和四月天数(AprilMinutes)。
In [1]: dayPerMonth = 30
In [3]: hoursPerDay = 24
In [4]: minutePerHour = 60
In [5]: totalDays = dayPerMonth*hoursPerDay*minutePerHour
In [6]: print totalDays
43200
python变量类型:
int ,long , float
指数表示:
例如: 0.0000012 1.2e-6
12000000 1.2e7
复数表示;a = 2j + 3
type(a)
complex //复数
In [7]: a = 2
In [8]: type(a) //整型
Out[8]: int
id(a) //返回的是对象的“身份证号”,为一且不变,特指符合类型的对象(如类和list),对于字符串整数来说,id随值的改变而该变
In [9]: b=1.0
In [10]: type(b) //浮点型
Out[10]: float
In [11]: 3/2
Out[11]: 1
In [12]: float(3/2) //强制类型转换
Out[12]: 1.0
In [13]: float(3)/2
Out[13]: 1.5
In [14]: 3/float(2)
Out[14]: 1.5
注意:也可以添加指令:
In [55]: from __future__ import division //接下来计算除法就会输出应有的正确答案
In [15]: type(a)
Out[15]: int
In [16]: a=1L //强制成long型
In [17]: type(a)
Out[17]: long
In [18]: c = 2j + 3 //复数
In [19]: type(c)
Out[19]: complex
比较运算符;
In[22]: 1 > 2
talse
In[23]: 1 > 2 and 3 > 4
false
下午:
1.三个双引号或者单引号的作用
保证输入的格式不改变
In [1]: str = "hello"
In [2]: print str
hello
In [3]: type(str)
Out[3]: str
In [4]: mail = "tom: hello i am westos"
In [5]: print mail
tom: hello i am westos
In [6]: mail = """tom: hello i am westos"""
In [7]: print mail
tom: hello i am westos
In [8]: mail = """tom: hello i am westos"""
In [9]: mail = """tom: hello
...: i am westos
...:
...: """
也可以使用这种方法:mail = "tome:\nhello\ni am a boy"
In [10]: print mail
tom: hello
i am westos
In [11]: mail
Out[11]: 'tom: hello \ni am westos\n\n' //按照指定的顺序和格式输出想相输出的内容
字符串切片:
>>> a = 'hello world'
>>> a[0:5]
'hello'
>>> a[0:5:2] //在0到5的范围内以步长为2进行选取所需要的内容
'hlo'
>>> a[4:0:-1] //反向切片,锁切区间均为左闭右开区间
'olle'
>>> a[-4:0:-1] //倒叙切片
'ow olle'
比较字符串中数字大小和字符串所代表的数字大小:
In [1]: str1 = "1234567890"
In [2]: max(str1)
Out[2]: '9'
In [3]: min(str1)
Out[3]: '0'
In [4]: str1 = "12"
In [5]: str2 = "23"
In [6]: cmp(str1,str2)
Out[6]: -1 //前者小则输出为-1
In [7]: str1 = "23"
In [8]: cmp(str1,str2) //两个字符串大小相等则输出为0
Out[8]: 0
In [9]: str1 = "24"
In [10]: cmp(str1,str2)
Out[10]: 1 //前者大则输出为1
元组:
提取整条信息更加方便,字符串适用切片,索引等
In [19]: user1 = ("fentiao",4,"male")
In [20]: user2 = ("westos",10,"company")
In [21]: user1[0]
Out[21]: 'fentiao'
In [22]: user1[2]
Out[22]: 'male'
In [23]: user1[1]
Out[23]: 4
元组不可分 :tulpe
n [53]: t = (1,2,3,1,3,2,1,5,6,7)
In [54]: t.
t.count t.index
In [54]: t.count(1) //输出1的个数
Out[54]: 3
In [55]: t.count(2)
Out[55]: 2
In [56]: t.index(2)
Out[56]: 1,
In [58]: t.index(3,1,5) //在请6个数中输出第一个3的位置,若第六个数出现所要的数,不可输出,开区间
Out[58]: 2
In [51]: a,b,c=1,2,3
In [52]: print a,b,c //可以同时定义多个数并输出
1 2 3
In [46]: t = ('fentiao', 8, 'male', 'fentiao')
In [47]: name, age, gender, kind = t //相当于赋值操作
In [50]: print name, age, gender, kind
fentiao 8 male fentiao
列表:[]
In [63]: li = [1,"hello",(1,2),[1,2,3,4]]
n [65]: li.
li.append li.extend li.insert li.remove li.sort
li.count li.index li.pop li.reverse
In [65]: li[1] //列表可直接输出指定位置的信息
Out[65]: 'hello'
In [67]: li[0] = "fendai" //列表可直接更改指定位置的信息
In [68]: li //输入列表名字即可输出列表内容
Out[68]: ['fendai', 'hello', (1, 2), [1, 2, 3, 4]]
n [69]: li.append("cat") //直接将
In [70]: li
Out[70]: ['fendai', 'hello', (1, 2), [1, 2, 3, 4], 'cat']
In [71]: li.extend([1,2,3,4])
In [72]: li
Out[72]: ['fendai', 'hello', (1, 2), [1, 2, 3, 4], 'cat', 1, 2, 3, 4]
In [73]: li.count(1) //列表中1出现的次数
Out[73]: 1
In [74]: li.pop(1) //弹出元组,列表中不再显示
Out[74]: 'hello'
In [75]: li.reverse //对列表中元素反向排序
:li.sort() //对列表中源序正向排序
>>> list ("str")
['s', 't', 'r']
>>> type([])
<type 'list'>
>>> list ((1,2,3,4))
[1, 2, 3, 4]
>>> list ({1,2,4,5}) //列表的定义方式和结果显示格式
[1, 2, 4, 5]
>>> list[(1,2,34,5)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object has no attribute '__getitem__'
>>> list ["str"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object has no attribute '__getitem__'
>>>
集合:set,与列表和元组的不同是,集合中的元素是无序的,也无法用数字进行索引,集合中的元素也是不重复的
s = set([1,2,3,4,3]) //创建一个数值集合
s = set("hello") //创建一个唯一字符有的集合
In [90]: s1 = {1,2,3,4,5}
In [91]: s2 = {1,2,3}
n [92]: s1.difference(s2)
Out[92]: {4, 5} //找出集和之间的不同
In [93]: s1.intersection(s2)
Out[93]: {1, 2, 3} //两集和交集
In [94]: s1.union(s2)
Out[94]: {1, 2, 3, 4, 5} //两集合并集
In [95]: s1
Out[95]: {1, 2, 3, 4, 5}
In [96]: s2
Out[96]: {1, 2, 3}
In [97]: s2.add(4) //给集合中添加元素,只可添加一项
In [98]: s2
Out[98]: {1, 2, 3, 4}
In [99]: s2.update([7,3,6]) //可以添加多项
In [100]: s2
Out[100]: {1, 2, 3, 4, 6, 7}
len(s2) //集合的长度
x not in s1 //测试x是否不是s1的成员
s1.issubset(t) //测试是否s1中的每一个元素都在t中
s1.issuberset(t) //测试是否t中的每一个元素都在s1中
s1.union(t) //返回一个新的集合包含s1和t中的每一个元素
s1.intersection(t) //返回一个新的集合包含s1和t中的公共元素
s.different(t) //返回一个新的集合高寒s1中有但t中没有的元素
s1.discard(4) //删除集合中的4元素
In [101]: s1.remove(7) //删除集合中的元素,如果不存在则报错
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-101-301152350457> in <module>()
----> 1 s1.remove(7)
KeyError: 7
In [102]: s1.discard(7) //删除集合中的元素
In [103]: s1
Out[103]: {1, 2, 3, 4, 5}
In [104]: s1.discard(2)
In [105]: s1
Out[105]: {1, 3, 4, 5}
In [106]: s1.pop() //删除并返回集合中的一个不确定的元素,如果为空则报错
Out[106]: 1
In [111]: s1
Out[111]: {1, 2, 4, 5, 6}
In [112]: s1.isdisjoint(s2) //有交集的话是false
Out[112]: False
In [113]: s2
Out[113]: {1, 2, 3, 4, 6, 7}
In [114]: s2.add(5)
In [115]: s2
Out[115]: {1, 2, 3, 4, 5, 6, 7}
In [116]: s1.issubset(s2) //测试是否s1中的每一个元素都在s2中
Out[116]: True
In [117]: s1.issuperset(s2) 测试是否s2中的每一个元素都在s1中
Out[117]: False
In [118]: s1
Out[118]: {1, 2, 4, 5, 6}
In [119]: s1.issuperset(s2)
Out[119]: False
In [120]: s2.issuperset(s1) //真子集显示true
Out[120]: True
In [121]: s2.issubset(s1) //子集显示true
Out[121]: False
In [122]: s1.issubset(s2)
Out[122]: True
添加下面的模块可用于在脚本中直接输入密码;
password = getpass.getpass("please input password:")
也可以在交互模式下使用:
In[100]:password = getpass.getpass("please input password:")
please input password :\
In[101]:print password
hello
脚本:
1.pathon 脚本实现加法运算;
#!/usr/bin/env python
#coding:utf-8
#coding=utf-8
#encoding:utf-8 //此五项仅仅需要一个就可以输出中文
#coding=utf-8
#_*_coding:utf-8-*-
a = raw_input("first num:")
b = raw_input("seconf num:")
print "结果=",int(a)+int(b)
1,输出偶数,奇数,并作判断
(1)
#!/bin/env python
#enconding:utf-8
print "打印出1~100之间的偶数:";
li=range(1,101)
print li[1:100;2]
print "打印出1~100之间的奇数:";
print li[0:100:2]
(2)
for i in range (1,100):
if i % 2 == 0:
print i //如果是偶数则输出
2.
判断字符串中是个否含有所输入字母(脚本)
vim panduan.py
#!/usr/bin/env python //提高脚本的可移植性,可以适应多个版
本的python
str = "abcde"
a = raw_input("please input a string:")
if a in str:
print "%s is in %s"%(a,str)
else :
print "false"
~
python
3.在脚本中实现加法器:
#!/usr/bin/env python
#conding:utf_8
num1 = raw_input("num1:")
operator = raw_input("please input a operator:")
num2 = raw_input("num2:")
if operator == "+":
print int(num1)+int(num2)
elif operator = "-":
print int(num1)-int(num2)
elif operator == “*”:
print int(num1)*int(num2)
elif operator == "/":
print int(num1)/int(num2)
else:
print "error"
4.用户登陆脚本:
#!/usr/bin/env python
#coding:utf-8
user = "westos"
passwd = "linux"
import getpass
for i in range(1,4) :
userName = raw_input("请输入账户名字:")
passWord = getpass.getpass("请输入密码:")
if userName == user and passWord == passwd :
print ("登陆成功!")
break
else :
i = i-1
print("帐号或密码错误,请重新登陆!")
print("你还有%d次登陆机会\n" %(2-i))
if i==2 :
print("你的3次机会已经用完,登录失败!")