Getting started with Python 100 examples

Source: Internet
Author: User
Tags bitwise cos ord sin

Original link: http://www.cnblogs.com/CheeseZH/archive/2012/11/05/2755107.html

No matter what computer language to learn, as long as the 100 cases of most of the topics are done once, the basic mastery of the language grammar.

"Program 1"

Title: There are 1, 2, 3, 4 numbers, can make up how many different and no repetition of the number of three digits? How much are they?

#Filename: 001.pycnt = 0#count The sum of resultfor I in range (1,5): for    J in Range (1,5): for        K in range (1,5):            If I!=j and I!=k and j!=k:                print i*100+j*10+k                cnt+=1print cnt

"Program 2"

Title: The bonuses awarded by the Enterprise are based on the profit commission. Profit (I) less than or equal to $100,000, the bonus can be raised by 10%, the profit is higher than $100,000, less than $200,000, less than 100,000 of the portion of the 10% commission, higher than the portion of 100,000 yuan, Cocoa Commission 7.5%, 200,000 to 400,000, higher than 200,000 yuan of the portion, can commission 5% ; Between 400,000 and 600,000 is higher than the portion of 400,000 yuan, can commission 3%, 600,000 to 1 million, higher than 600,000 yuan portion, can commission 1.5%, higher than 1 million yuan, the portion of more than 1 million yuan by 1% Commission, from the keyboard input month profit I, the total bonus should be issued?

1 #Filename: 002.py 2 i = Int (raw_input (' Enter the Profit: ')) 3 arr = [1000000,600000,400000,200000,100000,0] 4 rat = [0.01 , 0.015,0.03,0.05,0.075,0.1] 5 r = 0 6 for IDX in range (0,6): 7     if I>ARR[IDX]: 8         r+= (I-arr[idx]) *rat[idx] 9         Print (I-arr[idx]) *rat[idx]10         i=arr[idx]11 Print R

"Program 3"

Title: An integer, which plus 100 is a complete square number, plus 168 is a complete square number, what is the number?

"Thank you to the 12 floor of the classmate Peiqianggao provide code"

#-*-Coding:utf-8-*-"Created on 2015-6-7# third question: An integer, which plus 100 is a complete square number, plus 168 is a complete square number, how much is the number @author:administrator ' ' Import mathnum = 1while True:    if math.sqrt (num + +)-int (math.sqrt (num +)) = = 0 and math.sqrt (num + 268)-int (MA TH.SQRT (num + 268)) = = 0:        print (num) break    num + = 1

"Program 4"

Title: Enter a certain day of the year, judging the day is the first of the year?

1 #author: Rebel 2 import datetime3 import time4 dtstr = str (raw_input (' Enter the DateTime: (20151215): ')) 5 dt = Datetime.datet Ime.strptime (Dtstr, "%y%m%d") 6 another_dtstr =dtstr[:4] + ' 0101 ' 7 Another_dt = Datetime.datetime.strptime (another_ Dtstr, "%y%m%d") 8 Print (int ((DT-ANOTHER_DT). Days) + 1)

"Program 5"

Title: Enter three integers x, y, Z, please put these three numbers from small to large output.

"Program 8"
Title: Output 9*9 formula.

1 for I in Range (1,10): 2 for     J in Range (1,i+1): 3         print I, ' * ', j, ' = ', i*j,4     print '

"Program 11"
Title: Classical Question: There is a pair of rabbits, from the 3rd month after birth, every month, a pair of rabbits, rabbit long to the third month
After each month and a pair of rabbits, if the rabbits are not dead, ask the total number of rabbits each month?

1 A = b = All for I in range (1,21,2): 4     print '%d%d '% (a, b), 5     A + = B6     B + = a

"Program 12"
Title: Determine the number of primes between 101-200 and the output of all primes.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3 from math import sqrt  4 def Main (): 5 for     I in range (101,201): 6
   
    flag = 1 7         k = Int (sqrt (i)) 8 for         J in Range (2,k+1): 9             if i%j = = 0:10                 flag = 011                 break12         if flag = = 1:13             print '%5d '% (i),     if __name__ = = "__main__": +     Main ()
   

"Program 13"
Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3 def Main (): 4 for     I in range (100,1000): 5         a = i%10 6         b = i/100 7         c = (int (I/10))%10 8         if i = = A**3+b**3+c**3:9             print "%5d"% (i), ten if __name__ = = "__main__":     Main ()

"Program 14"
Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3  4 def Main (): 5     n = Int (raw_input (' Enter a number: ')) 6     Print N, ' = ', 7 while     (n!=1): 8 for         I in range (2,n+1): 9             if (n%i) ==0:10                 n/=i11                 if (n = = 1):                     print '%d '% (i)                 else:14                     print '%d * '% (i),                 break16         if __name__ = = "__main__": 19     Main ()

