Python program Syntax element analysis

Source: Internet
Author: User
Tags eval

Python program Syntax element analysis

[TOC]

Format Framework for Programs

Here is an example of a temperature conversion code:

#TempConvert. PYTempStr= input("Please enter a temperature value with symbol:")iftempstr[-1]inch[' F ',' F ']: C=(Eval(tempstr[0:-1])-  +)/1.8    Print("The converted temperature is {:. 2f}f".format(F))eliftempstr[-1]inch[' C ',' C ']: F= 1.8*Eval(tempstr[0:-1])+  +    Print("The converted temperature is {:. 2f}f".format(F))Else:Print("Input format error")

Code highlighting is a color aid system, not a grammar requirement, you can check for basic grammatical spelling errors

In Python, indentation is part of the syntax, so it is not possible to add indentation arbitrarily, otherwise there will be a syntax error, indented as follows:

For indentation, the following requirements are available:

Indent in

Formatting framework for Indented expression programs

  • strictly clear: Indentation is part of the syntax, and an incorrect indent program will run an error
  • Affiliation: The only way to express the inclusion and hierarchical relationships between code
  • consistent length: for indentation, in the same program to maintain the same length, generally with 4 spaces or 1 tab to indicate

    Note in Python, use # to represent annotations, such as: to #TempConvert.py improve code readability and not be executed
  • Single-line comment: start with #, followed by comment content

    # here is a single-line comment

  • Multi-line Comment: with '开头和结尾 > \`\`\` 这是多行注释第一行 > 这是多行注释第二行 \`\

    Named and reserved word variable variables: placeholder symbols for saving and representing data in programs

  • naming: variables are represented by identifiers (names), and the process of associating identifiers is called naming
  • Naming conventions: characters and combinations of uppercase and lowercase letters, numbers, underscores, and Chinese characters
    Such as:TempStr,Python_great,我在学Python
  • Caveats: case sensitive, the first character cannot be a number, not the same as the reserved word
    such as: Python and python are different variables, 123python is not legal
  • Assignment Symbols: You can use an equal sign (=) to assign or modify a value to a variable, = is called an assignment symbol

    Reserved word reserved words are identifiers that are internally defined and reserved for use by the programming language
  • Python language has 33 reserved words (keywords)
    if,elif,else,in
  • Reserved word is the basic word of the programming language, case sensitive
    ifis a reserved word, If is a variable

    Data type
  • integer type: if10011101

  • String Type: if"10,011,101"
    An ordered sequence of characters consisting of 0 or more characters
  • Strings are represented by a pair of single or double quotation marks
    such as: “请输入带有符号的温度值” orC
  • A string is an ordered sequence of characters that can be indexed
    "请"is "请输入带有符号的温度值:" the No. 0 character
  • Ordinal of a string

  • Use of strings
    Use [] to get one or more characters in a string
  • Index: Returns a single character in a String < string >[m]

  • Slices: Returns a string of characters in a substring < string >[m:n]

    Number Type
  • integer: an integer in mathematics
    32Or-89
  • floating point: a real number in mathematics with a decimal part
    1.8or -1.8 or-1.0

    List type list Type is an ordered sequence of 0 or more data
  • A list [ ] is represented by a (,) delimited element.
    [‘F‘,‘f‘]Represents two elements F and af
  • Use a reserved word in to determine whether an element is in the list
    For example, TempStr[-1] in[‘C‘,‘c‘]: to determine if the former is the same as an element in the list

    Statements and functions
  • Assignment statement: A line of code consisting of an assignment symbol
  • Assignment statements are used to assign new data values to variables
    C = (eval(TempStr[0:-1]) - 32)/1.8 #右侧运算结果赋给变量C
  • The data type on the right side of the assignment statement acts on the variable
    TempStr = input("请输入带有符号的温度值:") #input()返回一个字符串,TempStr 也是一个字符串
    Branch statements

  • A branch statement is a statement that determines the direction in which a program runs by judging conditions
  • if elif else Branching structure judged by the condition of reserved words
    if TempStr[-1] in[‘F‘,‘f‘]: #如果条件为true则执行冒号后边的语句
  • The last colon in the row for each reserved word is part of the syntax, and the colon and subsequent indents are used to denote the relationship of the subsequent statement to the condition

    Function
    function: The function process of producing different output according to the input parameters
  • Functions similar to mathematics, y=f (x)
    print("输入格式错误") #打印输出“输入格式错误”
  • Functions are used with < function name > (< parameter >)
    eval(TempStr[0:-1] #TempStr[0:-1]是参数

    Python program input and output
  • input (): a function that obtains user input from the console

  • Use format for the input () function:
    < variable >=input (< hint information string >)
  • The information entered by the user is stored in the < variable > in the String type
    ' TempStr = input ("Enter a temperature value with symbol:") #TempStr保存用户输入的信息
  • print (): a function that outputs results to the console in character form

  • Basic use format for the print () function:
    print(<拟输出字符串或字符串变量>)
  • A pair of quotation marks of a string type are used inside the program, with no quotes entered
    print("输入格式错误") #向控制台‘输出输入格式错误’
  • Format of the print () function:

  • eval () a function that strips out the outer quotes of the parameter and executes the rest of the statement

  • The basic use format of the eval () function:

    For example:eval(TempStr[0:-1])
    If the tempstr[0:-1] value is "12.3", the output is 12.3

Python program Syntax element analysis

Related Article

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.