当前位置:网站首页>What do you know about the 15 entry-level applets

What do you know about the 15 entry-level applets

2022-06-23 21:49:00 Chen Chen 135

Many students have finished Python It is still difficult to use it flexibly . I sort out 15 individual Python Entry applet . Application in practice Python There will be a half power effect .

01 Realize binary quadratic function

Realize the binary quadratic function in mathematics :f(x, y) = 2x^2 + 3y^2 + 4xy, Exponential operator is required **

"""
 Bivariate quadratic function 
"""

x = int(input(' Input x:'))
y = int(input(' Input y:'))

z = 2 * x ** 2 + 3 * y ** 2 + 4 * x * y
print('f(%d, %d) = %d' % (x, y, z))

02 Separates the single digits of an integer

The single digit of a positive integer , And partial separation except single digits . Need to use die ( Take the remainder ) Operator %, And the division operator //

"""
 Separate integer and single digits 
"""

x = int(input(' Input integer :'))

single_dig = x % 10
exp_single_dig = x // 10

print(' Single digit : %d' % single_dig)
print(' Except for single digits : %d' % exp_single_dig)

03 Implement an accumulator

Implement a simple accumulator , User input is acceptable 3 A digital , And add it up . You need to use the compound assignment operator :+=

"""
 accumulator  v1.0
"""

s = 0

x = int(input(' Input integer :'))
s += x

x = int(input(' Input integer :'))
s += x

x = int(input(' Input integer :'))
s += x

print(' The sum of the :%d' % s)

04 Judgement of leap year

Year of importation , Determine if it's a leap year . Leap year judgment method : Can be 4 to be divisible by , But can't be 100 to be divisible by ; Or can be 400 to be divisible by . You need to use arithmetic operators and logical operators

"""
 Judgement of leap year 
"""

year = int(input(' Year of importation : '))
is_leap = year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(is_leap)

05 Judge odd even number

Enter a number , Determine whether the cardinality is even , Requires modular operations and if ... else structure

"""
 Judge odd even number 
"""

in_x = int(input(' Input integer :'))

if in_x % 2 == 0:
    print(' even numbers ')
else:
    print(' Odd number ')

06 Degrees Celsius and Fahrenheit rotate with each other

I've done Fahrenheit to Celsius before , Now through the branch structure, the two can turn to each other .

"""
 Degrees Celsius and Fahrenheit are interchangeable 
"""

trans_type = input(' Enter Celsius or Fahrenheit :')

if trans_type == ' Centigrade ':  #  Execute the logic of Fahrenheit to Celsius 
    f = float(input(' Enter Fahrenheit temperature :'))
    c = (f - 32) / 1.8
    print(' The temperature in centigrade is :%.2f' % c)
elif trans_type == ' Fahrenheit ':  #  Execute the logic of Celsius to Fahrenheit 
    c = float(input(' Enter the temperature in centigrade :'))
    f = c * 1.8 + 32
    print(' The temperature in Fahrenheit is :%.2f' % f)
else:
    print(' Please enter   Fahrenheit   or   Centigrade ')

07 Whether it forms a triangle

Enter the length of the three edges , Judge whether a triangle is formed . The conditions for forming a triangle : The sum of the two sides is greater than that of the third side .

"""
 Whether it forms a triangle 
"""
a = float(input(' Enter the three sides of the triangle :\n a = '))
b = float(input(' b = '))
c = float(input(' c = '))
if a + b > c and a + c > b and b + c > a:
    print(' Can form a triangle ')
else:
    print(' Can't make a triangle ')

08 Output grade

Enter score , Output the grade corresponding to the score .

>=90 get a share A,[80, 90) have to B,[70, 80) have to C,[60, 70) have to D,< 60 have to E

"""
 Output grade 
"""

score = float(input(' Please enter the grade : '))
if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
elif score >= 60:
    grade = 'D'
else:
    grade = 'E'
print(' The grade is :', grade)

09 Calculate the Commission

The bonus of an enterprise is calculated according to the following rules according to the sales profit . Enter sales profit , Calculate the bonus .

profits <= 10 ten thousand , Bonus can be increased 10% 10 ten thousand < profits <= 20 ten thousand , Higher than 10 The part of ten thousand is increased 7.5% 20 ten thousand < profits <= 40 ten thousand , Higher than 20 The part of 10000 yuan will be increased 5% 40 ten thousand < profits <= 60 ten thousand , Higher than 40 The part of 10000 yuan will be increased 3% profits > 60 ten thousand , exceed 60 The part of ten thousand is increased 1%

"""
 Calculate the Commission  v1.0
"""

profit = float(input(' Enter sales profit ( element ): '))

if profit <= 100000:
    bonus = profit * 0.1
elif profit <= 200000:
    bonus = 100000 * 0.1 + (profit - 100000) * 0.075
elif profit <= 400000:
    bonus = 100000 * 0.1 + 200000 * 0.075 + (profit - 200000) * 0.05
elif profit <= 600000:
    bonus = 100000 * 0.1 + 200000 * 0.075 + 400000 * 0.05 + (profit - 400000) * 0.03
else:
    bonus = 100000 * 0.1 + 200000 * 0.075 + 400000 * 0.05 + 600000 * 0.03 + (profit - 600000) * 0.01

print(' Bonus :%.2f' % bonus)

10 Guess the game

The program randomly generates a positive integer , Users guess , The program gives the corresponding prompt according to the guessed size . Last , The output user guessed how many times to guess .

"""
 Guess the game 
"""

import random

answer = random.randint(1, 100)
counter = 0
while True:
    counter += 1
    number = int(input(' Guess a number (1-100): '))
    if number < answer:
        print(' A little bigger. ')
    elif number > answer:
        print(' A little smaller ')
    else:
        print(' Guessed it ')
        break

print(f' Guess together {counter} Time ')

11 Print multiplication table

"""
 Print multiplication table 
"""

for i in range(1, 10):
    for j in range(1, i + 1):
        print(f'{i}*{j}={i * j}', end='\t')

12 Is it a prime

Enter a positive integer , Judge whether it is a prime number . Definition of prime number : Greater than 1 Of the natural number , Can only be 1 And the natural number divisible by itself . Such as :3、5、7

"""
 Judge whether it is a prime number 
"""

num = int(input(' Please enter a positive integer : '))
end = int(num // 2) + 1  #  Just judge whether the first half can be divided , The first half has nothing divisible, so , The second half must not 

is_prime = True
for x in range(2, end):
    if num % x == 0:
        is_prime = False
        break
if is_prime and num != 1:
    print(' prime number ')
else:
    print(' Not primes ')

range(2, end) Can generate 2, 3, ... end Sequence , And assign values to x Execute loop .range There are also the following uses range(10): Generate 0, 1, 2, ... 9 Sequence range(1, 10, 2): Generate 1, 3, 5, ... 9 Sequence

13 Guessing game

Use the program to realize the stone scissors paper game .

"""
 Guessing game 
"""

# 0 For cloth ,1 Represents scissors ,2 It stands for stone 
import random

rule = {' cloth ': 0, ' scissors ': 1, ' stone ': 2}

rand_res = random.randint(0, 2)
input_s = input(' Enter the stone 、 scissors 、 cloth :')
input_res = rule[input_s]
win = True

if abs(rand_res - input_res) == 2:  #  Difference between 2 It means that cloth meets stone , The one who gives the cloth wins 
    if rand_res == 0:
        win = False
elif rand_res > input_res:  #  Difference between 1 Who is the bigger and who wins 
    win = False

print(f' Program out :{list(rule.keys())[rand_res]}, Input :{input_res}')

if rand_res == input_res:
    print(' flat ')
else:
    print(' win ' if win else ' transport ')

14 Dictionary sort

Dictionary key It's the name ,value It's height , Now you need to reorder the dictionary by height .

"""
 Dictionary sort 
"""

hs = {' Zhang San ': 178, ' Li Si ': 185, ' Wang Ma Zi ': 175}

print(dict(sorted(hs.items(), key=lambda item: item[1])))

15 Bivariate quadratic function v2.0

Encapsulate bivariate quadratic functions in functions , Convenient to call

"""
 Bivariate quadratic function  v2.0
"""

def f(x, y):
    return 2 * x ** 2 + 3 * y ** 2 + 4 * x * y


print(f'f(1, 2) = {f(1, 2)}')

Last

Beginners python My little friend, pay attention to ~ Don't just rely on tutorials , Practical ability is ignored . Otherwise , It's hard to make progress . You can do more , Try it , Accumulate experience .

I hope it helps you , Friends who like this article A little praise and attention

原网站

版权声明
本文为[Chen Chen 135]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112211148022424.html