"Program 15"
Title: Use the nesting of conditional operators to complete this problem: the students of the study score >=90 with a, 60-89 points between the use of B, 60 points below the C expression.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3 def Main (): 4     s = Int (raw_input (' Enter a number: ')) 5     if S>=90:6         grade = ' A ' 7     elif s>=60:8         grade = ' B ' 9     else:10         grade = ' C '     print grade,12 if __name__ = = ' __main__ ': +     Main ()

"Program 17"
Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3 #there is no + + operator in Python 4 import string 5 def main (): 6     s = r Aw_input (' Input a string: ') 7 Letter     = 0 8     space = 0 9     digit = 010 and other     = 011 for     C in S:12         if C. Isalpha ():             letter+=114         elif c.isspace (): space+=116 elif             c.isdigit         ():             digit+=118         else:19             other+=120     print ' There is%d letters,%d spaces,%d digits and%d other characters in your string. '% (letter,space,digit,other) if __name__ = = ' __main__ ':     Main ()

"Program 18"

Title: The value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (there are 5 numbers added at this time), several numbers are added with keyboard control.

1 #!/usr/bin/python 2 #-*-coding:utf-8-*-3 def Main (): 4     basis = Int (raw_input ("Input the basis number:")) 5     N = Int (raw_input ("input the longest length of number:")) 6     B = Basis 7     sum = 0 8 for     i in range (0,n): 9         if I==n-1:10             print "%d"% (basis), one-         else:12             print "%d +"% (basis),         sum+=basis14         basis = basis*10+ B15     print ' =%d '% (sum),     if __name__ = = ' __main__ ':     Main ()

"Program 19"
Title: If a number is exactly equal to the sum of its factors, this number is called the "end number". For example 6=1+2+3. programming
Find out all the numbers within 1000.

1 from math import sqrt 2 n = int (raw_input (' Input a number: ')) 3 sum = n*-1 4 k = int (sqrt (n)) 5 for I in Range (1,k+1): 6     if n%i = = 0:7         sum + = n/i 8         sum + = i 9 if sum = = N:10     print ' YES ' else:12     print ' NO '

