1、題目:列表轉換為字典。
程序源代碼:
1 #!/usr/bin/env python 2 # -*- coding: UTF-8 -*- 3 4 i = ['a', 'b'] 5 l = [1, 2] 6 print dict([i, l])
以上實例輸出結果為:
{'a': 'b', 1: 2}
2、一個簡單的while循環
1 #!/usr/bin/env python 2 3 count = 0 4 while (count < 9): 5 print 'The count is:', count 6 count = count + 1 7 8 print "good bye"
以上實例輸出的結果為:
The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 good bye
3、一個簡單的循環continue
1 #!/usr/bin/env python 2 i = 1 3 while i < 10: 4 i += 1 5 if i%2 > 0: 6 continue 7 print i
以上實例輸出的結果為:
2 4 6 8 10
4、break的用法
1 #!/usr/bin/env python 2 i = 1 3 while 1: 4 print i 5 i += 1 6 if i > 10: 7 break
以上實例的實驗結果為:
1 2 3 4 5 6 7 8 9 10
5、 一個無限循環的小例子
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 var = 1 5 while var == 1: # 該條件永遠為true,循環將無限執行下去 6 num = raw_input("Enter a number:") 7 print "You entered:", num 8 9 print "Good bye!"
以上實例輸出結果(使用ctrl + c 推出無限循環):
Enter a number:5 You entered: 5 Enter a number:6 You entered: 6 Enter a number:^CTraceback (most recent call last): File "wuxian.py", line 6, in <module> num = raw_input("Enter a number:") KeyboardInterrupt
6、循環使用else😁
1 #!/usr/bin/env python 2 3 count = 0 4 while count < 5: 5 print count, " is less than 5" 6 count = count + 1 7 else: 8 print count, " is not less than 5"
以上實例結果
0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5
7、題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
程序分析:以3月5日為例,應該先把前兩個月的加起來,然后再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大於3時需考慮多加一天:
程序源代碼:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 year = int(raw_input('year: ')) 5 month = int(raw_input('month: ')) 6 day = int(raw_input('day: ')) 7 8 months = (0,31,59,90,120,151,181,212,243,273,304,334) 9 if 0 < month <= 12: 10 sum = months[month - 1] 11 else: 12 print 'data error' 13 sum += day 14 leap = 0 15 if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): #能被400或者4整除,但不能被100整除是閏年 16 leap = 1 17 if (leap == 1) and (month > 2): 18 sum += 1 19 print 'it is the %dth day of this year.' % sum
以上實例的輸出結果:
year: 2016 month: 11 day: 2 it is the 307th day of this year.
8、題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。
程序分析:我們想辦法把最小的數放到x上,先將x與y進行比較,如果x>y則將x與y的值進行交換,然后再用x與z進行比較,如果x>z則將x與z的值進行交換,這樣能使x最小。
程序源代碼:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 l = [] 5 for i in range(3): 6 x = int(raw_input('integer: ')) 7 l.append(x) 8 l.sort() 9 print l
以上實例的輸出結果:
integer: 4 integer: 7 integer: 1 [1, 4, 7]
9、題目:將一個列表的數據復制到另一個列表中。😁
程序分析:使用列表[:]。
程序源代碼:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 a = [1, 2, 3] 5 b = a[:] 6 print b
以上實例輸出結果為:
[1, 2, 3]
10、題目:輸出9*9乘法口訣表。
程序分析:分行與列考慮,共9行9列,i控制行,j控制列。
程序源代碼:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 for i in range(1,2): 5 for j in range(1,10): 6 result = i * j 7 print '%d * %d = % -3d' % (i,j,result) 8 print ''
以上實例的結果:🙄
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9
11、python的標准輸出
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 name=input("name: ") 4 age=int(input("age: ")) 5 job=input("job: ") 6 salary=float(input("salary:")) 7 info2=''' 8 ==================info of {_name}================= 9 name:{_name} 10 age:{_age} 11 job:{_job} 12 salary:{_salary} 13 ================================================== 14 '''.format(_name=name, 15 _age=age, 16 _job=job, 17 _salary=salary) 18 print(info2)
12、一個python輸入密碼的小程序,在輸入密碼時,為了使密碼不可見,可以條用getpass模塊的getpass()方法。
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 import getpass 4 user =raw_input("請輸入用戶名:") 5 pwd=getpass.getpass("請輸入密碼:") 6 print(user, pwd)
以上實例的輸出結果:
# python passwd.py 請輸入用戶名:wangtao 請輸入密碼: ('wangtao', '123456')
13、使用sys模塊的小例子
1 #!/usr/bin/env python 2 from sys import argv 3 print argv
以上實例的輸出結果:
# python argvtest.py 1 2 3 4 5 ['argvtest.py', '1', '2', '3', '4', '5']
14、猜數字的小游戲
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 4 time=0 5 real_age=23 6 while time<3: 7 guess_age=int(raw_input("請猜猜我的真實年齡,請輸入猜測的年齡:")) 8 if guess_age==real_age: 9 print("哈哈,你真聰明猜對了!") 10 break 11 elif guess_age>real_age: 12 print("數字太大了,請猜小一點!") 13 else: 14 print("數字有些小,請猜大一點!") 15 time += 1 16 if time==3: 17 continue_flag = raw_input("還要繼續往下猜嗎?(yes or no)") 18 if continue_flag == "y": 19 time=0 20 else: 21 print("退出系統!\n") 22 break
以上實例的輸出結果:
# python guess_agetest.py 請猜猜我的真實年齡,請輸入猜測的年齡:35 數字太大了,請猜小一點! 請猜猜我的真實年齡,請輸入猜測的年齡:232 數字太大了,請猜小一點! 請猜猜我的真實年齡,請輸入猜測的年齡:34 數字太大了,請猜小一點! 還要繼續往下猜嗎?(yes or no)y 請猜猜我的真實年齡,請輸入猜測的年齡:12 數字有些小,請猜大一點! 請猜猜我的真實年齡,請輸入猜測的年齡:23 哈哈,你真聰明猜對了!
15、for循環的小例子
#!/usr/bin/env python # _*_ coding:utf8 _*_ #author:snate for i in range(0,10,3): print("loop",i) if i>=6: break
以上實例的輸出結果為:
# python for.py ('loop', 0) ('loop', 3) ('loop', 6)
16、顯示目錄文件
>>> import subprocess >>> subprocess.call(["ls","-l","/tmp/"]) total 84 -rw-r--r-- 1 root root 1702 Feb 24 10:44 6379.conf -r--r--r-- 1 root root 74812 Oct 25 10:53 cronie-1.4.4-12.el6.x86_64.rpm drwxr-xr-x 2 root root 4096 Feb 24 16:46 hsperfdata_root
17. python包裝ls命令
#!/usr/bin/env python #python wrapper for the ls command import subprocess subprocess.call(["ls","-l","/tmp"])
18. 顯示系統信息腳本
#!/usr/bin/env python # A system information gathering script import subprocess #command 1 uname = "uname" uname_arg = "-a" print "Gathering system information with %s command:\n" % uname subprocess.call([uname, uname_arg]) #command 2 diskspace = "df" diskspace_arg = "-h" print "Gathing diskspace information %s command:\n" % diskspace subprocess.call([diskspace,diskspace_arg])