2019.7.31 learning content and notes

Formatted output

Defined : the output is in line with certain norms and specifications here called the formatting

Three kinds of format

  1. The first embodiment (placeholders) based version of the output python3.0

The advantage of this approach is that the output can be of any type of string concatenation, placeholders two forms, one% s (for all data types),% d (only for the number types), for example:

name = 'nick'
age = 19 
print('my name is %s my age is %d(或者%s)' %(name, age)) # my name is nick my age is 19
  1. The second way ( format formatting )

    This approach is based on python3.4 version formatted output mode (but basically nobody used)

First, when a lot of the time parameter, if used in this way (format format) will be very troublesome, because in the final print when you use the ".format ( 'parameter 1', 'parameter 2', '3 parameters '........) way to control them is very troublesome, so very few people use

  1. The third embodiment (f-String of format)

This approach is based on version python3.6 formatted output, expression is: print (f 'description information: variable name corresponding to {1}, description information: variable name corresponding to {2}'), if wherein a parameter required to print a small value, expressed as: print (f 'description: {corresponding variable names: .2f}), ":. 2f" that is two decimal places

Basic operators

Into basic operators: 1. arithmetic operators (+, -, *, /, // (rounded),% (remainder), ** (power)) and the like

  1. Comparison operators (>, <, ==,> =, <=,! =,), Comparison result is "true or false" is displayed
  2. Assignment operator (=, + =, - =, * =, / =, = //, =%, ** =)
  3. Logical operators (and, or, not)
  4. The identity of the operator (is, is not)

python operator precedence

python operator precedence in mathematics equivalent of the first count and then count addition and subtraction multiplication and division, just remember, you want whoever operation would parentheses like

determining if the flow control

What is flow control, is from top to bottom a corresponding operation step by step, a predetermined sequence

determining if the flow control

if the assumption is that, if the meaning, if the condition is a word, a given condition, if ....., ....... what if, given a specified condition, if the conditions are met, what will happen do, what will happen if the condition fails to do, like we sentences primary school, what if, just what. It belongs to a single branch structure

light = "green"
if light ='red':
   print('等')
print('啥子')

if ... else, it represents a branched structure bis

Representation is established if the code will do, else set up will not do, I feel just like this sentence in elementary school (how do you, how else), if the front does not hold back will be performed, for example,

light = 'yellow'
if light == 'red':
    print('等')
else:
    print('闯马路')
    
print('shit')
    

Multi-branch structure, by definition is the determination of a plurality of words

if ... elif ... elif ... else

Representation is what to do after the establishment of conditions 1, 2 established the conditions to do, what to do after the establishment of conditions 3, and then how to do otherwise

light = 'white'
if light == 'red':
   print('等')
elif light == 'yellow':
   print('注意')
elif light == 'green':
   print('请过马路
   ')
else:
   print('傻子,没有这个信号灯')
   
 print('shit')
age = 18

inp_age = input('age:')  # 17
inp_age = int(inp_age)

if age > inp_age: # a # 条件a成立我就做
    print('猜小了')
elif age < inp_age: # b  # 条件b成立并且条件a不成立才做
    print('猜大了')
else:  # c  # 条件a和b都不成立才做
    print('猜中了')

if nesting

if the nesting is determined if there are a plurality of word

My personal understanding is that in meeting a condition, the condition itself has diversity, such as if a> = b, the what if it would do when a> b, what will happen if the time to do a == b, or ( else) what to do

age = 18
inp_age = int(input('age:'))

if age >= inp_age:
    if age > inp_age:
      print('猜小了')
    else:
       print('中了')
else:
    print('猜大了')
    
             
           
       
    

Guess you like

Origin www.cnblogs.com/chmily/p/11278650.html