py_11_ 0731

Formatted output of three ways:

1. placeholder

Programs often require user input to a fixed format and printing

such as:My name is xxx,my age is xxx.

The easiest is to use the "+" stitch up, and the figure must also be str (digital) came out to splicing, is very troublesome

Such as:

 age = 18
 print('my age is '+str(age)

The above method is too cumbersome, and the mentally retarded, so we use a placeholder, such % s (for all data types),% d (only for the number types)

 name = 'llj'
 age = 18
 print('my name is %s my age is %s' % (name, age))

2.format format

Formatting method is a tasteless, this might as well use by a third. Why do you say, Bibi look on the line.

Such as:

 name = 'llj'
 age = 18
 print("Hello, {}. You are {}.".format(name, age))

3f-String formatting

Compared placeholders and second, or the more simple and clear, and do not bother, it is recommended to use this.

such as:

 name = "LLJ" 
 Age = 18 is
 Print ( F "the Hello, {name}. by You are {Age}.")
 Print ( F "the Hello, {name}. by You are {Age}.") # uppercase F also apply .

Basic operators

1. Arithmetic Operators

Is the arithmetic of elementary school, take the remainder, rounding, exponentiation.

 x = 10 
 y = 5
 print ( x + y)
 print ( x - y)
 print ( x * y)
 print ( x / y)
 print ( x % y)
 print ( x // y)
 print ( x ** y)

2. Comparison Operators

Symbol is used for comparison with ==,! =, <>,>, <,> =, <=

The operator returns a Boolean value

3. assignment operator

 = A b # b is the value assigned to A 
 A = + b # it is equivalent to A + b = A
 A- = b # = ab & just equivalent to A
 A * = b # it is equivalent to a = a * B
 A / = B # it is equivalent to A = A / B
 A% = B # it is equivalent to A% B = A
 A ** = B # can ** equivalent to A = B A
 A = // B # it is equivalent to a = a // b

4. Logical Operators

 x and y # Boolean "and" Only x and y are returns true if True, another return Falise 
 x or y # Boolean "or" x or y as long as one is true, it returns True
 not x # Boolean " not "if x is true, returns Falise, on the contrary, true is returned

The identity of the operator

is是判断两个标识符是不是引用自一个对象,简单点就是这两个变量的id是否相同,相同返回True,反之,返回Falise

is not与is相反就是判断是不是引用不同对象,如果不同返回True,反之,返回Falise

 x = 257
 y = x
 z = 257
 
 print(f'x is y:{x is y}')
 print(f'x == y:{x == y}')
 
 print(f'x is z:{x is z}')
 print(f'x == z:{x == z}')

x is y:True x == y:True x is z:False x == z:True

6.python operator precedence

In fact, this is not very important, because you want Who priority, he brackets on the line. . Just remember logical operators operators latest operation

determining if the flow control

1. Grammar

if the judge is doing it? In fact, if the judge is to make a judgment in the simulation of people. if means that if, say, if doing so, and if so what.

1.1 if

if is to let the computer to judge things.

if condition: 
    Code 1 
    Code 2 
    Code 3 
# code blocks (in the same indent level code, such as code 1, code 2 and code 3 is the same as the indented code, which is a combined three code block, the same reduction into the code will run from top to bottom)

Such as:

cls = 'human'
gender = 'female'
age = 18
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')

Began confession

1.2 if...else

if condition: 
    code block. 1 
the else: 
    block 2

if it said that if the condition is true will execute the code block 1, else indicate otherwise it will execute the code block 2

= CLS 'Human' 
Gender = 'FEMALE' 
Age = 40 
IF CLS == 'Human' and Gender == 'FEMALE' and Age> 16 Age and <22 is: 
    Print ( 'declare start') 
the else: 
    Print ( 'get out Aunt ')

Aunt get out

1.3 if...elif...else

if condition: 
    code block. 1 
elif Condition 2: 
    code block 2 
elif Condition 2: 
    code block. 3 
the else: 
    block 4

executed if condition 1 is established code block 1, elif establishment condition 2 block of code 2, elif condition 3 is established block of code 3, elif ... otherwise, execute block 4.

cls = 'human'
gender = 'female'
age = 28
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
    print('考虑下')
else:
    print('阿姨好')

consider

2.if nesting

That is, in a nested if statements inside the if statement.

# If nested 
CLS = 'Human' 
Gender = "FEMALE" 
Age = 18 is 
is_success = False 

IF CLS == 'Human' and Gender == 'FEMALE' and Age> 16 Age and <22 is: 
    Print ( 'start declare' ) 
    IF is_success: 
        Print ( 'that we go together ...') 
    the else: 
        Print ( 'I tease you play with') 
the else: 
    Print ( 'good aunt')

Began confession I tease you play with

Guess you like

Origin www.cnblogs.com/lulingjie/p/11277487.html