Python Language Programming Songtian Notes 1

Class Notes:

#TempConvert.py Temperature Conversion Example                     
TempStr = input("Please enter a temperature value with a sign: ")
if TempStr[-1] in ['F', 'f']:                
    C = (eval (TempStr [0: -1]) - 32) /1.8
    print("The converted temperature is {:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
    F = 1.8 * eval (TempStr [0: -1]) + 32
    print("The converted temperature is {:.2f}F".format(F))
else:
    print("Incorrect input format")

Comment: Auxiliary explanatory information not executed by the program

-Single-line comments: start with #, followed by comments

- Multi-line comments: start and end with '''

Variables: placeholders used to hold and represent data

-Variables are represented by identifiers (names), and the process of associating identifiers is called naming

        TempStr is the variable name

- You can use the equal sign (=) to assign or modify a value to a variable, = is called an assignment symbol

        TempStr = '82F' #Assign '82F' to the variable TempStr, where there is no difference between single quotes and double quotes

Naming: the process of associating identifiers

- Naming rules: uppercase and lowercase letters, numbers, underscores and Chinese characters and combinations

        Such as: TempStr, Python_Great, this is a python class

-Notes: case sensitive, the first character cannot be a number, not the same as reserved words

        Such as: Python and python are different variables, 1python is illegal

Reserved Words: Identifiers defined internally by a programming language and reserved for use

-The Python language has 33 reserved words (also called keywords)

        

- Reserved words are the basic words of programming languages ​​and are case sensitive

       if is a reserved word, IF is a variable

data type: the form of data understood by a computer program

- Integer type: 82

- float type: 82.0

- String type: '82'

- List type: [82, 81, 80]

String serial number:

    

Use of strings: use [] to get one or more characters in a string

-Index: Returns a single character in the string <string>[M]

        For example: TempStr = '82F', the return result of TempStr[-1] is F, and the return result of TempStr[0] is 8

-Slice: Return a substring of characters in a string <string>[M:N]

        For example: TempStr = '82F', the return result of TempStr[0:2] is 82

Branch statement: a statement that determines the direction of program execution by the judgment condition

- Use reserved words if, elif, else to form the branch structure of conditional judgment

if TempStr[-1] in ['F', 'f']: #If the condition is True, execute the statement after the colon

- There is a colon (:) at the end of the line where each reserved word is located, part of the syntax

        The colon and subsequent indentation are used to indicate the affiliation of the subsequent statement with the condition

Function: A functional process that produces different outputs according to input parameters

-input(): function to get user input from console

-print(): function to output the result to the console in character form

-eval(): function that removes the outermost quotes of the parameter and executes the rest of the statement

        eval('2')        eval('1 + 2')

        2                    3

Guess you like

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