1 "2" program 20 "3 title: A ball from 100 meters height of freedom falling, each landing back to the original height of half, and then fall, to ask it in the 4 10th time, the total number of meters? How high is the 10th time rebound? 5 "' 6 s = 100. 7 h = 50.0 8 for I in Range (2,11): 9     S + = 2*h10     h/= 211 print "The sum length of path:%f"%S12 print "the Last Hei Ght is:%f "%h
1 "2  " program 21 "3 title: Monkey Eat Peach problem: The first day of monkeys to take off a number of peaches, immediately ate half, but also not addicted to eat a 4 the next morning and the rest of the peach eaten half, and eat one more. Every morning after eating the day before the 5 of the remaining half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number. 6 "7 N = 1 8 for I in Range (9,0,-1): 9     n = (n+1) <<110 print n
1 "2★3" program 22 "4 title: Two table tennis team to play, each out three people. Team A for A,b,c three, team B for X, Y, z three people. Have drawn lots to determine the 5 contest list. Someone asked the team for a list of matches. A says he does not compare with X, C says he does not compare with x,z, please compile the procedure to find out the list of 63 teams.  7 "' 8 for I in Range (Ord (' X '), ord (' z ') + 1): 9     for J in Range (Ord (' X '), ord (' z ') + 1): Ten         if I! = j:11             fo R k in range (Ord (' X '), ord (' z ') + 1):                 if (i! = k) and (J! = k):                     if (i! = Ord (' x ')) and (k! = Ord (' x ')) and (K!) = Ord (' z ')):                         print ' order is a--%s\t B--%s\tc--%s '% (Chr (i), Chr (j), Chr (k))
1 "2" program 23 "  3 title: Print out Case (Diamond) 4  5    * 6 * * * 7  * * * 8 ******* 9  *****10   ***11    *12" ' For I in Range (1,8,2):     print ' * (i+1/2) + ' * ' *i15 for I in Range (5,0,-2):     print "* (i+1)/2) + ' * ' *i
1 "2" program 24 "  3 title: There is a fractional sequence: 2/1,3/2,5/3,8/5,13/8,21/13 ... to find out the sum of the first 20 items of this series. 4 "' 5 U = 2.0 6 d = 1.0 7 s = 0.0 8 for I in Range (0,20): 9     s = s+u/d10     u = u+d11     d = u-d12 print '%f '%s
1 "2" program 25 "  3 title: 1+2!+3!+...+20! and 4 ' ' 5 s = 0 6 t = 1 7 for I in Range (1,21): 8     t*=i 9     s+=t10 print S
1 "2" program 26 "  3 title: Use recursion method to find 5!. 4 "5 def Fun (i): 6     if I==1:7         return 1 8     return I*fun (i-1) 9 Print fun (5)
1 "2" program 27 "  3 title: Use the Recursive function call method, the input of the 5 characters, in reverse order to print out. 4 "5 def output (s,l): 6     if L==0:7         return 8     print S[L-1] 9     output (S,L-1) Ten s = raw_input (' Input a St Ring: ') + L = Len (s) output (s,l)
1 "2" program 28 "  3 title: 5 people sit together and ask the five how old is the person? He said he was 2 years older than the 4th one. Asked about the age of 4th man, he said he was 2 years older than the 4th 3. Asked the third person, and said that the 2nd People's Congress two years old. Ask the 2nd person, who says two years older than the first one. The last  5 asked the first man, who said he was 10 years old. How old is the fifth one? 6 "7 def Fun (i): 8     if I==1:9         return 1010 return fun     (i-1) +211 print Fun (5)
1 "2" program 29 "  3 title: To a positive integer not more than 5, requirements: First, it is a number of digits, second, the reverse print out the figures. 4 "5 def Fun (i,cnt): 6     if i==0:7         print ' There is%d digit in the number. ' %CNT 8         return 9     print i%10,10     i/=1011     cnt+=112 fun     (i,cnt) i = Int (raw_input (' Input a number: ') ) (i,0)
1 "2" program 30 "3 title: a 5-digit number, judging whether it is a palindrome number.    That is, 12321 is a palindrome number, single-digit and million-bit the same, 10 bits and thousands of the same. 4 "' 5 ans=[' Yes ', ' No '] 6 i = Int (raw_input (' Input a number (10000~99999): ')) 7 if i<10000 or I>99999:8     print ' Input error! ' 9 else:10     i = str (i)     flag = 012 for     j in Range (0,2):         if i[j]!=i[4-j]:14             flag =             Brea K16     Print Ans[flag]
1 "2" program 36 "" Screening Method "3 title: 100" 4 "for the prime number of 5 A = [0]*101 6 for I in Range (2,11): 7 for     J in Range (I+i,101,i): 8         A [J]=-1; 9 for I in Range (2,101):     if a[i]!=-1:11         print ", I,
1 "2" program 37 "3 title: 10 numbers sorted 4 ' ' 5 print ' input numbers please: ' 6 L = [] 7 for I in range: 8     l.append (int (r Aw_input (' Input a number: '))) 9 #可以直接使用sort函数: L.sort () #也可以自己写排序代码 (select sort) one for me in range (9): A     -J in range (i+1,1 0):         if l[j]<l[i]:14             temp = l[j]15             l[j] = l[i]16             L[i] = temp +    print L
1 "2" program 38 "3 title: Ask for a 3*3 matrix diagonal element 4" '  5 L = [] 6 for I in Range (3): 7 for     J in Range (3): 8         l.append (int (RA W_input (' Input a number: ')) 9 s = 010 for I in range (3): one     s + = l[3*i+i]12 print S

1 "2" program 39 "3 title: There is an array that is already sorted. Now enter a number that requires it to be inserted into the array as it was originally. 4 "5 L = [0,10,20,30,40,50] 6  7 print ' The sorted list is: ', l 8 cnt = len (l) 9 n = Int (raw_input (' Input a number: ') ) L.append (n) one for I in range (CNT):     -If n<l[i]:13 for         J in Range (cnt,i,-1):             l[j] = l[j-1]15         l[i ] = N16         break17 print ' The new sorted list is: ', l

1 "2" program 40 "3 title: Output an array in reverse order. 4 "5 A = [1,2,3,4,5,6,7,8,9] 6 L = Len (a) 7 print a 8 for I in Range (L/2): 9     a[i],a[l-i-1] = a[l-i-1],a[i] #注意此句10 Print a
1 "2" program 41 "3 topic: Learning static use of static Variables 4 topic: Learning how to define variables using Auto 5 topic: Learning how to use external 6 title: Learn how to use register to define variables 7 title: Macro # # Command Practice 8 "' 9 #Python没有这些功能
1 "2 demonstrates the use of lambda. 3 "4 MAXIMUM = lambda x, y:  (x > Y) * x + (x < y) * y 5 MINIMUM = lambda x:  (x > Y) * y + (x < y ) * x 6  7 If __name__ = = ' __main__ ': 8     a = 9     b =     print ' The Largar one is%d '% MAXIMUM (A, b) 11
   print ' The lower one is%d '% MINIMUM (A, B)
