basic programming python: python simple example code 10

Note: I use python2.7, if you use more than Python3.0 version, please remember function print () Oh! If the issue because the version comments, do not do back Oh! ! !

1. Title: 1,2,3,4 figures, the number of other with no repeat of the three-digit numbers can be composed? How much are?

Program analysis: can be filled in one hundred, ten digital bits are 1,2,3,4. Composition before removing all permutations arrangement conditions are not satisfied.
Source Code:

 # -*- coding: UTF-8 -*-
for 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,j,k

2. Topic: Enterprise commission bonuses based on profits. When profit (I) is less than or equal to 10 million, 10% of the prize can be mentioned; when profit than 10 million, less than 20 million, 10 million commission portion is less than 10%, higher than 100,000 yuan part, 7.5% cocoa commission; is between 200,000 to 400,000, more than 20 million portion may commission 5%; more than 40 million portion is between 400,000 to 600,000, may be 3% commission ; is between 600,000 to 1,000,000, more than 60 million portion may commission 1.5% above 100 million, more than one million yuan portion 1% commission, from the keyboard month profit I, should seek The total number of paid bonuses?

Program Analysis: Please use axes to cut-off, location. The bonus to be defined in Note grow integer defined.

Method One: This method is a mathematical principle I used to do, is to list every case an expression, finally simplified expression, then I calculate the value of the expression to profit directly from the input, that is, the total number of bonus . Is to stack up bonus, specific algorithms or to calculate on paper, this method is suitable for beginners coding of people, like me, the most stupid ideas in mathematical problem-solving, that mathematically you do is in accordance with what methods, he then converted into the code, it is easier to understand.

# -*- coding: UTF-8 -*-
while True:
 I = input("pls input the lirun:")
 if I <= 10:
  a = I * 0.01
  print a
 elif I <= 20 and I > 10:
  b =0.25 + I * 0.075
  print b
 elif I <= 40 and I > 20:
  c = 0.75 + I * 0.05
  print c
 elif I <= 60 and I > 40:
  d = 0.95 + I * 0.03
  print d
 elif I <= 60 and I > 100:
  e = 2 + I * 0.015
  print e
 else:
  f = 2.95 + I * 0.01
  print f

Method Two: This method is more difficult to understand, and suitable for the foundation of good people

# -*- coding: UTF-8 -*-
I = int(raw_input('净利润:'))
#这应该就是各个分界值了,把它们放在列表里方便访问
arr = [1000000,600000,400000,200000,100000,0] 
#这是各个分界值所对应的奖金比例值
rat = [0.01,0.015,0.03,0.05,0.075,0.1] 
 #这是总奖金的初始值
r = 0 
 #有6个分界值当然要循环6次     
for idx in range(0,6):  
 if I > arr[idx]:
  r = r + (I - arr[idx]) * rat[idx] 
  print (I - arr[idx]) * rat[idx]
  I = arr[idx]
print r

3. Title: An integer plus 100 and 268 are coupled after a perfect square, what is the number is how much?

Program Analysis: Analyzing less than 10,000, then 100 plus the number of prescription, and then prescribing plus 268, if the result of the square root satisfies the following condition, i.e., is the result. Here to use the square root function sqrt mathematics.

# -*- coding: UTF-8 -*-
import math
for i in range(10000):
 x = int(math.sqrt(i + 100))
 y = int(math.sqrt(i + 268))
 if (x * x == i + 100) and (y * y == i + 268):
  print i

4. Title: Enter a certain period of a day, this day is judgment day of the year?

Program analysis: March 5, for example, should put the first two months together, then add the five days that is the day of this year's special circumstances, enter the month and leap year added an extra day to be considered greater than 3 .

Ideas: Date of first input divides, year, month, day three numbers, in turn prepared in accordance with the actual situation.

# -*- coding: UTF-8 -*-
x = raw_input("请输入日期,比如20160506:")
year = int(x[:4])
month = int(x[4:6])
day = int(x[6:8])
month_day = [31,28,31,30,31,30,31,31,30,31,30,31]
data = sum(month_day[:(month-1)],day)
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
 if month > 2:
  data = data + 1
print "it is the %dth day"% (data)

5. Title: Enter three integers x, y, z, please these three numbers small to large outputs.

A program analysis: We find a way to put the smallest number x, x and y are compared first, if x> y then the x and y values ​​are exchanged, and then use the x and z are compared, if x> z x will be exchanged with the value of z, and x can be minimized.

# -*- coding: UTF-8 -*-
l = []
for i in range(3):
 x = int(raw_input('integer:\n'))
 l.append(x) #这里用append()函数,意思是追加元素
 l.sort()
print l

Analysis of two procedures: a variable used to store the minimum value, comparing the number of three twenty-two exchange, which is the most common way of thinking.

# -*- coding: UTF-8 -*-
 
x = input("input a num:")
y = input("input a num:")
z = input("input a num:")
temp = 0
if x < y:
 temp = x
 x = y
 y = temp
if x < z:
 temp = x
 x = z
 z = temp
if y < z:
 temp = y
 y = z
 z = temp
print z,y,x #由于上面是由小到大来比较和交换的,所以这里需要逆序输出

Analysis of three procedures: a sorting function sort, sort function can be rearranged in accordance with the digital ascending order.

