Examples of children's python exercises

  • 2020-09-16 07:35:20
  • OfStack

Example 1:

Question: How many three digits can be formed from four digits: 1, 2, 3, and 4? What are they?

Program analysis: the Numbers that can be filled in the hundreds, 10, and ones places are 1, 2, 3, and 4. Make up all the permutations and remove the permutations that do not satisfy the criteria (as long as the hundreds do not equal 10 and do not equal the ones place).

Instance (2.0 + Python)


#!/usr/bin/python
# -*- coding: UTF-8 -*-
for i in range(1,5):# A one hundred - bit 
  for j in range(1,5):#10 position 
    for k in range(1,5):# bits 
      if( i != k ) and (i != j) and (j != k):# A one hundred - bit 10 The Numbers in the ones place are not equal 
        print i,j,k #3.0+ print (i,j,k)

Example 2:

Subject: Bonuses are paid on a commission basis of profits. If the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; If the profit is higher than 100,000 yuan, if it is lower than 200,000 yuan, 10% commission will be given for the part lower than 100,000 yuan, and 7.5% for the part higher than 100,000 yuan. If the amount is between 200,000 yuan and 400,000 Yuan, 5% of the amount above 200,000 yuan can be taken as commission; For the part above 400,000 yuan between 400,000 yuan and 600,000 Yuan, the commission can be 3%; If it is between 600,000 yuan and 1,000,000 yuan, 1.5% of the amount above 600,000 yuan can be taken as commission. If it is over 1,000,000 yuan, 1% of the amount above 1,000,000 yuan can be taken as commission. Enter the monthly profit I from the keyboard and ask for the total amount of bonus.

Knowledge base: Array traversal

Program analysis: please use the array to divide (two arrays, 1 array profit space, 1 array is the percentage), location, judge the profit range. For example, if the profit is 120,000 and 120,000 is greater than 100,000 through array positioning, then the bonus consists of two parts:

1. (120,000-100,000) *0.75
2, 100000 * 0.1

Instance (2.0 + Python)


# !/usr/bin/python
# -*- coding: UTF-8 -*-
i = int(raw_input(' Net profit :'))
arr = [1000000, 600000, 400000, 200000, 100000, 0]
rat = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
r = 0
for idx in range(0, 6):
  if i > arr[idx]:
    r += (i - arr[idx]) * rat[idx] # A percentage above the base, such as profit 120000 , it is 20000 Part of the commission 
    print (i - arr[idx]) * rat[idx]
    i = arr[idx] 
print r


Related articles: