I recommend my own artificial intelligence Python Learning group :[672948930
], There's a copy I sorted out about pytorch,python Basics , image processing opencv\ natural language processing , machine learning , Mathematics foundation and other resource database , Want to learn artificial intelligence or switch to high salary industry , College students are very practical , No routines are available free of charge ! just so so
Code scanning plus VX Get the information !
Text start :
1, subject : Convert list to dictionary .
program source code :
1 #!/usr/bin/env python
2 #-*- coding: UTF-8 -*-
3
4 i = ['a', 'b']5 l = [1, 2]6 printdict([i, l])
The output result of the above example is :
{'a': 'b', 1: 2}
2, A simple while loop
1 #!/usr/bin/env python
2
3 count =04 while (count < 9):5 print 'The count is:', count6 count = count + 1
7
8 print "good bye"
The output result of the above example is :
The count is: 0
The countis: 1The countis: 2The countis: 3The countis: 4The countis: 5The
countis: 6The countis: 7The countis: 8good bye
3, A simple loop 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
The output result of the above example is :
2
4
6
8
10
4,break Usage of
1 #!/usr/bin/env python
2 i = 1
3 while 1:4 printi5 i += 1
6 if i > 10:7 break
The experimental results of the above examples are :
1
2
3
4
5
6
7
8
9
10
5, A small example of an infinite loop
1 #!/usr/bin/python
2 #-*- coding: UTF-8 -*-
3
4 var = 1
5 while var == 1: # The condition is always true, The loop will execute indefinitely
6 num = raw_input("Enter a number:")7 print "You entered:", num8
9 print "Good bye!"
Output results of the above examples ( use ctrl + c Launch infinite loop ):
Enter a number:5You entered:5Enter a number:6You entered:6Enter a
number:^CTraceback (most recent call last):
File"wuxian.py", line 6, in num= raw_input("Enter a number:")
KeyboardInterrupt
6, Recycling else�
1 #!/usr/bin/env python
2
3 count =04 while count < 5:5 print count, "is less than 5"
6 count = count + 1
7 else:8 print count, "is not less than 5"
The above example results
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, subject : Enter a year, month and day , Judge which day of the year this day is ?
Program analysis : with 3 month 5 Take Japan as an example , You should add up the first two months first , Then add 5 Day is the day of the year , exceptional case , Leap year and input month is greater than 3 One more day should be considered :
program source code :
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 +=day14 leap =015 if (year % 400 == 0) or ((year % 4 == 0) and (year %
100 != 0)): # Can be 400 perhaps 4 to be divisible by , But it can't be 100 Division is a leap year
16 leap = 1
17 if (leap == 1) and (month > 2):18 sum += 1
19 print 'it is the %dth day of this year.' %sum
Output results of the above examples :
year: 2016month:11day:2itis the 307th day of this year.
8, subject : Enter three integers x,y,z, Please output these three numbers from small to large .
Program analysis : We'll find a way to put the smallest number in x upper , First x And y Compare , If x>y Will x And y Exchange values for , Then use it again x And z Compare , If x>z Will x And z Exchange values for , This will enable x minimum .
program source code :
1 #!/usr/bin/python
2 #-*- coding: UTF-8 -*-
3
4 l =[]5 for i in range(3):6 x =
int(raw_input('integer:'))7l.append(x)8l.sort()9 print l
Output results of the above examples :
integer: 4integer:7integer:1[1, 4, 7]
9, subject : Copy data from one list to another .�
Program analysis : Use list [:].
program source code :
1 #!/usr/bin/python
2 #-*- coding: UTF-8 -*-
3
4 a = [1, 2, 3]5 b =a[:]6 print b
The output result of the above example is :
[1, 2, 3]
10, subject : output 9*9 Multiplication formula table .
Program analysis : Branch and column considerations , common 9 that 's ok 9 column ,i Control line ,j Control column .
program source code :
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 *j7 print '%d * %d =
% -3d' %(i,j,result)8 print ''
The results of the above examples :�
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 Standard output of
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, One python Applet for entering password , When entering the password , To make the password invisible , Can be used getpass Modular getpass() method .
1 #!/usr/bin/env python
2 #_*_ coding:utf-8 _*_
3 importgetpass4 user =raw_input(" enter one user name :")5 pwd=getpass.getpass(" Please input a password :")6
print(user, pwd)
Output results of the above examples :
#python passwd.py
enter one user name :wangtao
Please input a password :
('wangtao', '123456')
13, use sys Small example of module
1 #!/usr/bin/env python
2 from sys importargv3 printargv
Output results of the above examples :
#python argvtest.py 1 2 3 4 5
['argvtest.py', '1', '2', '3', '4', '5']
14, Guessing numbers games
1 #!/usr/bin/env python
2 #_*_ coding:utf-8 _*_
3
4 time=05 real_age=23
6 while time<3:7 guess_age=int(raw_input(" Please guess my real age , Please enter your age :"))8 if
guess_age==real_age:9 print(" ha-ha , You're smart. You guessed right !")10 break
11 elif guess_age>real_age:12 print(" The number is too big , Please guess smaller !")13 else:14
print(" The number is a little small , Please guess bigger !")15 time += 1
16 if time==3:17 continue_flag = raw_input(" Do you want to continue to guess ?(yes or no)")18 if
continue_flag == "y":19 time=020 else:21 print(" Exit the system ! ")22 break
Output results of the above examples :
#python guess_agetest.py
Please guess my real age , Please enter your age :35 The number is too big , Please guess smaller !
Please guess my real age , Please enter the age of the guess :232 The number is too big , Please guess smaller !
Please guess my real age , Please enter your age :34 The number is too big , Please guess smaller !
Do you want to continue to guess ?(yesorno)y
Please guess my real age , Please enter your age :12 The number is a little small , Please guess bigger !
Please guess my real age , Please enter your age :23 ha-ha , You're smart. You guessed right !
15,for Small example of loop
#!/usr/bin/env python#_*_ coding:utf8 _*_#author:snate
for i in range(0,10,3):print("loop",i)if i>=6:break
The output result of the above example is :
#python for.py
('loop', 0)
('loop', 3)
('loop', 6)
16, Show catalog files
>>> importsubprocess>>> subprocess.call(["ls","-l","/tmp/"])
total84
-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 packing ls command
#!/usr/bin/env python#python wrapper for the ls command
importsubprocess
subprocess.call(["ls","-l","/tmp"])
18. Display system information script
#!/usr/bin/env python#A system information gathering script
importsubprocess#command 1
uname = "uname"uname_arg= "-a"
print "Gathering system information with %s command: " %uname
subprocess.call([uname, uname_arg])#command 2
diskspace = "df"diskspace_arg= "-h"
print "Gathing diskspace information %s command: " %diskspace
subprocess.call([diskspace,diskspace_arg])
I recommend my own artificial intelligence Python Learning group :[672948930
], There's a copy I sorted out about pytorch,python Basics , image processing opencv\ natural language processing , machine learning , Mathematics foundation and other resource database , Want to learn artificial intelligence or switch to high salary industry , College students are very practical , No routines are available free of charge ! just so so
Code scanning plus VX Get information !
Technology
Daily Recommendation