[关闭]
@dragonfive 2015-07-23T02:47:58.000000Z 字数 3051 阅读 634

python基础教程

python编程


算术运算

整数除法

在python3.0以后,/的结果不论是什么都是浮点数。
在python3.0以前。

  1. >>> 1/2
  2. 0

这里用的是整除,如果想执行普通的除法,常用的做法有两种 :
1. 其中一个因子用浮点数
2. 在程序前引入一个模块

  1. >>> from __future__ import division
  2. >>> 1/2
  3. 0.5

记住这里future前有两个下划线,后面也有两个下划线。
但引入这个模块之后,就不能执行整除了,这时候可以使用双除号来执行整除,当然也可以在没引入这个模块的时执行双除号。

  1. >>> 1/2
  2. 0
  3. >>> 1//2
  4. 0
  5. >>> 1.0/2
  6. 0.5
  7. >>> 1.0//2
  8. 0.0

乘方运算

**表示乘方

大整数

python支持大整数,结尾加L

常用函数

print

  1. >>> print('hello')
  2. hello
  3. >>>

python可以输出多个内容,用逗号隔开,再输出的时候逗号的地方输出空格。

  1. >>> name = '张三'
  2. >>> print("hello",name)
  3. hello 张三
  4. >>>

input

用input函数可以实现标准化输入,默认接受的作为字符串存储;

  1. >>> name = input()
  2. '李四'
  3. >>> name
  4. "'李四'"
  5. >>> name = input()
  6. 3
  7. >>> name
  8. '3'
  9. >>>

编码问题

utf-8编码

本着节约的精神,UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符,用UTF-8编码就能节省空间:
UTF-8编码有一个额外的好处,就是ASCII编码实际上可以被看成是UTF-8编码的一部分,所以,大量只支持ASCII编码的历史遗留软件可以在UTF-8编码下继续工作。

内存网页unicode,传输保存用utf-8

国内网站的也谢网页传输貌似用的是GB2312,比如西电睿思就是。
网页文件编码
文本文件编码

网页文件编码
网页文件

python字符串与字节数组

python字符串编码用unicode
通过encode()方法,可以把字符串按指定的编码编为字节数组。

用decode()方法,可以把字节数组按指定的编码编为字符串。

  1. str = 'china的中文是中国'
  2. bt = str.encode('Utf-8')
  3. print(bt.decode('Utf-8'))

用len()方法可以求元素个数

条件语句与循环

  1. #条件判断
  2. age = input("请输入你的年龄")
  3. age = int(age)
  4. if age>=6:
  5. print("teenage")
  6. elif age>=18:
  7. print('adult')
  8. else:
  9. print('child')
  10. print('over');
  11. #条件判断二
  12. height=float(input("输入身高:"))
  13. weight=float(input("输入体重:"))
  14. ratio=weight/(height**2)
  15. if ratio<18.5:
  16. print("过轻")
  17. elif ratio>=18.5 and ratio<25:
  18. print("正常")
  19. elif ratio>=25 and ratio<28:
  20. print("过重")
  21. elif ratio>=28 and ratio<32:
  22. print("肥胖")
  23. elif ratio>=32:
  24. print("严重肥胖")
  25. #循环语句
  26. sum=0
  27. L = ['Bart', 'Lisa', 'Adam']
  28. for name in L:
  29. print("hello",name)
  30. for i in range(len(L)):
  31. print("hello",L[i])
  32. i=len(L)
  33. while i>0:
  34. print("hello",L[-1*i])
  35. i=i-1

python中貌似没有自增运算。

list与tuple

list用[] tuple用()
list可变,tuple直接元素不可变。
list的方法有append,insert,pop,len
list[-1]表示去倒数第一个元素,list[-2]表示取倒数第二个元素。

python中没有自增运算。

dict

dict用{}

新增元素两种方法

  1. myDict = {"Canada" : 1,"Rusia" : 2.2,3 : "china"}
  2. myDict["America"]=4.0
  3. print(myDict[3])

取元素两种方法

  1. myDict = {"Canada":1,"Rusia" : 2.2,3 : "china"}
  2. myDict["America"]=4.0
  3. #print(myDict[5])
  4. a=myDict.get(3,-1)
  5. if a==-1:
  6. print("不存在这个配对")
  7. else:
  8. print(a)

get方法更安全。如果失败是返回一个none,也可以自己指定,这里指定为-1.

删除元素

pop函数; 

  1. >>> myDict
  2. {'America': 4.0, 3: 'china', 'Rusia': 2.2, 'Canada': 1}
  3. >>> pop(3)
  4. Traceback (most recent call last):
  5. File "<pyshell#10>", line 1, in <module>
  6. pop(3)
  7. NameError: name 'pop' is not defined
  8. >>> myDict.pop(3)
  9. 'china'
  10. >>> myDict
  11. {'America': 4.0, 'Rusia': 2.2, 'Canada': 1}
  12. >>>

set

set用set([])来定义,与dict一样,不存在相同的元素,不过没有value,元素同样不能是list等可变的东西,字符串等才是不可变的东西。

  1. >>> c=[3,4]
  2. >>> s=set([1,2,c])
  3. Traceback (most recent call last):
  4. File "<pyshell#39>", line 1, in <module>
  5. s=set([1,2,c])
  6. TypeError: unhashable type: 'list'
  7. >>>

add添加元素
remove删除元素
两个set之间可以 &(求交集) |(求并集)

定义函数

语法

  1. import math #引入包
  2. def quadratic(a,b,c): #def声明函数 定义函数时,需要确定函数名和参数个数;
  3. if not isinstance(a,(int,float)): #用来做类型检测
  4. raise TypeError('a bad operand type')
  5. if not isinstance(b,(int,float)):
  6. raise TypeError('b bad operand type')
  7. if not isinstance(c,(int,float)):
  8. raise TypeError('c bad operand type')
  9. if a==0:
  10. return -c/b
  11. if pow(b,2)<4*a*c:
  12. print("不存在实数解")
  13. return None #return None可以简写作return
  14. sqrt_value=math.sqrt(pow(b,2)-4*a*c)
  15. x1=(-b+sqrt_value)/(2*a);
  16. x2=(-b-sqrt_value)/(2*a);
  17. return x1,x2
  18. print(quadratic(2, 3, 1)) # => (-0.5, -1.0)
  19. print(quadratic(1, 3, -4)) # => (1.0, -4.0)
  20. print(quadratic(0, 2, 2)) # => (-1.0)
  21. print(quadratic(4, 1, 2)) # => '不存在实数解'
  22. pass #表示占位

默认参数

传参顺序

形参指向不可变内容;

  
  
  
  
  
  未完

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注