Experiment 1: Python basics exercise

1, Experimental purpose

1. Master the installation and use of Python environment;

2. Master the application of basic data types;

3. Master how to use pip to manage Python extension library;

4. Master the use of common built-in functions input(), print().

2, Experiment title and results

1. Practice the basic operations of numerical values: +, -, *, / / /,%, * *, hex(),oct(),bin(), etc. Practice the use of type conversion functions, such as str(),bool(),int(),float(), etc. Programming program, input a natural number and output its binary, octal and hexadecimal representations.

```>>> 5+8
13
>>> 34-77
-43
>>> 3*9
27
>>> 5/8
0.625
>>> 5//8 '/ / integer division (floor Division). The result is an integer. Take the largest integer smaller than the result'
0
>>> 5%10            'Surplus'
5
>>> 3**2            'Power operation'
9
>>> hex(55)         'Convert to hex'
'0x37'
>>> oct(55)         'Convert to octal'
'0o67'
>>> bin(55)         'Convert to binary'
'0b110111'
>>> str(123)        'Convert to string form'
'123'
>>> bool(5)         'bool Indicates true or false, and the result is True or Flase'
True
>>> bool(0)
False
>>> int(3.5)        'Convert to integer'
3
>>> float(4)        'Convert to floating point'
4.0```

2. Practice the basic operation of string: +, *, len(),ord(),chr(),in, [], etc. For example: str = 'B21011506 Cangjie', how to get 'B21011506' from str? How to get 'Cangjie' from str?

```>>> '3456'+'abc'                   'Connect two strings'
'3456abc'
>>> "a"*3                          'Repeat for element'
'aaa'
>>> len("jhsckhs")                 'Calculates the length of the string'
7
>>> ord('a')                       'Will character a Convert to ASCII code'
97
>>> chr(65)                        'Convert 65 to ASCII character'
'A'
>>> 3 in [1,2,3]                   'Member judgment'
True
>>> [3] in [1,2,3]
False
>>> alist = [1,2,3,4,5,6,7,8,9]    'Slicing operation'
>>> alist[::]                      'Output all elements in the list'
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[::2]                     'Take one after another, and take the number at the even index position'
[1, 3, 5, 7, 9]
>>> alist[::-1]                    'Reverse all elements'
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> alist[3:8]                     'Take the element from the third position, but do not include the element from the eighth position'
[4, 5, 6, 7, 8]
>>> str = 'B21011506 Cangjie'
>>> str[:-2:]                      'The step size is 1, taken from left to right'
'B21011506'
>>> str[:-2]
'B21011506'
>>> str[:9]
'B21011506'
>>> str[9:]
'Cangjie'
>>> str[-2:]
'Cangjie'```

0 is a special even number

3. Practice installing PyPinyin extension library to complete the function of phoneticizing Chinese characters. (PyPinyin extension library is a library for converting Chinese characters to Pinyin, which can be used for Chinese phonetic notation, sorting, retrieval, etc.) other extension libraries can also be installed to master the management and use of the extension library.

Reference steps:

```pip install pypingyin

>>>import pypinyin
>>>pypinyin.pinyin('Cangjie')
[['cāng'],['jié']]
>>>pypinyin.pinyin('Sunrise',heteronym=True)
[['zhāo','cháo'],['yáng']]```

4. Comprehensive questions

Temperature conversion problem: there are two different systems for temperature characterization. One degree Celsius, used by most countries in the world such as China; Two degrees Fahrenheit, used in the United States, Britain and other countries. If, use f for Fahrenheit and C for Celsius. For example, 82F for 82 degrees Fahrenheit and 36C for 36 degrees Celsius. Combined with the following conversion formula, the temperature conversion problem is programmed.

This problem considers the case of letters. The formula is expressed by using the evaluation function eval() and slicing knowledge. At the same time, the member judgment in is also used. Two formatted output methods are used. The format() method is recommended.

eval(): remove the outermost quotation marks of the parameters and execute the remaining statements. Try to convert any string into a python expression and evaluate it. Sometimes it can be used to realize the function of type conversion.

input(): what you read must be a string. If you want to use the actual data type, you need to do type conversion

format() method: format

infer other things from one fact:

1. Temperature conversion problem is a representative problem of various conversion problems. For example, currency conversion, length conversion, weight conversion, area conversion... The problems are different, but the program codes are similar.

2. In this problem, the temperature value is obtained through input, and the temperature value is directly converted. It can also be used later

1) Understand and convert the sound or image form of temperature information release;

2) Monitor the temperature information release channel, obtain and convert the temperature value in real time;

Tags: Python

Posted on Tue, 12 Oct 2021 02:56:20 -0400 by recklessgeneral