python loop condition judgment

1, python input and output

Input: s = input ( "Enter:")) #input type str is received

Output: print ( 'hello world')

2, python of single and double quotation marks, and three marks (three single quotes)
Python variable is defined when strings are enclosed in quotation marks, single and double quotes no difference, with which are good, but if this string inside single quotes, then double quotes on the outside; inside double quotes, then outside to use single quotes; if there are both single-double, then a triple quotes.

Three marks may be multi-line comments in the code, single-line comments, using # (shortcut ctrl + /).

Print ( "Hello World") 
Print ( " 'Hello World'")
Print ( '' '' "Hello World" '' '')
'' '
Print ( "Hello World") # Comment out this paragraph
' ''
2 condition judging
age = input ( "Please enter the Age:")) 
IF Age <18:
Print ( 'minors')
elif Age> = 18 and Age <60:
Print (' middle-aged ')
the else:
Print (' the elderly ')

3, circulation

 #while first define a while loop counter

count = 0

while count < 3:

       print('xxxx')

       count+=1

       break # plus break, an immediate end to the whole cycle

 

The following example write a Guess: a randomized input a number between 1-100 judge guess how big or small a guess, a total of seven times the opportunity to 
# use while
Random Import 
Number = the random.randint (1,100)
COUNT = 0
the while COUNT <. 7:
COUNT +. 1 =
GUESS = int (INPUT ( 'Enter a number:'))
IF GUESS == Number:
Print ( 'guessed, end ')
BREAK
elif gUESS <Number:
Print (' small guess')
continue # end of this cycle, behind the code is no longer executed, execution continues to the next cycle; may continue without, because there is no code elif below, and the next cycle will be

the else:
Print ( 'big guess')
the else: 
Print ( 'the number of errors and light')

#用for
Random Import 
Number = the random.randint (1,100)
for I in Range (. 7):
GUESS = INPUT ( "Please enter a number: ')
GUESS = int (GUESS)
IF GUESS == Number:
Print (' guessed ')
BREAK
elif gUESS <number:
Print ( 'small guess')
else:
Print ( 'guess the da')
else:
Print ( 'number of errors and optical') #for or else while the corresponding left and right after the normal end, will execute the code inside else

4, the operator

Arithmetic operators

Operators description example
+ addition  a + b = 30
- Subtraction  a - b = -10
* multiplication a * b = 200
/ except b / a = 2
% Mode - the operand is returned by right and left-hand operand is divided by I b % a = 0
** Index - index performing calculation operations (power) of a ** b = 20 power of 10
// Quotient of the divide operands, wherein the result is the number of decimal places to be removed - in addition to the floor 2 = 4 and 9 // 9.0 // 2.0 = 4.0

Comparison Operators

Operators description Examples
== Check the value of two operands are equal, if the condition becomes true (A == b) is false
!= Check the value of two operands are equal, if the values ​​are not equal, the condition becomes true a! = b) is true.
> Check whether the value of the left operand is greater than the value of the right operand, if so, the conditions are satisfied (A> b) is not true.
< Check the value of the left operand is less than the value of the right operand, if so, the conditions are satisfied (a < b) 为 true.
>= Check whether the value of the left operand is greater than or equal to right operand, if it is, then the condition is satisfied (A> = b) is not true.
<= Check the value of the left operand is less than or equal to the value of the right operand, if it is, then the condition is satisfied (a <= b) 为 true.

Python assignment operator

Operators description Examples
= Simple assignment operators, assignment number from the left right operands c = a + b a specified value a + b to c
+= AND assignment operator addition, it increases the left and right operands and assign the result to the left operand Operand c + = a corresponds to c = c + a
-= Less AND assignment operator, it subtracts the right operand from the left operand and assign the result to left operand c - = a corresponds to c = c - a
*= Multiplication AND assignment operator, which is multiplied by the right operand and the left operand and assign the result to left operand c * = a c = c * a corresponding to
/= Division AND assignment operator that the left and right operand operand assigns the result to the left operand c / = a corresponds to c = c / a
%= Modulus AND assignment operator, it needs to use the modulus of the two operands and assign the result left operand c% = a corresponds to c = c% a
**= Index assignment operators AND, execution index (power) is calculated and the operator assigned to the left operand c ** = a corresponds to c = c ** a
//= In addition to the floor, and assign a value, the floor and the left operand of assignment to the addition operation c // = a corresponds to c = c // a

Logical Operators

Operators description Examples
and The logical AND operator. If both operands are true, then the condition is satisfied. (a and b) 为 true.
or The logical OR operator. If two operands are zero then condition becomes true. (a or b) 为 true.
not The logical NOT operator. Logic inversion operation state number. If a condition is true, the logic NOT operator returns false. not(a and b) 为 false.

Identity operator

Operators description Examples
is It is determined whether or not the two identifiers from an object reference x is y, if id (x) equals id (y), returns True
is not is not判断两个标识符是否引用不同的对象

Guess you like

Origin www.cnblogs.com/lsl1230/p/11517595.html