search
HomeBackend DevelopmentPython TutorialShare a python exercise example

Share a python exercise example

Jul 23, 2017 am 11:23 AM
pythonPractice questions

1、有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
2、企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
3、一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
4、输入某年某月某日,判断这一天是这一年的第几天?
5、输入三个整数x,y,z,请把这三个数由小到大输出。
6、斐波那契数列。
7、将一个列表的数据复制到另一个列表中。
8、输出 9*9 乘法口诀表。
9、暂停一秒输出。
10、暂停一秒输出,并格式化当前时间。
1、有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
方法一:
1 l = []2 for i in range(1,5):3     for j in range(1,5):4         for m in range(1,5):5             if len({i,j,m}) == 3:6                 l.append(i * 100 + j * 10 + m)7 print(l)8 print(len(l))
1 [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]2 24
方法二:
1 from itertools import permutations2 3 l = []4 for i in permutations([1, 2, 3, 4], 3):5     l.append(i)6 7 print(l)8 print(len(l))
1 [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]2 24
2、企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
方法一:
 1 # 这是最弱的方法,然而我想到的就是这种。。 2 a = [1000000,600000,400000,200000,100000,0] 3 b = [0.01,0.015,0.03,0.05,0.075,0.1] 4 x = int(input('销售利润是:')) 5  6 if x >= a[0]: 7     bonus = (x - a[0]) * b[0] + 400000 * b[1] + 200000 * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5] 8     print('提成是:',bonus) 9 elif a[0] > x >= a[1]:10     bonus = (x - a[1]) * b[1] + 200000 * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5]11     print('提成是:', bonus)12 elif a[1] > x >= a[2]:13     bonus = (x - a[2]) * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5]14     print('提成是:', bonus)15 elif a[2] > x >= a[3]:16     bonus = (x - a[3]) * b[3] + 100000 * b[4] + 100000 * b[5]17     print('提成是:', bonus)18 elif a[3] > x >= a[4]:19     bonus = (x - a[4]) * b[4] + 100000 * b[5]20     print('提成是:', bonus)21 elif a[4] > x >= a[5]:22     bonus = (x - a[5]) * b[5]23     print('提成是:', bonus)24 elif x 
1 销售利润是:2000012 提成是: 17500.05
方法二:
 1 # 参考别人的 2 a = [1000000, 600000, 400000, 200000, 100000, 0] 3 b = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1] 4 count = 0 5 while count  a[n]:10             tmp = (x-a[n])*b[n] # 计算该级别的提成11             sum += tmp12             x = a[n]  # 后续的每一级都计算满额提成13     print(sum)14     count += 1

3. An integer. After adding 100, it becomes a perfect square number. When added to 168, it becomes a perfect square number. What is the number?

Method 1:

import math

x = -99  # 这个值是后面看别人答案时想到的,x+100必须大于0while x <pre class="brush:php;toolbar:false"># print(x) x += 1

Method 2: After reading other people’s thoughts, I feel like I am mentally retarded.

'''1、则:x + 100 = n2, x + 100 + 168 = m2
2、计算等式:m2 - n2 = (m + n)(m - n) = 168
3、设置: m + n = i,m - n = j,i * j =168,i 和 j 至少一个是偶数
4、可得: m = (i + j) / 2, n = (i - j) / 2,i 和 j 要么都是偶数,要么都是奇数。
5、从 3 和 4 推导可知道,i 与 j 均是大于等于 2 的偶数。
6、由于 i * j = 168, j>=2,则 1 

4. Enter a certain day of a certain year, a certain month, and determine what day of the year this day is?

Method one:

import datetime

Y = int(input('年:'))
m = int(input('月:'))
d = int(input('日:'))

days = datetime.datetime(Y,m,d) - datetime.datetime(Y,1,1) + datetime.timedelta(1)  # 减去当年1月1日n = int(str(days).split(' ')[0])print(n)


年:2003月:12日:2
336

Method two:

import time# D=input("请输入年份,格式如XXXX-XX-XX:")D='2017-4-3'd=time.strptime( D,'%Y-%m-%d').tm_yday# print("the {} day of this year!" .format(d))print(d)print(time.strptime( D,'%Y-%m-%d'))# 或使用datetime模块import datetimeprint(datetime.date(2017,4,3).timetuple().tm_yday)print(datetime.date(2017,4,3).timetuple())
93time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)93time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)

5. Input three integers x, y, z. Please output these three numbers from small to large.

Method one

L = [a,b,c]
L.sort()print('\n'.join(l))

Method two:

 1 # 最弱智的做法 2 # a = input('a=') 3 # b = input('b=') 4 # c = input('c=') 5 a = 10 6 b = 20 7 c = 30 8  9 l = []10 11 l.append(a)12 if b  a:17         l.append(c)18     else:19         l.insert(1,c)20 else:21     l.append(b)22     if c  b:25         l.append(c)26     else:27         l.insert(1,c)28 29 print('\n'.join(l))

Method three:

# 冒泡法a=[1,3,5,2,4,5,7]

n=len(a)for i in range(0,n):  for j in range(i,n) :     if (a[i] >= a[j] ):
        a[i],a[j] = a[j],a[i]print(a)
[1, 2, 3, 4, 5, 5, 7]
6、斐波那契数列。
方法一:
l = [0,1]def f(n):for x in range(n):
        l.append(l[x]+l[x+1])

f(20)print(l)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]

Method 2:

# 使用递归def fib(n):if n == 1 or n == 2:return 1return fib(n - 1) + fib(n - 2)# 输出了第10个斐波那契数列print(fib(20))
7、将一个列表的数据复制到另一个列表中。
a = list(range(20))
b = a[:]print(a)print(b)print(id(a))print(id(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]2030007944584
2030007104200
8、输出 9*9 乘法口诀表。
 1 # 先百度一下乘法口诀表长什么样子,怎么排列的 2 ''' 3 乘法口诀表如下: 4 1*1=1 5 2*1=2 2*2=4 6 3*1=3 3*2=6 3*3=9 7 4*1=4 4*2=8 4*3=12 4*4=16 8 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 9 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=3610 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=4911 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=6412 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=8113 '''14 formula = '{a} * {b} = {c}'15 16 s = ''17 for i in range(1,10):18     print(s)19     s = ''20     for j in range(1,i+1):21         s += formula.format(a=i,b=j,c=i*j) + ' '22         # print(formula.format(a=i,b=j,c=i*j))
1 * 1 = 1 
2 * 1 = 2 2 * 2 = 4 
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9、暂停一秒输出。
import timefor n in range(1,10):print(n)
    time.sleep(1)
10、暂停一秒输出,并格式化当前时间。
import timefor i in range(5):
    struct_time = time.localtime(time.time())
    t = time.strftime('%Y-%m-%d %H:%M:%S',struct_time)
    t = time.strftime('%Y-%m-%d',struct_time)print(t)
    time.sleep(1)

The above is the detailed content of Share a python exercise example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),