Python temperature conversion instance analysis and python temperature instance analysis

Source: Internet
Author: User

Python temperature conversion instance analysis and python temperature instance analysis

This document describes how to implement temperature conversion in Python.

The Code is as follows:

# TempConvert. py val = input ("enter a temperature value (for example, 32c)") if val [-1] in ["C", "c"]: f = 1.8 * float (val [0:-1]) + 32 print ("the converted temperature is %. 2fF "% f) elif val [-1] in [" F "," f "]: c = (float (val [0:-1])-32) /1.8 print ("converted temperature: %. 2fC "% c) else: print (" incorrect input ")

Knowledge point:

1. Comment: A single line comment starts with #, and multiple lines comment starts and ends '''

2. input: <variable> = input (<prompt text>) to obtain user input and save it as a string in <variable>

3. the string length is L, the index value of the first byte is 0 or-L; the index value of the last byte is L-1 or-1

Eg. Val = "28C"

Val [-1] is the last character C;

A substring consisting of the first two characters can use Val [0: 2], which indicates the range from [0, 2;

Val [0:-1] can be used to represent strings except the last character.

4. Output: the print () function is used to output character information or output variable values in character form.

The print () function uses % to select the variable to be output.

5. Counting cycle: for I in range (<counting value>)

<Expression>

6. You can change the float () function to the eval () function: eval (str)

Eval () function: Evaluate a string expression, for example:

>>>a=1         >>>eval(“a+1”)2>>>eval(‘pow(2,2)')4

Summary

The above is all the content of this article on the Python temperature conversion instance analysis, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.