Fourth, the output formats and basic operators

1, the output format

  Procedure, there is often such a scenario: the user to enter information, and printing a fixed format.

  Such as requiring the user to enter a user name and age, and then print the following format:

  My name is xxx,my age is xxx.

  Obviously, a character with a comma splice, the user can input the name and age into the end, not placed in the specified location xxx, and the figure must also be str (digital) conversion to string splicing.

  This uses a placeholder, such as:% s,% d.

1.1,% s placeholder string; acceptable strings, numbers may also be received. E.g:

print('My name is %s,My age is %s'%('egon',18))

1.2,% d placeholder string; only receive digital. E.g:

print('My name is %s,My age is %d')%('egon',18)

Print ( 'My name IS% S,% S My Age IS')% ( 'Egon', 18 is)    #age type string, can not pass% d, it will be given

1.3, receiving user-specified input, to the specified print format

name=input('your name:')

age = input ( 'your age:') # 18 a user input, is stored as a string 18, can not pass% d.

print('My name is %s,My age is %s'%(name,age))

2, basic operators

  With an operational computer technology for there are many, can not only simple addition, subtraction, according to the type of operation can be divided into: math, comparison operators, logical operators, assignment operators, members of the operation, operation status, bit operation.

2.1 Arithmetic

  Suppose variables: a = 10, b = 20

Operators description Examples
= Simple assignment operator c=a+b
+= Addition assignment operator c + = a is equivalent to c = c + a
-= Subtraction assignment operator c- = a is equivalent to c = ca
*= Multiplication assignment operator equivalent to c * = a c = c * a
/= Division assignment operator c / = a is equivalent to c = c / a
%= Take the remainder assignment operator c% = a is equivalent to c = c% a
**= Power assignment operator c ** = a is equivalent to c = c ** a
//= Assignment operator take divisible c // = a is equivalent to c = c // a

2.2, logic operations

Operators description Examples
and "And" gate, x is false, or x and y are false, the output values ​​of x, y values ​​otherwise (A and b) returns true
or "Or" gates, x is true, the value of the output of x, y values ​​otherwise (A or b) returns true
not "NOT" gate, x is true, output x is false, x is false, the output x is true not (a and b) returns true

2.2.1, the priority relationship between the three: not> and> or, the same level as the default from left to right calculation.

>>> 3>4 and 4>3 or 1==3 and 'x' == 'x' or 3 >3
False

2.2.2, it is best to distinguish priority in brackets, in fact, the same meaning as above

Principle:
2.2.2.1, not the highest priority, is the condition that results closely followed by negation, and not so closely followed the conditions indivisible

2.2.2.2, if the statement is made and all connected, or connected with, or all, then from left to right in order to sequentially calculate

2.2.2.3, and if both have the statement or, then the first brackets and the left and right two conditions to be enclosed, and then calculates

>>> (3> 4 and 4> 3) or (1 == 3 and 'x' == 'x' ) or 3> 3

talk

2.2.3, the short-circuit operation: Once the result of the logic operation can be determined, then it returns the current value to calculate the final result.

>>> 10 and 0 or '' and 0 or 'abc' or 'egon' == 'dsb' and 333 or 10> 4
we use parentheses to clarify what priority
>>> (10 and 0) or ( '' and 0) or 'abc' or ( 'egon' == 'dsb' and 333) or 10> 4
short-circuit: 0 '' 'abc'
                 false false true

Returns: 'abc'

2.2.4, the short-circuit operation face questions:
>>> #. 3. 1. 1 or
>>>. 3. 1 and #. 3
>>> # 0. 1. 1 and 2 and 
>>> # 0. 1. 1 and 2 or
>>> 0 and or. 4. 1 or # 2. 1
>>> 0 or False. 1 #False and
2.2.5, the identity calculation

  is comparable is the id

  The comparison is value ==

Guess you like

Origin www.cnblogs.com/jingpeng/p/12374971.html