@dragonfive
2015-07-23T02:47:58.000000Z
字数 3051
阅读 634
python编程
在python3.0以后,/的结果不论是什么都是浮点数。
在python3.0以前。
>>> 1/20
这里用的是整除,如果想执行普通的除法,常用的做法有两种 :
1. 其中一个因子用浮点数
2. 在程序前引入一个模块
>>> from __future__ import division>>> 1/20.5
记住这里future前有两个下划线,后面也有两个下划线。
但引入这个模块之后,就不能执行整除了,这时候可以使用双除号来执行整除,当然也可以在没引入这个模块的时执行双除号。
>>> 1/20>>> 1//20>>> 1.0/20.5>>> 1.0//20.0
**表示乘方
python支持大整数,结尾加L
>>> print('hello')hello>>>
python可以输出多个内容,用逗号隔开,再输出的时候逗号的地方输出空格。
>>> name = '张三'>>> print("hello",name)hello 张三>>>
用input函数可以实现标准化输入,默认接受的作为字符串存储;
>>> name = input()'李四'>>> name"'李四'">>> name = input()3>>> name'3'>>>
本着节约的精神,UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符,用UTF-8编码就能节省空间:
UTF-8编码有一个额外的好处,就是ASCII编码实际上可以被看成是UTF-8编码的一部分,所以,大量只支持ASCII编码的历史遗留软件可以在UTF-8编码下继续工作。
国内网站的也谢网页传输貌似用的是GB2312,比如西电睿思就是。
网页文件编码
网页文件编码
python字符串编码用unicode
通过encode()方法,可以把字符串按指定的编码编为字节数组。
用decode()方法,可以把字节数组按指定的编码编为字符串。
str = 'china的中文是中国'bt = str.encode('Utf-8')print(bt.decode('Utf-8'))
用len()方法可以求元素个数
#条件判断age = input("请输入你的年龄")age = int(age)if age>=6:print("teenage")elif age>=18:print('adult')else:print('child')print('over');#条件判断二height=float(input("输入身高:"))weight=float(input("输入体重:"))ratio=weight/(height**2)if ratio<18.5:print("过轻")elif ratio>=18.5 and ratio<25:print("正常")elif ratio>=25 and ratio<28:print("过重")elif ratio>=28 and ratio<32:print("肥胖")elif ratio>=32:print("严重肥胖")#循环语句sum=0L = ['Bart', 'Lisa', 'Adam']for name in L:print("hello",name)for i in range(len(L)):print("hello",L[i])i=len(L)while i>0:print("hello",L[-1*i])i=i-1
python中貌似没有自增运算。
list用[] tuple用()
list可变,tuple直接元素不可变。
list的方法有append,insert,pop,len
list[-1]表示去倒数第一个元素,list[-2]表示取倒数第二个元素。
python中没有自增运算。
dict用{}
myDict = {"Canada" : 1,"Rusia" : 2.2,3 : "china"}myDict["America"]=4.0print(myDict[3])
myDict = {"Canada":1,"Rusia" : 2.2,3 : "china"}myDict["America"]=4.0#print(myDict[5])a=myDict.get(3,-1)if a==-1:print("不存在这个配对")else:print(a)
get方法更安全。如果失败是返回一个none,也可以自己指定,这里指定为-1.
pop函数;
>>> myDict{'America': 4.0, 3: 'china', 'Rusia': 2.2, 'Canada': 1}>>> pop(3)Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>pop(3)NameError: name 'pop' is not defined>>> myDict.pop(3)'china'>>> myDict{'America': 4.0, 'Rusia': 2.2, 'Canada': 1}>>>
set用set([])来定义,与dict一样,不存在相同的元素,不过没有value,元素同样不能是list等可变的东西,字符串等才是不可变的东西。
>>> c=[3,4]>>> s=set([1,2,c])Traceback (most recent call last):File "<pyshell#39>", line 1, in <module>s=set([1,2,c])TypeError: unhashable type: 'list'>>>
add添加元素
remove删除元素
两个set之间可以 &(求交集) |(求并集)
import math #引入包def quadratic(a,b,c): #def声明函数 定义函数时,需要确定函数名和参数个数;if not isinstance(a,(int,float)): #用来做类型检测raise TypeError('a bad operand type')if not isinstance(b,(int,float)):raise TypeError('b bad operand type')if not isinstance(c,(int,float)):raise TypeError('c bad operand type')if a==0:return -c/bif pow(b,2)<4*a*c:print("不存在实数解")return None #return None可以简写作returnsqrt_value=math.sqrt(pow(b,2)-4*a*c)x1=(-b+sqrt_value)/(2*a);x2=(-b-sqrt_value)/(2*a);return x1,x2print(quadratic(2, 3, 1)) # => (-0.5, -1.0)print(quadratic(1, 3, -4)) # => (1.0, -4.0)print(quadratic(0, 2, 2)) # => (-1.0)print(quadratic(4, 1, 2)) # => '不存在实数解'pass #表示占位
传参顺序
形参指向不可变内容;
未完