如果想让代码中可以输入中文,必须要加上这一句。
1 2 3
   | def add_function(a, b):     c = a + b     print c
   | 
 
Python 中定义一个函数用 def 关键字,函数没有大括号,用tab空格代表方法体。
1 2 3 4 5
   | num = 100 tempC = "Number: " print tempC + `num` print tempC + str(num) print tempC + repr(num)
   | 
 
str和repr结果都一样,区别: str是 对象类型 ;repr是 函数 
1 2
   | print "one is %s"%num print "tempA: %s, tempB: %s"%(tempA, tempB)
   | 
 
如果是两个占位符,要写在括号内,用逗号隔开。
1 2 3 4 5 6 7 8 9
   | wordsA = " this is a book " print wordsA[0] print wordsA[-3]
 
  print wordsA[1:4] print wordsA[1:]  print wordsA[:]  print wordsA[:4] 
   | 
 
1 2 3
   | print wordsA.strip() print wordsA.lstrip() print wordsA.rstrip()
   | 
 
strip() 去掉字符串左右的空格,lstrip() 去掉左侧的空格,rstrip() 去掉右侧的空格。
表示5除以2,返回了商和余数
1 2
   | print 10 ** 2  print 9 // 2 
   | 
 
** 幂 - 返回 x 的 y 次幂
// 取整除 - 返回商的整数部分
1 2 3 4
   |  print 4 > 3 and 4 < 2  print 4 > 3 or 4 < 2  print not(4 < 2) 
 
  | 
 
与 and ,或 or ,非 not
1 2 3 4 5 6
   | if 4 > 2:     print "4 > 2" elif 4 == 2:      print "4 == 2" else:     print "4 < 2"
   | 
 
python 中的 else if 写成 elif
1 2
   | ary = [1, 2, 3, 4] print type(ary) 
   | 
 
python 的数组叫做 list
1 2 3 4 5
   | ary.append("Test") print ary ary2 = ["Test1", "ddd"] ary.extend(ary2) print ary
  | 
 
python list 类型添加一个元素 append() ,合并两个 list extend()。
1 2 3 4 5 6
   | print ary.count("ddd") print ary[4:] print ary[4] print ary.index("ddd") ary.insert(2, 2.3) print ary
  | 
 
count() 用于计算某个元素出现的次数,这里计算 ddd 出现了几次。
index() 查看 ddd 的下标。
insert() 在指定地方插入元素。
1 2 3 4 5 6 7 8 9 10
   | if 2.3 in ary:     ary.remove(2.3)     print "2.3在list中:"     print ary else:     print "2.3不在list中"
 
 
 
 
   | 
 
1 2 3 4 5 6 7 8
   | ary.pop() print ary ary.pop(3) print ary
 
 
 
 
   | 
 
pop() 删除list中的最后一个元素。
pop(3) 删除list中3这个元素,如果有多个,就删除最前面的一个。
1 2 3 4 5 6
   | print range(9) print range(2, 10, 2)  print range(0, 10, 3) print range(5, 100, 5)
  print range(0, -9, -1)
   | 
 
range(start, stop[, step])
start:开始数值,默认为0,也就是如果不写这项,就是认为start=0
stop:结束的数值,必须要写的。
step:变化的步长,默认是1,也就是不写,就是认为步长为1。坚决不能为0
1 2 3 4 5 6 7 8
   |  number = [1, 4, 6, 2, 9, 7, 3] number.sort() print number print sorted(number) number.sort(reverse=True) print number print sorted(number, reverse=True)
 
  | 
 
reverse=True 倒序排列
1 2 3
   | power = [] for x in range(9):     power.append(x ** 2)
   | 
 
在Python中,上面的代码可以改成如下形式:
1
   | print [x ** 2 for x in range(9)]
   | 
 
类似的,还有如下的形式:
1 2 3 4
   | print [n for n in range(3, 100) if n % 3 == 0] 
  mybag = [' glass',' apple','green leaf ']               print [one.strip() for one in mybag] 
   | 
 
1 2 3
   | weak = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] for (idx, item) in enumerate(weak):     print "idx: " + str(idx) + "; item: " + item
   | 
 
list中的迭代器
1 2 3 4 5 6
   | dicNum = {} print type(dicNum) dicNum["name"] = "Tom" dicNum[1] = "Tom" dicNum[2] = 90 print dicNum
  | 
 
字典类型,用大括号表示。
也可以这样创建字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | name = (["first", "Google"], ["second", "Yahoo"]) webSite = dict(name) print webSite		
 
  webSiteTest = {}.fromkeys(("third", "forth"), "facebook") print webSiteTest
 
  a = dict(one=1, two=2, three=3) b = {'one': 1, 'two': 2, 'three': 3} c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) d = dict([('two', 2), ('one', 1), ('three', 3)]) e = dict({'three': 3, 'one': 1, 'two': 2}) print a		 print b		 print c		 print d		 print e		
   | 
 
1
   | dicNum.update(webSiteTest)
   | 
 
将 webSiteTest 合并到 dicNum。
1 2
   | t = 1, 3, 4, 123, "test"
 
   | 
 
自动封装成元组类型,元组类型是不可变的。
元组的使用:
一般认为,tuple有这类特点,并且也是它使用的情景:
- Tuple 比 list 操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用 tuple 代替 list。
 
- 如果对不需要修改的数据进行 “写保护”,可以使代码更安全。使用 tuple 而不是 list 如同拥有一个隐含的 assert 语句,说明这一数据是常量。如果必须要改变这些值,则需要执行 tuple 到 list 的转换 (需要使用一个特殊的函数)。
 
- Tuples 可以在 dictionary 中被用做 key,但是 list 不行。实际上,事情要比这更复杂。Dictionary key 必须是不可变的。Tuple 本身是不可改变的,但是如果您有一个 list 的 tuple,那就认为是可变的了,用做 dictionary key 就是不安全的。只有字符串、整数或其它对 dictionary 安全的 tuple 才可以用作 dictionary key。
 
- Tuples 可以用在字符串格式化中,后面会用到。
 
set可以用{}定义;
- 元素没有序列,不能重复(类似于dict);
 
- 可以原处修改,但是不能通过下标的方式修改(类似于list,事实上是一种特别的set可以原处修改,另一种是不可以的);
 
1 2
   | s1 = set("qiwsir") print s1
  | 
 
输出内容:set(['q', 'i', 's', 'r', 'w']) 
将字符串拆开,并且没有重复的字母。
1 2 3 4
   | s3 = {"abc", "abc", "21", 33} print s3
  s4 = {} 
  | 
 
1 2 3
   | s6 = set(['a', 'b']) s7 = set(['github', 'qiwsir']) s6.update(s7)
   | 
 
合并s7到s6, s7不变。
从set中任意选择一个删除,并返回该值, pop() 不能有参数。
1 2 3
   | if "github" in s6:     s6.remove("github") print s6
   | 
 
想要删除特定的元素,需要使用 remove(obj) 函数。
并且obj必须在set中是存在的,否则报错。
discard(obj) 跟remove类似,不同之处在于,如果删除的元素在set中不存在则 discard(obj) 函数什么也不做。
在Python中变量有如下特殊的赋值方式:
1 2 3
   | name, web = "qiwsir", "qiwsir.github.io" 
  one, two, three, four = "good" 
   |