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
Assigning values to variables
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