Python Core Programming _ Chapter two exercises after class

Source: Internet
Author: User

Here are some of the post-lesson exercises that you do when you're learning python core programming. Now paste them out to record your learning process. Brother is a mechanical background, a lot of practice topics written is very mechanical. Although the script can be written to meet the requirements of the topic, but the efficiency may not be the best, so, the younger brother or the thick skin to put them, one can let the expert guidance, and secondly, and I can learn the same as the brothers in Python learning together.

The following programs are named after the title, such as 2-3, the program name is 2_3.py.

Exercise 2_3.py

#!/usr/bin/env Pythona = 10B = 4print "A plus B is:", print (A + B) printprint "A minus B is:", print (-a) print "a multi B" is: ", print (A * b) print" A divide B is: ", print (A/b) print" A mod B is: ", print (a% B) print" A POW B is: ", print (A * * b)

Exercise 2_4.py

#!/usr/bin/env pythonstr1 = raw_input () print "You'll get the string str1:", Print str1int1 = Int (raw_input ()) print "You w Ill get the Dicimal int1: ", print Int1


Exercise 2_5.py

#!/usr/bin/env Pythoni = 0while i < 11:print i, i + = 1 Printprint "The next example used range () function" for I in range (0, one): Print I,


Exercise 2_6.py

#!/usr/bin/env Pythona = 10if a > 0:print "A is positive" elif a < 0:print "A is negative" else:print "a I s equal 0 "printprint" Edition 2nd "a = Int (raw_input ()) If a > 0:print" A is positive "elif a < 0:print" A is n Egative "Else:print" A is equal 0 "


Exercise 2_7.py

#!/usr/bin/env pythonstr1 = str (raw_input ()) strlength = Len (str1) i = 0while i < strlength:print "%5d"% i, str1[i] i + = 1printprint '--------------' for eachstring in Str1:print eachstring


Exercise 2_8.py

#!/usr/bin/env pythonprint "while condition" print "---------------" alist = [1, 2, 3, 4, 5]i = 0iSum = 0while i < len (AL  IST): Isum + = Alist[i] i + = 1 print "Isum is:", isumprintprint "for condition" print "-------------" blist = [1, 2, 3, 4, 5]isum1 = 0for eachlist in Blist:isum1 + = Eachlistprint "ISum1 is:", iSum1


Exercise 2_9.py

#!/usr/bin/env Pythoni = 0aList = []while i < 5:alist.append (float (raw_input ())) i + = 1sumList = SUM (alist) Aveli st = Sumlist/len (alist) print "average is:", avelist


Exercise 2_10.py

#!/usr/bin/env pythonwhile true:d = Int (raw_input (' Please input an integer: ')) if D >= 1 and D <= 100: print ' successfully ' break else:continue


exercise 2_11.py

#!/usr/bin/env pythonprint  "s.  (S) um of five integers" print  "A.  (A) Verage five integers "print " X. e (X) it "mylist = []def getnumber ():     i = 0        while i < 5 :         num = int (Raw_input ())          print  "Input the %dth number:", i         mylist.append (num)         i += 1             return mylistdef mysum (x):     return sum (x) def myaverage (x):     return mysum (x)  / 5while true:    input_string = raw_input (' Choose Your  Function:  ')     if input_string ==  "S":         result =  mysum (GetNumber ())         print  ' The sum is ',  result            elif input_string  ==  "A":         result = myaverage (GetNumber ())         print  ' The average is ', result             elif input_string ==  "X":         break             else:        print  "No more sense! try  again! "


Exercise 2_13.py

#!/usr/bin/env pythonprint dir () import sysprint dir () print Sys.versionprint sys.platformsys.exit (0)


Exercise 2_15.py

#!/usr/bin/env Pythonmylist = []i = 0while i < 3:print ' input%dth number '% i myinput = Int (raw_input ()) MyL Ist.append (myinput) i + = 1print ' The original list is: ', mylistsortedlist = sorted (MyList) print ' sorted list is: ', Sort Edlistprint ' reversed list is: ', i = 2while i >= 0:print sortedlist[i], I-= 1

This article is from the "Tiandaochouqin" blog, make sure to keep this source http://lavenliu.blog.51cto.com/5060944/1564099

Python Core Programming _ Chapter two exercises after class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.