[关闭]
@Sarah 2016-04-03T14:00:40.000000Z 字数 1665 阅读 867

Python session 1

命名

区分大小写,字幕开头

  1. input = raw_input("Please enter your name")
  2. print input
  3. //function example
  4. def print_lyrics():
  5. print "Im a AAA"
  6. print 'I sleep'
  7. print_lyrics()
  8. //rerurn values
  9. def greet():
  10. return "Hello"
  11. print greet(), "Gleen"
  12. print greet(), "Sally"
  13. //while Loops
  14. n = 5
  15. while n>0:
  16. print b
  17. n = n-1
  18. print 'Blastoff!'
  19. print n
  20. //Breaking out of a loop
  21. n = 5
  22. while True:
  23. print n
  24. if n == 3:
  25. break
  26. //finishing an iteration with continue
  27. n = 5
  28. while n>0:
  29. n=n-1
  30. if n>=3:
  31. continue
  32. print n
  33. //For Loop
  34. for i in [5,4,3,2,1]:
  35. print i
  36. print 'Blastoff'
  37. //if elif else
  38. if x<y:
  39. print 'x is less than y'
  40. elif x>y:
  41. print 'x is greater than y'
  42. else:
  43. print 'x and y are equal'
  44. //try and catch
  45. inp = raw input("Enter Fahrenheit Temperature:')
  46. try:
  47. fahr = float(inp)
  48. cel = (fahr -32.0)*5.0/9.0
  49. print cet
  50. except:
  51. print 'please enter a number'
  52. //Strings
  53. str1 = 'H'
  54. str2 = 'T'
  55. bob = str1 + str2
  56. print bob
  57. str3 = '123'
  58. str3 = int(str3)+1
  59. print str3
  1. //index
  2. fruit = 'b'
  3. x = len(fruit)
  4. print x
  5. for letter in fruit:
  6. print letter
  7. //
  8. s = 'Monday Python'
  9. print s[0:4]
  10. print s[6:7]
  11. print s[6:20]
  12. s ='Monday Python'
  13. print s[:2]
  14. print s[8:]
  15. print s[:]
  16. //true anf false
  17. fruit = 'banana'
  18. 'n' in fruit
  19. 'm' in fruit
  20. 'nan' in fruit
  21. if 'a' in fruit:
  22. print 'Found it!'
  23. //String library
  24. str.capitalize() 首字母大写
  25. str.upper() 全部大写
  26. str.lower()小写
  27. str.find() finds the first occurrence of the substring.没找到的话返回-1
  28. str.replace()
  29. str.lstrip()
  30. str.strip()
  31. str.startswith()搜搜用啥开头的
  32. 字符串不能够改变。

Lists

list1 = [2,3]

//
range(4)
//
a = [1,2]
b= [3,4]
c = a+b
print c

//
list1.remove(2)
list1.append(2)

//常用列表函数
list.pop()
.reverse()
.sort()
.count()
.index()
.max()
.min()
.sum()

//
purse = dict()
purse['m'] = 12
purse['c']=5

//tuple
d = {'a':10,'b':1,'c':22}
t = d.items()
print t

for k.v in c.items():
temp.append((v,k))
print tmp

c = {'a':10,'b':2,'c':13}
print sorted c

```

正则表达

```python

import re
x = "wwwwweetjyuketgha34273i5"
y = re.findall('[0-9]',x)
print y

//
import re
x = "From: Using the : character"

//
y = re.findall('\s+@\s',x)

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