Python Foundation One

Source: Internet
Author: User

The first Python program
>>> print (' Hello World ') Hello world>>>

The >>> before print is called a prompt (different language prompts are used only in Python). Print is the function of the screen after the display of print content, here is the Hello World

If you write a program in Notepad, you need to save it as a. py format. The. Py is a suffix and is a file that lets the reader know that it is python.

Variable

The value that the variable is stored in memory. This means that there is a space in memory when creating variables. Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory. Therefore, variables can specify different data types, which can store integers, decimals, or characters.

name = "Flash" age = 22
#这里name, age is represented as a variable with a value of: flash,22

The function of the variable is to save the data for later invocation. Imagine that if you don't define a variable, it's a waste of time to define it one at a time using the same value in a large project. If a variable is defined beforehand, subsequent calls to that data will be much more convenient.

Variable naming rules
    • To have a descriptive
    • Variable names can only be _, numbers, letters, not spaces or special characters (#?<.,¥$*!~)
      name = "Flash" #正确name1 = "flash" #正确name_nn = "flash" #正确 ¥name  = "Flash" #错误
    • Cannot use Chinese as a variable name
      name = "Flash" #错误
    • Cannot start with a number
      5name = "Flash" #错误
    • Reserved characters are not allowed to be used. such as [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' From ', ' global ', ' if ', ' Import ', ' on ', ' is ', ' lambda ', ' no ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' W ' ith ', ' yield ']
Assigning values to variables
    • Variable assignments in Python do not require a type declaration.
    • Each variable is created in memory and includes information about the identity, name, and data of the variable.
    • Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
    • The equals sign (=) is used to assign a value to a variable.
    • The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable.
      >>> num =            #整型变量 >>> num1 = 100.0        #浮点型 >>> name = "Flash"      #字符串 >>> Print (num) 100>>> print (NUM1) 100.0>>> print (name) flash>>>

Constants are relative to variables, so constants are the amount that will not be modified when the program is run. Constant zones are divided into different types, such as 25, 0, 8 as shaping constants, 6.8, 7.89 as real constants, and ' a ' B ' for character constants. Constants are generally judged by their literal form. Like pie = 3.141592653 ....

All variables in Python are mutable, so the variable name in all capitals is used to represent the secondary variable as a constant.

Comments

Comments are divided into single-line comments and multiline comments, the main function is to comment out the extra code or in the beginning or in the middle of the program, without any function implementation, is simply a statement to explain the program. Make it easy for you and others to understand the code later.

Single-line Comment: format as # commented content

Multi-line Comment: Format such as ' annotated content ' or ' ' ' Annotated Content ' ""

User input
Name = input (' Enter your Name: ') Age = input (' Enter your Age: ') print (name,age) F:\>python Cn_support.pyenter your name : Flashenter your Age:22flash 22

Note: In Python2, user input is Raw_input ()

Conditional judgment

A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).

The Python program language specifies any non-0 and non-null (NULL) values of true,0 or NULL to FALSE.

Basic formats such as

If  judgment condition:    Execute statement ... else:    EXECUTE statement ...

If the condition is true, then the following statement is executed, which can follow multiple lines. In Python, indentation in a specific form represents the scope of the same executable.

else is optional, when the condition is set, the statement skips and executes when the condition is not established.

Age_oldboy = 56guess_age = Int (input (' age '))  #输入自己猜测年龄if guess_age = = Age_oldboy:    #判断输入年龄是否相等. Print (' yes,you got it ') #条件成立时输出else:p rint (' No, it is wrong ')  #不成立时输出

When judging multiple conditions, a single if else does not meet the requirements. This can be resolved as follows.

If judgment condition 1:    EXECUTE statement 1......elif judgment Condition 2:    EXECUTE statement 2......elif judgment Condition 3: Execute statement    3......else:    EXECUTE statement 4 ...

Specific forms of execution such as

score = Int (input ("score:"))  #输入成绩if score >:    print ("A") Elif score >:    print ("B") Elif score > 7 0:    print ("C") Elif score >:    print ("D") Else:    print ("E")

  

 

  

Python Foundation One

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.