Python初见

参考资料:http://wenku.baidu.com/link?url=_akpT-G5Tvf7ECyszSipOAhHXzjlpYu-RWPcRTYp_tecPOollPGUxXG4MH69MLNEgWf1aVLFCz65QxL-yph3r0MFCiHJ81I3SWHM8yhBl4O

下面是一些语法:

  1 __author__ = 'wrq'
  2 #coding = utf-8
  3 
  4 import time
  5 print("Hello!")
  6 
  7 '定义函数'
  8 def test():
  9     nowTime = time.time()
 10     a = 10
 11     a += 1
 12     b = "test string"
 13     print(b)
 14 
 15 '分支'
 16 love = False
 17 if love== True:
 18     test()
 19 else:
 20     print("didn't invoke function test!")
 21 
 22 '循环'
 23 for i in range(4):
 24     print(i)
 25 
 26 print("next loop")
 27 
 28 '起始,终止,步长'
 29 for j in range(1,10,2):
 30     print(j)
 31 
 32 """
 33 a = input("请输入: ")
 34 print("您的输入是:")
 35 print(int(a))
 36 """
 37 
 38 length = len("I begin to learn python,interesting.")
 39 print(length)
 40 
 41 '将对象转换成string'
 42 str(1000)
 43 print(str)
 44 
 45 '列表'
 46 a = [1,2,4,8]
 47 for v in a :
 48     print(v)
 49 a.append(16)
 50 for v in a:
 51     print(v)
 52 a.insert(1,-1)
 53 for v in a:
 54     print(v)
 55 a.remove(4)
 56 for v in a :
 57     print(v)
 58 print(a.index(2,1,4))
 59 print(a[2])
 60 
 61 '元组'
 62 yuanZu = (1,2)
 63 print(yuanZu[0])
 64 
 65 '字典'
 66 addressBook = {
 67     'Li':'li from china.',
 68     'larry':'larry is not here',
 69     'Sam':'uncle Sam'
 70 }
 71 print('something about larry %s' %addressBook['larry'])
 72 'add'
 73 addressBook['Li'] = 'Guidi is female'
 74 'delete'
 75 del addressBook['Sam']
 76 print("
 there are %d contacts in the address book
"%len(addressBook))
 77 for name,address in addressBook.items():
 78     print("contact %s at %s"%(name,address))
 79 if "Li" in addressBook:
 80     print("
 its information is %s"%addressBook["Li"])
 81 
 82 "序列"
 83 a = "I love python."
 84 print(a[0])
 85 print(a[0:1])
 86 print(a[5:])
 87 print(a[:-1])
 88 
 89 "模块"
 90 # !/usr/bin/python
 91 import sys
 92 print("The command line arguments are:")
 93 for i in sys.argv:
 94     print(i)
 95 print("/n/n the PYTHONPATH is",sys.path,"/n")
 96 
 97 import _md5
 98 "m = _md5(""iloveyou)"
 99 "print(m.digest())"
100 "print(m.hexdigest())"
101 
102 "文件"
103 file = open("D:\firstPython\file.txt")
104 for line in file.readlines():
105     print(line)
106 
107 "数据库编程"
原文地址:https://www.cnblogs.com/qingcheng/p/3458252.html