This is the 10th day of my participation in Gwen Challenge
Don’t let dream just be your dream. Don’t let dreams be dreams.
A few days ago, xiaobian did Python. Earlier we also covered the installation of Python3.8, Portal: First encounter with Python
Other IDLE pythons include Pycharm and Anaconda. The installation of Pycharm is very similar to that of Clion, except that Clion is installed without installation and Pycharm is required to be installed.
Today I’m going to show you a transition between Fahrenheit and Celsius:
Let’s start with the code (Python code is really neat, I have to say) :
TempStr=input(" if TempStr[-1] in ['F',' F']: C = (eval (TempStr [0, 1]) - 32) / 1.8 print (" after the transformation temperature is {: 2 f} C ". The format (C)) elif TempStr [1] in [' C ', 'C'] : F = 1.8 * eval (TempStr [0, 1]) + 32 print (" after the transformation temperature is {: 2 F} F ". The format (F)) else: print (" input error ")Copy the code
I have just started learning Python, so I will try to explain it in more detail.
1. The first line
'TempStr=input(" Please enter a signed temperature value: ")'Copy the code
First, let’s look at the first line of code. The first line of code (as shown above) has a TempStr, which we can call a variable, which is easier to understand if you have a programming background. Unlike C, this variable does not need to be defined, which explicitly states that variables need to be defined before they can be used. Python does not need to be defined in advance; it can be thought of as defining variables at assignment time.
There is also a keyword input, which is used for input. It can be understood as:
Variable =input (prompt for input: what to input (assigned to variable TempStr))Copy the code
2. The second line
if TempStr[-1] in ['F','f']:
Copy the code
Let’s move on to the second line, if you all know what that means, if… If (elif) : elif (else) : elif (elif) : elif (elif) : elif (else)
So if the variable (TempStr)… < string >[M] < string >[M] < string >[M] < string >[M] < string >[M]
Since Python has two types of ordering, called forward incrementing and backward decrement, it should be easy to understand. The forward incrementing sequence starts at 0, just like the small notation used in C arrays. The reverse decrement sequence starts at the last bit of the character, starting at -1, corresponding to the beginning of the increment 0, presumably to better determine the beginning and end of the character.
Back in the code, the [-1] character of the variable TempStr is the last character of the variable TempStr.
In the middle of… in the middle of… Inside, yes, that’s what it means.
And finally [‘F’,’ F’]: what does it mean? By the way, there is still a frame [] that does not talk about pictures pictures pictures, really there is no good picture to talk about. Simple understanding as a boundary, belonging to the 38 boundaries as a child, separate a variety of different statements.
It can be interpreted as whether there is an F and F character in the variable TempStr. Both numbers and letters can be used as characters. F is degrees Fahrenheit in temperature.
3. The third row
C = (eval (TempStr [0, 1]) - 32) / 1.8Copy the code
Let’s look at the next line of code, which looks like a formula, that’s right, to express the formula, and this formula is also a formula for converting Fahrenheit to Celsius. There are also some keywords used in this statement, remember: keywords cannot be used as variables. Eval is a keyword, or evaluation function, that removes the outermost quotation marks from arguments and executes the rest of the statement.
For example: > > > eval (” 1 “)
The output is 1, removing the “” double quotes. Remember to remove only one layer. “” Oh.
>>>eval(‘print(“Hello”)’)
The output is Hello, because once the outermost ” is removed, all that is left is print(“Hello”), which is also a reserved word or keyword. This is very similar to Printf in C, just one letter. It also means output. That is, the output character Hello.
The/symbol in Python means division.
+[0:-1] +[-1] +[-1] +[-1] +[-1] +[-1] +[-1] +[-1] < string >[0:-1] < string >[0:-1]
If the string we enter is 45F, TempStr[0:-1] stands for 45, combining the two permutations mentioned above.
4. The fourth row
Print (" convert temperature is {:.2f}C".format(C))Copy the code
Finally, the output line, the rest of the code except elif, basically explained.
Print print print print print print print print print print print Format (C) replaces the content of variable C with {:.2f}, where.2 can be understood as preserving the two digits after the decimal point. F is added when the output variable is a character, and this is one of the many formats the output can take.
This statement can be interpreted as output as the converted temperature is the content of variable C in +format.
5. A brief introduction
There is also an elif that is not mentioned, which actually looks similar to the else if in C language. It feels like a short form picture. If, if, if, if, if, if, if, if, if, if, if, if, if, if, if
If and elif are not true, execute the else statement.
6. Running result
Finally, let’s take a look at the result: Have fun learning Python with xiaobian.