Home Backend Development Python Tutorial Share a python exercise example

Share a python exercise example

Jul 23, 2017 am 11:23 AM
python Practice 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、暂停一秒输出,并格式化当前时间。
Copy after login
1、有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
方法一:
Copy after login
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))
Copy after login
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
Copy after login
方法二:
Copy after login
Copy after login
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))
Copy after login
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
Copy after login
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,求应发放奖金总数?
方法一:
Copy after login
 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 < 0:25     print(&#39;x必须大于0&#39;)
Copy after login
1 销售利润是:2000012 提成是: 17500.05
Copy after login
方法二:
Copy after login
Copy after login
 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 < 5: 6     x = int(input(&#39;销售利润是:&#39;)) 7     sum = 0 8     for n in range(0,6): 9         if x > a[n]:10             tmp = (x-a[n])*b[n] # 计算该级别的提成11             sum += tmp12             x = a[n]  # 后续的每一级都计算满额提成13     print(sum)14     count += 1
Copy after login

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 < 10000:if math.pow(int(math.sqrt(x+100)),2) == x+100:if math.pow(int(math.sqrt(x + 100 + 168)), 2) == x + 100 + 168:  # 简单列等式,谁不会。。。mdzz
Copy after login
# print(x) x += 1
Copy after login

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

&#39;&#39;&#39;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 < i < 168 / 2 + 1。
7、接下来将 i 的所有数字循环计算即可。&#39;&#39;&#39;i = 1#求出最大范围while ((i+1)*(i+1)-i*i) <= 168:
      i += 1#循环测试并打印for j in range(1,i):for k in range(1,i):if (k*k - j*j) == 168:print(k*k-268)
Copy after login

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(&#39;年:&#39;))
m = int(input(&#39;月:&#39;))
d = int(input(&#39;日:&#39;))

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


年:2003月:12日:2
336
Copy after login

Method two:

import time# D=input("请输入年份,格式如XXXX-XX-XX:")D=&#39;2017-4-3&#39;d=time.strptime( D,&#39;%Y-%m-%d&#39;).tm_yday# print("the {} day of this year!" .format(d))print(d)print(time.strptime( D,&#39;%Y-%m-%d&#39;))# 或使用datetime模块import datetimeprint(datetime.date(2017,4,3).timetuple().tm_yday)print(datetime.date(2017,4,3).timetuple())
Copy after login
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)
Copy after login

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(&#39;\n&#39;.join(l))
Copy after login

Method two:

 1 # 最弱智的做法 2 # a = input(&#39;a=&#39;) 3 # b = input(&#39;b=&#39;) 4 # c = input(&#39;c=&#39;) 5 a = 10 6 b = 20 7 c = 30 8  9 l = []10 11 l.append(a)12 if b < a:13     l.insert(0,b)14     if c < b:15         l.insert(0,c)16     elif c > a:17         l.append(c)18     else:19         l.insert(1,c)20 else:21     l.append(b)22     if c < a:23         l.insert(0,c)24     elif c > b:25         l.append(c)26     else:27         l.insert(1,c)28 29 print('\n'.join(l))
Copy after login

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)
Copy after login
[1, 2, 3, 4, 5, 5, 7]
Copy after login
6、斐波那契数列。
方法一:
Copy after login
l = [0,1]def f(n):for x in range(n):
        l.append(l[x]+l[x+1])

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

Method 2:

# 使用递归def fib(n):if n == 1 or n == 2:return 1return fib(n - 1) + fib(n - 2)# 输出了第10个斐波那契数列print(fib(20))
Copy after login
7、将一个列表的数据复制到另一个列表中。
Copy after login
a = list(range(20))
b = a[:]print(a)print(b)print(id(a))print(id(b))
Copy after login
[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
Copy after login
8、输出 9*9 乘法口诀表。
Copy after login
 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))
Copy after login
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
Copy after login
9、暂停一秒输出。
Copy after login
import timefor n in range(1,10):print(n)
    time.sleep(1)
Copy after login
10、暂停一秒输出,并格式化当前时间。
Copy after login
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)
Copy after login

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 of this Website
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

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles