I. Data types and variables 1. Variable and immutable data types
Variable data type: The element inside the data type can be changed in the case of the same ID
List
Dictionary
Immutable data type: value changed, ID changed
Digital
String
Boolean
Add:
True--->1
False--->0
2. Operators
1. Arithmetic Operation:
2. Comparison operation:
3. Assignment Operation:
4. Logic operation:
and annotations:
- In Python, and and or to perform Boolean logic calculations, as you would expect, but they do not return Boolean values, but instead return one of the values they actually compare.
- The value of a left-to-right calculus expression in a Boolean context, and returns the last value if all the values in the Boolean context are true.
- If a value in the Boolean context is False, and returns the first false value
or annotations:
- When using or , the left-to-right calculus is evaluated in a Boolean context, just like and. If a value is true,or returns the value immediately
- If all values are false,or returns the last False value
- Note that or in a Boolean context, the expression calculus continues until the first true value is found, and then the remaining comparison values are ignored
And-or used in combination:
- Combining the two preceding grammars, reasoning can be.
- To enhance the readability of the program, it is best to be associated with parentheses, for example:
(1 and ' X ') or ' Y '
5. Operator Precedence: top-down, high-to-low priority
Two. Basic conditions and cycles
1.if Loop:
If you write a program than do walk, we have so far, have been walking straight, have not encountered too much fork, imagine the reality, you encounter a fork, and then you decide where to turn must be motivated. You have to judge that fork is the way you really want to go, if we want to let the program can also handle such a judgment what to do? Very simple, just need to preset some conditions in the program to judge the sentence, to meet which conditions, the road fork. This process is called Process Control.
Dual Branch:
if condition: satisfies the conditional execution code else: =if ageofoldboy > : Print ("Too old, time to retire. " Else: print(" can also toss a few years! ") ")
View Code
Indent:
This is a feature of Python, forced indentation, the purpose is to let the program know, each piece of code depends on which condition, if not by indentation in the distinction, how will the program know, when your condition is set up, to execute what code?
In other languages, most of them pass
{}
To identify blocks of code, such as C,c++,java,javascript.
Python is the door super concise language, the inventor is to think {}
it is too ugly, so simply do not use it directly, how can distinguish code block it? The answer is force indentation.
There are several principles for Python indentation:
- The top-level code must write the top line, i.e., if a line of code itself does not depend on any condition, it must not be indented
- Same level of code, indentation must be consistent
- The official recommendation is to indent with 4 spaces, of course you can also use 2 if you want to be laughed at.
Multi-branch:
if Condition : satisfies the condition executes the code elif Condition: The above condition does not satisfy to go this elif Condition: The above condition does not satisfy to go this elif Condition: The above conditions are not satisfied go to this else: All the above conditions are not satisfied to go this paragraph
2.while Cycle
Age_of_oldboy = 48Guess= Int (Input (">>:"))ifGuess >Age_of_oldboy:Print("guess too big, go to small li try ...")elifGuess <Age_of_oldboy:Print("guess too small, go to Dali try ...")Else: Print("Congratulations, you guessed right ...")#2nd timeguess = Int (input (">>:"))ifGuess >Age_of_oldboy:Print("guess too big, go to small li try ...")elifGuess <Age_of_oldboy:Print("guess too small, go to Dali try ...")Else: Print("Congratulations, you guessed right ...")#3rd timeguess = Int (input (">>:"))ifGuess >Age_of_oldboy:Print("guess too big, go to small li try ...")elifGuess <Age_of_oldboy:Print("guess too small, go to Dali try ...")Else: Print("Congratulations, you guessed right ...")
Grammar
Count =while# as long as count<=100 executes the following code Printcontinuously (" ", Count) +=1 # Every time it executes, it turns count+1, or it becomes a dead loop, because count is always 0 .
Dead loop
There is a cycle called the cycle of death, once triggered, will run an everlasting, lasting.
While is performed as long as the condition behind it (i.e. the condition result is true) is executed.
Count = 0while#true is true in itself print(" You are the wind I am the sand, tangled up to the horizon ... " , Count) +=1
Loop Abort Statement
If, for some reason, you do not want to continue the loop during the loop, how do you stop it? This will use the break or continue statement
- Break is used to completely end a loop, jumping out of the loop body to execute the statement following the loop
- Continue and break are a bit similar, the difference is that continue just terminates the loop, and then executes the subsequent loop, and break terminates the loop completely.
Example: Break
Count = 0while# as soon as count<=100 executes the following code Print(" ", Count) if count = =5 :break # each time it is executed, the count+1 is turned into a dead loop, because count is always 0 . Print ("-----out of a whileloop------")
Example: Continue
Count = 0 while Count <= : + = 1 ifand# As long as count is between 6-94, do not go to the following print statement, go directly to the next loop continue print(" ", Count)print("-----out of a whileloop------" )
Python Learning: Basic data types and variables (medium) and underlying conditions and loops (on)