Python is an interpreted language in which programs do not need to be compiled, translated into machine language at run time, translated once per execution, and therefore less efficient
Python installation will bring a idle (such as 2-1), that is, you can write code inside, but not good, we use the Python editor--pycharm, in the process of writing code will be prompted to facilitate debugging and operation.
Figure 2-1
First, Notes
1) single-line comment
Python's single-line comment symbol is #, used in code #, and all data on his right is ignored as a comment.
Shortcut key: Ctrl +?
2) Multi-line Comment
Multiline comments enclose the comment in three single quotation marks ("") or three double quotation marks ("").
Second, variables
1 ) variable name rule:
1. Any combination of letters, numbers, underscores 2. The first number must not be the number 3. Variable name it's better to be famous. 4. You cannot use keywords (such as) as variable names
2 ) variable assignment
A variable assignment in Python does not need to specify a data type and can be defined directly.
The equals sign (=) is used to assign a value to a variable. The left side of the equals operator is a variable name, and the right side of the equals operator is the value stored in the variable.
Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
The equal sign is followed by either single or double quotation marks, which is a character string character type, an integer after the equal sign, or an int integer, or a float after the equal sign
As shown:
Code Run Results
Note: the variable is actually stored in the memory address, that is the value of the memory of the place, the variable itself has no type, we say "type" is the variable refers to the type of object in memory.
Third, single quotes, double quotes, three quotation marks
When defining variables, the strings are enclosed in quotation marks, with no distinction between single and double quotes. However, it is reasonable to use the following special circumstances:
1. When there is a single quotation mark in the string, double quotation marks are used as the comment character, or you can use the escape character (\)
2. Double quotation marks are used when there are double quotes in the string, or you can use the escape character (\)
3. When there are both single and double quotation marks in a string, use three quotation marks (either single or double), or you can use the escape character (\)
Four, Function Introduction
1) Input function: Gets the user input and returns the string type.
2) Print function: output; in Python, the print function defaults to newline, which can be written as print (the value to output, end= ').
3) type function: Returns the type of the object.
4) INT function: type conversion, convert to int type
5) Float function: type conversion, convert to float type
Five, Conditional Judgment
Python uses the IF statement to make conditional judgments. Basic forms such as. Python represents the same range in indentation.
The subsequent statement is executed when the condition is determined, and the Else statement is an optional statement.
The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).
If the condition is judged by multiple conditions, then the logical operator and (and), or (or), not (non, negate) is required. Parentheses can be used to distinguish the order of judgment, the judgment in parentheses takes precedence, and the priority of and and or is lower than the > (greater), < (less than) symbols.
Example : User input score, according to score evaluation: greater than or equal to 90, is excellent; less than 90 and greater than or equal to 75, is good; less than 75 and greater than or equal to 60, for passing; less than 60, failing
Six, while Loops
The while statement in Python is used to loop the execution of a program that, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:
The execution statement can be a single statement or a block of statements. When an execution statement is a single statement, it can be written on the same line as while.
The judgment condition can be any expression, the execution statement is executed when it is true, and the loop is ended when it is false.
Example: small game, randomly generated 1-100 of integers, guess this number, each time the hint guess big or small, limit can only guess 3 times
Seven, for Loops
A For loop can be used to traverse an object (traversal: The first element in the loop is accessed once to the last element) in the following format:
Instance:
Eight, Break , Continue
Break: Exits the loop. Used in while and for loops
Continue: Exit this loop and proceed to the next loop. Used in while and for loops
Instance:
Nine, in the Loop Else Statement
In Python, while and for can correspond to a else,else statement that executes after the loop iteration is completed normally. In other words, if we do not exit the loop in any way other than the normal way, then the Else branch will not be executed.
Instance:
10. formatted output
Formatted output is the format of your output.
There are three ways of doing this:
1) with "+" connection , directly connect the output string and variable, not recommended, occupy the memory space
2) placeholder with 3 common placeholders:%s,%d, and%f
%s is the following value is a string,%d is the following value must be an integer,%f is followed by a decimal.
%f 6 decimal places are reserved by default, or you can specify the number of decimals to be retained, in the format:%. Reserved number of decimal places F
%F specifies the ability to have auto-rounding when the number of decimal digits is retained
3)format method : official recommended Use
It replaces% with {} and:
1 , through location
2. by keyword: The name in {} is written casually, but it is the same as the name in the following format.
Getting Started with Python