【Homework】Homework assigned by our Python teacher

First of all, my code is a screenshot, you need to tap it if you want to use it.
The reason is not explained, the sturdy life does not need explanation.

                                            experiment one

 Question 1. Bring up "The Zen of Python"

1)       Practice goals

    Learn some important principles to focus on to write beautiful Python programs

2) Thought tips

        import this

 

 

Question 2. Compute the following mathematical expressions written in a Python program     

(1)   

 

(2)  

1) Practice goals

    Master built-in numerical operators and variable assignment

2) Thought tips

Pay attention to the operator corresponding to the operator symbol

 

Question 3. The user inputs a four-digit natural number, calculates and outputs the numbers in the thousands, hundreds, tens and ones places

1) Practice goals

     Master the features of built-in numeric type converters and input functions

2) Thought tips

Note the difference between division and division

 

 

 

 

Question 4. Write a script to write your student number, name, age, class and other information into myinf.txt in the following format

      Name: Guo Jing

      Age: 27

      Student ID: D2017001

Class: Class 2

1) Practice goals

    Master basic input and output and file operations

2) Thought tips

Use the open function, pay attention to the use of escape characters

 

 

 

Question 5. Enter the lengths of two sides of a triangle and its included angle, find the length of the third side

1) Practice goals

Calling and using the math library

2) Thought tips

Use the cosine law

Convert angles to radians

 

                                            Experiment 2

 

 

 

 

Problem 4. Generate a list containing 30 squares of random numbers, ranging from 50 to 100, and then arrange them in ascending order of thousands and output them together with their corresponding random numbers. Such as

[6400, 2500, 8100, …]

2500  50

6400  80

8100  90

1) Practice goals

   Application of key and lambda in sorting

2) Thought tips

Use list comprehension, sort(key=lambda....)

 

 

 

 

                                                Experiment 3

 

Question 1. Generate 100 random integers, count the frequency of each integer, and arrange them in ascending order of frequency

1) Practice goals

    Master the basic operations and properties of dictionaries and the use of sorted

2) Thought tips

        Take advantage of random, the get() method of dictionary objects, sorted() and itemgetter()

 

Problem 2. Input an integer n within 1000 from the keyboard , and factor it, such as

 

can be output as a list

1) Practice goals

    Master the use of while loop

2) Thought tips

Note that the factors are all prime numbers, you can start from 2 to judge whether the prime numbers are divisible by n .

 

Question 3. Body Mass Index (BMI) is an important standard commonly used internationally to measure the degree of obesity and health of the human body. Its definition is as follows:

         

The Ministry of Health of my country gives the domestic BMI reference value

 

Classification

BMI value (kg/m²)

thin

<18.5

normal

18.5-24

fat

24-28

obesity

>=28

Input height and weight, output the value of BMI and its category.

1) Practice goals

    Master the application of multi-branch selection structure

2) Thought tips

if and elif

 

 

 

 

Question 4. Input two integers from the keyboard and output their greatest common divisor and least common multiple.

1) Practice goals

   Use of loop structures

2) Thought tips

The greatest common divisor can be obtained by rolling division, and the least common multiple can be obtained by dividing the product of two numbers by the greatest common divisor.

                                            Experiment 4

Question 1. Format and output the binary, octal, decimal, and hexadecimal expressions of 389 respectively, as well as the corresponding Unicode. Requires width of 30, center alignment, padding with '*'

1) Practice goals

    Learn how to format strings

2) Thought tips

        Use the {}.format() method

 

 

Question 2. Caesar cipher: The Caesar cipher is a method used by Julius Caesar in ancient Rome to encrypt military intelligence. It uses a substitution method to replace each English character in the message with the third character after the character in the alphabet sequence. characters, the corresponding relationship is as follows:

原文:ab c d e f g h i j k l m n o p q r s t u v w x y z

密 文 : de fghijklmnopqrstuvwxyz abc

For the original character P, the ciphertext character C satisfies the following conditions:

C=(P+3)mod26

Conversely, the decryption method satisfies:

P=(C-3)mod26

Write an encryption algorithm for plaintext

it was the best of times,it was the worst of times, it was the age of wisdom, it was the age offoolishness, it was the epoch of belief, it was the epoch of incredulity

to encrypt.

1) Practice goals

    Master the basic operations of strings

2) Thought tips

Use ord() function or maketrans() function and translate() function

 

 

 

Question 3. Enter a paragraph of English, in which there is a separate letter I wrongly written as i, please write a program to correct it

1) Practice goals

Common ways to master strings

2) Thought tips

Use replace() function

 

 

 

Question 4. There is an English text in which a word is repeated 2 times, write a program to check the repeated word and keep one. For example, the text content is "This is is a desk", the program output is "This is a desk".

1) Practice goals

   Common ways to master strings

2) Thought tips

Use split() to convert to a list, operate on the list, and then use the join() function to join

 

                                            Experiment 5

 

 

Question 1. Enter a paragraph of English, in which there is a separate letter i wrongly written as I, please write a program to correct it

1) Practice goals

Master regular expression and subpattern syntax

2) Thought tips

use re.sub

 

 

 

Question 2. Given a segment of documents, find all words starting with the letter k or K

1) Practice goals

    Master regular expressions and word pattern syntax

2) Thought tips

        Use re.complie() method and while loop

 

 

 

Question 3. Write a function that receives two integer parameters m and n, and returns the first number in the Fibonacci sequence greater than m+n

1) Practice goals

    Master the definition and calling methods of functions

2) Thought tips

Using a while loop, note the proper way to generate the Fibonacci sequence

 

 

Question 4. Goldbach's conjecture: Write a function that receives an even number, outputs two prime numbers, and the sum of the two prime numbers is equal to the original even number, if there are multiple groups, all output.

1) Practice goals

   Master the definition and calling methods of functions

2) Thought tips

Two functions can be defined, one to determine whether it is a prime number, and one to decompose

 

 

 

                                               Experiment 6

 

 

Question 1. Write a function to determine whether an integer is a prime number, and write the main program to call the function

1) Practice goals

Master the definition and calling of functions

 

 

Question 2. Write a function to accept a string, count the number of uppercase letters, lowercase letters, numbers and other characters, and return the result in the form of a tuple

1) Practice goals

    Master the definition and calling of functions

 

 

 

 

Question 3. In a Python program, does a local variable hide the code of a global variable with the same name? verify.

1) Practice goals

    Understanding the scope of variables

 

 

 

 

Question 4. Define a function using a lambda expression that computes the sum of squares of three integers and one parameter defaults to 5.

1) Practice goals

   Mastering lambda expressions and default value parameters

 

 

 

                                            Experiment seven

 

Question 1. Define a circle class Circle, which can calculate the circle product and perimeter according to the radius

1) Practice goals

   Master class definitions and properties

 

 

 

 

Question 2. Write a program, write a student class, and require the attribute of a counter to count how many students have been instantiated in total

1) Practice goals

    Master the definition and use of classes

 

 

 

Question 3. Define an operation class for a list, including the methods:  

1 list element added

2 Access list elements by index

1) Practice goals

    Understand the definition and use of classes

 

 

 

 

Problem 4. Define a class of students. Has the following class properties:

 1 name

 2 years old

 3 Grades (Chinese, Math, English) 

class method: 

1 Get the student's name: 

2 Get the age of the student

3 Returns the highest score among 3 subjects

Test by instantiating the object

1) Practice goals

Master class definitions and methods

 

If you feel good after reading it, leave a like and leave.

 


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326011009&siteId=291194637