Many students still have difficulty using Python flexibly after learning it. I’ve compiled 15 small programs for getting started with Python. Using Python in practice can be much more effective.
01 Implement binary quadratic function
F (x, y) = 2x^2 + 3y^2 + 4xy
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))Copy the code
Many students still have difficulty using Python flexibly after learning it. I’ve compiled 15 small programs for getting started with Python. Using Python in practice can be much more effective.
The 02 separates the units digit of the whole number
Separates the units digit of a positive integer from all but the units digit. You need the modulo (remainder) operator %, and the divisor operator //
X = int(input(' input ') x = x % 10 exp_single_dig = x // 10 print(' input ') x = x % 10 exp_single_dig = x // 10 print(' input ') x = x % 10 %d' % exp_single_dig print(' %d' % exp_single_dig ')Copy the code
Implement an accumulator
Implement a simple accumulator that can accept user input of three numbers and add them up. We need the compound assignment operator: +=
""" s = 0 x = int(input(' input ')) s += x x = int(input(' input ')) s += x x = int(' input ') ') s += x print(' %d' % s)Copy the code
04 Judge leap years
Enter the year to check whether it is a leap year. Leap year: divisible by 4, but not by 100; Or it could be divisible by 400. You need both arithmetic and logical operators
"" year = int(input(' input: ') is_leap = year % 4 == 0 and year % 100! = 0 or year % 400 == 0 print(is_leap)Copy the code
05 Judge odd and even numbers
Enter a number and determine whether the cardinality is even, using modular arithmetic and if… The else structure
"" "judge, even "" "in_x = int (input (' input integer:)) if in_x % 2 = = 0: print (' even ') else: print (' odd ')Copy the code
06 degrees Celsius and Fahrenheit turn each other
We did Fahrenheit to Celsius, and now we have a branching structure that allows them to rotate each other.
""" Trans_type = input(' Input to Celsius or Fahrenheit: ') if trans_type == 'Celsius ': F = float(input(' input Fahrenheit: ')) c = (f-32) / 1.8 print(' Celsius: %.2f' % c) elif trans_type == 'Fahrenheit ': C = float(input(' input Celsius: ')) f = c * 1.8 + 32 print(' Fahrenheit: %.2f' % f) else: print(' please enter Fahrenheit ')Copy the code
07 Whether a triangle is formed
Enter the length of three sides to determine whether they form a triangle. Conditions for forming a triangle: the sum of the two sides is greater than the third side.
A = float(input(' enter three sides of a 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 triangles ') else: print(' can't form triangles ')Copy the code
08 Output grades
Input score, output score corresponding grade.
=90 get A, [80, 90] get B, [70, 80) get C, [60, 70) get D, < 60 get E
"" score = float(input(' 表 格 ') if score >= 90: grade = 'A' elif score >= 80: float(input(' 表 格 ') if score >= 90: Grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: print(' C' elif score = 'D')Copy the code
09 Calculate the commission
The bonus of an enterprise is calculated as a commission according to the following rules. Enter the sales profit and calculate the bonus.
Profit <= 100,000, bonus can be increased by 10%
100,000 < profit <= 200,000, the part higher than 100,000 increases 7.5%
200,000 < profit <= 400,000, the part higher than 200,000 will increase by 5%
400,000 < profit <= 600,000, the part higher than 400,000 yuan will increase by 3%
Profit > 600,000, the portion over 600,000 increased by 1%
""" profit = float(input(' profit ($) : ')) 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 = 200000 * 0.1 + 200000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 200000) * 0.01 print(' bonus: %.2f' % bonus)Copy the code
10 guess games
The program randomly generates a positive integer, the user to guess, the program according to the size of the guess to give the corresponding hint. Finally, it outputs how many guesses the user had to make before they were right.
""" 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(' find a number ') elif number > answer: Print (' 0 ') else: print(' 0 ') break print(' 0 ')Copy the code
11 Print the multiplication table
"" "print my multiplication tables ""," for I in range (1, 1) : for j in range (1, I + 1) : print (f '{j} {I} * = {I * j}', end = '\ t')Copy the code
Whether 12 is prime
Enter a positive integer to determine if it is prime. Definition: A natural number greater than 1 that is divisible only by 1 and itself. Such as: 3, 5, 7
Num = int(input(' please enter a positive integer: Is_prime = True for x in range(2, end) 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 ') else: print(' not prime ')Copy the code
Range (2, end) yields 2, 3… End sequence and in turn assign values to the x execution loop. Range is also used in the following ways
Range (10) : generates 0, 1, 2… 9 sequence
Range (1, 10, 2) : generate 1, 3, 5… 9 sequence
13 The guessing game
With the program to achieve rock paper scissors game.
Import random rule = {' cloth ': 0, 'scissors ': 1,' rock ': 1, 'rock ': 2} rand_res = random. Randint (0, 2) input_s = input(' input rock, paper, scissors: ') input_res = rule[input_s] win = True if abs(rand_res-input_res) == 2: win = False elif rand_res > input_res: Win = False print(f' print: {list(rule-.keys ())[rand_res]}, enter: {input_res}') if rand_res == input_res: Print (' win ') else: print(' win 'if else' lose ')Copy the code
14 Dictionary Sort
The dictionary key is the name and value is the height. Now we need to reorder the dictionary by height.
"" "dictionary order "" "the hs = {' zhang SAN: 178, 'li si: 185,' just soso: 175} print (dict (sorted (hs) items (), key = lambda item: the item [1])))Copy the code
Binary quadratic function v2.0
The binary quadratic function is encapsulated in a function for easy call
"" binary quadratic function v2.0" "" "def f (x, y) : return * x * 2 * 2 + 2 + 3 * * * y 4 * x * y print (f '(1, 2) = f (1, 2)} {f')Copy the code
The last
If you’re new to Python, don’t just rely on tutorials and ignore the ability to use them. Otherwise, it is difficult to make progress. You can do it more, try it, gain experience.
Hope to help you, like the content of the small partners on the thumbs-up and attention!