1 "2" program 51 "3 topic: Learn to use bitwise AND &. 4 Program Analysis:0&0=0; 0&1=0; 1&0=0; 1&1=1 5  6 topic: Learn to use bitwise OR |. 7 program analysis: 0|0=0; 0|1=1; 1|0=1;   1|1=1 8  9 topic: Learn to use bitwise XOR or ^. 10 Program analysis: 0^0=0; 0^1=1; 1^0=1;   1^1=011 12 topic: Learn to use the bitwise inverse ~. 13 Program Analysis: ~0=1; ~1=014 "a = 077#8" b = a&318 print ' 077&3=%d '%b19 b&=720 print ' 077&3&7=%d '%b21 a = 0772 3 B = A | 324 print ' 077|3=%d '% b25 b |= 726 print ' 077|3|7=%d '% b27-a = 07729 B = a ^ print ' 077^3=%d '% B31 B ^= 732 Prin T ' 077^3^7=%d '% b
1 "2" program 54 "3 title: Take an integer A from the right end of the 4~7 bit. 4 "5 A = 1006 Print 100&0x00f0
1 "2" program 56 "3 title: Draw Round" Tkinter module "4" "5 if __name__ = = ' __main__ ': 6 from     Tkinter Import * 7  8     canvas = Canv As (width=800, height=600, bg= ' Red ')   9     canvas.pack (Expand=yes, Fill=both)                ten     k = 111     j =     For I in Range (0,26):         canvas.create_oval (310-k,250-k,310 + k,250 + K, width=1) K +         = J15         J + = 0.316 1 7     Mainloop ()
1 "2" program 57 "3 title: Draw Straight line. 4 1. Program Analysis: 5 2. Program Source code:  6 "' 7 if __name__ = = ' __main__ ': 8 from     Tkinter Import * 9"     canvas = Canva S (width=300, height=300, bg= ' green ')   one     canvas.pack (Expand=yes, Fill=both)     x0 = 26313     y0 = 26314     y1 = 27515     x1 = 27516     for i in range: Canvas.create_line         (X0,y0,x0,y1, width=1, fill= ' red '  )         x0 = x0-519         y0 = y0-520         x1 = x1 + 521         y1 = y1 + 522 x0     = 26324     y1 = 27525     y0 = 26326 for     i in range:         canvas.create_line (X0,y0,x0,y1,fill = ' red ')         x0 + = 529         y0 + = 530
   y1 + = 531 +     Mainloop ()
1 "2" program 58 "3 title: Draw Square.  4 "5 if __name__ = = ' __main__ ': 6 from     Tkinter Import * 7     root = Tk () 8     root.title (' canvas ') 9     Canvas =  Canvas (root,width = 400,height = 400,BG = ' yellow '),     x0 = 26311     y0 = 26312     y1 = 27513     x1 = 27514     for I in range:         canvas.create_rectangle (x0,y0,x1,y1)         x0-= 517         y0-= 518         x1 + 519         y1 + = 520     canvas.pack ()     Root.mainloop ()
 1 "2 title: Drawing, comprehensive example. 3 1. Program Analysis: 4 2. Program Source code: 5 "' 6 if __name__ = = ' __main__ ': 7 from Tkinter import * 8 canvas = canvas (width = 300,hei ght = 300,BG = ' green ') 9 canvas.pack (expand = Yes,fill = BOTH), x0 = 15011 y0 = 10012 Canvas.create_oval (x0-10,y0-10,x0 + 10,y0 + Ten) Canvas.create_oval (x0-20,y0-20,x0 + 20,y0 +) Canvas.create_oval (x0- 50,y0-50,x0 + 50,y0 +) 2 import math16 B = 0.80917 for I in range: a = * MATH.PI/16 * I x = Math.ceil (x0 + * Math.Cos (a)) y = Math.ceil (y0 + * Math.sin (a) * B) canvas.create _line (X0,y0,x,y,fill = ' red ') canvas.create_oval (x0-60,y0-60,x0 + 60,y0 +), for K in range (501 ): + For I in range: + A = (2 * math.pi/16) * i + (2 * math.pi/180) * K28 x = Math . ceil (x0 + x Math.Cos (a)) y = Math.ceil (y0 + x + math.sin (a) * B) and Canvas.create_line (x0,y0 XY,fill = ' red ') to J in range: + A = (2 * math.pi/16) * i + (math.pi/180) * k-133 x = Math.ceil (x0 + math.cos (a)) y = Math.ceil (y0 + x * Math.sin (a) * B) CANVAS.C Reate_line (X0,y0,x,y,fill = ' red ') Mainloop ()

Getting started with Python 100 examples

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.