# -*- coding: UTF-8 -*-
x = input("input a num:")
y = input("input a num:")
z = input("input a num:")
y = [x,y,z] #注意,这里只能是列表,不能是元组,因为元组是不可改变的,你懂得!!
y.sort()
print y

6. Title: copying a data list to another list.

Note: Some students think of using append () function, here pay special attention to, append () function is a time append an element, if you use append () function, that list will need to be added as a data use, do not believe you the following:

method one:

# -*- coding: UTF-8 -*-
l1 = [1,2,3]
l2 = [4,5,6]
l1.append(l2)
print l1
 
输出:[1, 2, 3, [4, 5, 6]]

Method two: Use the "+" to connect the two lists, i.e. data is copied to the list l1 l2 list.

# -*- coding: UTF-8 -*-
l1 = [1,2,3]
l2 = [4,5,6]
print l1 + l2
 
输出:[1, 2, 3, 4, 5, 6]

Method three: Use the list [:]. Slice method, we all know that if you do not specify start and end, so it means that the output element of the list.

# -*- coding: UTF-8 -*-
a = [1, 2, 3] #把列表a复制到列表b
b = a[:]
print b

7. Title: 9 * 9 outputs the multiplication tables.

Analysis procedures: Branches and columns consideration, a total of 9 rows 9 columns, row control I, j the control column. If the format is not required, it is simple, can be output in any format, as follows:

# -*- coding: UTF-8 -*-
for i in range(1,10):
 for j in range(1,10):
  x = i * j
  print "%d * %d = %d" % (i,j,x)

If you need to consider the output format, we will be mentioned later in the article, hehe.

8. Title: second pause output.

Program Analysis: pause function of time is time.sleep (), here are free, for example, lists, tuples, dictionaries can be.

# -*- coding: UTF-8 -*-
import time
d = {"a":1,"b":2}
for i in d:
 print i
 time.sleep(1) #暂停一秒输出

9. Title: Fibonacci columns.

Program Analysis: Fibonacci number (Fibonacci sequence), also known as golden columns, referring to such a series: 0,1,1,2,3,5,8,13,21,34, .......
Mathematically, the fee Fibonacci series is based on a recursive method to define:

n = 1 时 f(1) = 1
n = 2 时 f(2) = 1
n = 3 时 f(3) = f(3-1) + f(3-2)
    = f(2) + f(1)
    = 1 + 1
    = 2
n = 4 时 f(4) = f(4-1) + f(4-2)
    = f(3) + f(2)
    = 2 + 1
    = 3
n = 5 时 f(5) = f(5-1) + f(5-2)
    = f(4) + f(3)
    = 3 + 2
    = 5
.....   ...
 
所以,当 n >= 2 时,表达式为f(n) = f(n-1) + f(n-2)
n = 1 时 f(1) = 1
n = 2 时 f(2) = 1
n = 3 时 f(3) = f(3-1) + f(3-2)
    = f(2) + f(1)
    = 1 + 1
    = 2
n = 4 时 f(4) = f(4-1) + f(4-2)
    = f(3) + f(2)
    = 2 + 1
    = 3
n = 5 时 f(5) = f(5-1) + f(5-2)
    = f(4) + f(3)
    = 3 + 2
    = 5
.....   ...
所以,当 n >= 2 时,表达式为f(n) = f(n-1) + f(n-2)

method one:

# -*- coding: UTF-8 -*-
 
def fib(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a
 
# 输出了第10个斐波那契数列
 print fib(10)

Method Two:

# -*- coding: UTF-8 -*-
 
# 使用递归
def fib(n):
 if n==1 or n==2:
  return 1
 return fib(n-1)+fib(n-2)
 
# 输出了第10个斐波那契数列
print fib(10)

Method three: If you need to specify the output Fibonacci number Fibonacci number sequence, you can use the following code:

# -*- coding: UTF-8 -*-
 
def fib(n):
 if n == 1:
  return [1]
 if n == 2:
  return [1, 1]
 fibs = [1, 1]
 for i in range(2, n):
  fibs.append(fibs[-1] + fibs[-2])
 return fibs
 
# 输出前 10 个斐波那契数列
print fib(10)

10. Title: Classical problem: a pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to the third month after the month gave birth to one pair of rabbits, if the rabbit is not dead the total number of rabbits per month to ask how much?

Program Analysis: After calculation and find the law to get the law of the rabbit several columns 1,1,2,3,5,8,13,21 ... typical Fibonacci number, which is why I want to Fibonacci. the reason the number of columns on the ninth title of the column, then the method according to the previous question three, we would be very easy to obtain the number of rabbits per month.

In addition, there is a more beautiful output:

# -*- coding: UTF-8 -*-
 
f1 = 1
f2 = 1
for i in range(1,21):
 print '%12ld %12ld' % (f1,f2),
 if (i % 3) == 0:
  print ''
 f1 = f1 + f2
 f2 = f1 + f2

Output
. 1. 5. 1 2. 8. 3
13 is 21 is 34 is 89 55 144
233 377 61098715972584
4181 6,765,109,461,771,128,657 46368
75025 121 393 196 418 832.04 thousand 317811514229
13462692178309 35245785702887 922,746,514,930,352
24,157,817 102,334,155 165,580,141 267,914,296 3,908,816,963,245,986

Finally, I recommend a good reputation python gathering [ click to enter ], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data method, every day, programmers explain the timing Python technology, to share some of the learning and the need to pay attention to small details

Published 15 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104908295