python artificial intelligence to learn the next day

Pass

    effect:

        Grammar is usually used to fill gaps pass, also known as null statement

Boolean operations

    Operator:

        not     and     or

  1. Boolean NOT operation not

grammar:

    Not x

effect:

    Boolean negation of x, such BOOL (x) True, False, then return

    2. Boolean operation and

            Syntax: x and y

                    Note: x, y represents the expression

            effect:

                    Priority target returns false when X is a Boolean value is False, returns x, otherwise y.

            Description:

                    At the same time it is True, only to True.

                    As long as there is a False, it is False.

Exercise:

  1. Enter a month to determine whether it is legitimate month?
def pd(n):
    X = the eval (n-)
     the try :
         IF 0 <n-<13 is and type (X) == int:
             Print ( " legal " )
         the else :
             Print ( " invalid " )
     the except :
         Print ( " invalid " )
NUM = (the INPUT ( " Please enter a month: " ))
PD (whether)

 

  1. Write a interview process, requirements are men and younger than 50 years old before they can interview?

 

. 1  DEF PD (n-, m):
 2      IF n-==. 1 and 0 <m <50 :
 . 3          Print ( " can interview " )
 . 4      the else :
 . 5          Print ( " not interview " )
 . 6 NUM = int (INPUT ( " Men input 1, input lady 2: " ))
 7 MUM = int (the iNPUT ( " Please enter the Age: " ))
 8 pd (NUM, MUM)

 

 

3. Boolean or operation or

     grammar:

         X or y

     effect:

         Priority returns true object returns x when x is Ture, otherwise y

 

     At the same time as False, it is False

     As long as one is True, it is True

String

  1. effect:

     Used to record the text (text) message

  1. Representation:

     In all non-comment portion enclosed in quotation marks are strings 

     '' apostrophe

     "" Double quotes

     '' '' '' Three single quotes

     "" "" "" Three double quotes

     Empty string literal representation:

     ‘hello’

     “hello”

     ‘’’hello ‘’’

     “””hello “””

     Single and double quotes difference:

         Double quotes for single quotes not terminator

         Single quotes in double quotes is not terminator

         Print(“I am a teacher”)

         Print ( "I am" white "")

     The role of a string of triple quotes

         Three quoted strings may comprise single and double quotes

         Three quoted string is automatically converted into a newline '/ n'

  1. String operations

     +=

     * = Duplicate strings generated, then the original variable binding

  1. Comparison operation

     Operator: <<=>> = ==! =

     Comparison rules:

         By pairwise sequence encoded characters are compared, but a different, the end of the comparison, and returns the result of the comparison, when the exact same length and value encoding, the same two strings.

String code conversion function:

     ord (c) return a unicode character code value of C

     CHR (i) returns the value corresponding character i

operator in not in

    effect:

        In a sequence, the dictionary, the set, for determining a

        not in contrast

The basic operation of the string

    Python strings can not change the sequence of characters

  1. Index values ​​of the syntax:

String [integer expressions]

        Forward Index

        Inverted index

    Slicing

        Action: remove the string from the corresponding elements in the sequence to re-form a new string

        grammar:

            String [a: b (s)]

                Note: parentheses () representative of a portion enclosed omitted

        Description:

            Start index position of the slice is cut starts, 0 represents the first element, the second element represents, -1 is the last termination end index is an index of slice, (but not the end point)

            Each step is slicing the moving direction and the offset of the current element acquisition play

  1. No step, corresponding to step length 1 (default 1)
  2. When the step size is a positive integer, the forward sections taken: The default is the next start index position of the last element
  3. When the step is negative integer of the reverse sections: reverse sections, the default is the last element in the starting position, before the end position is a position of the first element

 

   

Exercise

    Write a program input string print string of the following

  1. Print the first character of the string
  2. Print this last character of the string
  3. If the length of the string is odd, this character print intermediate

 

. 1  DEF Dy (n-):
 2      Print (n-[0])
 . 3      Print (n-[-1 ])
 . 4      IF len (n-). 1% 2 == :
 . 5          X = int ((len (n-)) / 2 )
 . 6          Print (n-[X])
 . 7 NUM = iNPUT ( " Please enter a string of character string " )
 . 8 Dy (NUM)

 

Format string expression

    effect:

        Generating the formatted string

    Operator%

    Syntax:

        Format string parameter values%

        Format string% (parameter 1, parameter 2, parameter 3, ......)

    Description:

        The format string is replaced with the string parameter placeholder beginning%

Exercise:

  1. Enter three lines of text, so that the three lines of text in order to align the right width of 20 characters

Such as:

     Please enter the first line: hello world

     Please enter the second line: abcd

     Please enter the third line: aaaaaaa

     Print results are as follows:

      Hello  world

             Abcd

         Aaaaaaaa

. 1  DEF DQ (n-):
 2      X 20 is =
 . 3      P = ' %%% DS ' % X
 . 4      Print (P% n-)
 . 5 FL = INPUT ( " Please enter the first line " )
 . 6 SD = INPUT ( " Enter The second line " )
 . 7 TD = iNPUT ( " Please enter the third row " )
 . 8  DQ (FL)
 . 9  DQ (SD)
 10  DQ (TD)
 . 11  # ROL. 1 = 
12 is  # KK-len = 20 is (FL) 
13 is  # = kl to '' 
14  # print(kk*kl,end=fl)

  2. string 'hello'

  Creating a new string:

  ‘h-e-l-l-o’

  Tip: use a text analytic approach join method

1 n=input("输入:")
2 str="-"
3 print(str.join(n))

 

loop statement

  while loop for loop

  while statement

    effect

    According to certain conditions, is repeatedly performed one or more statements

    grammar:

    while the true value of the expression

    1 block of statements (statement in this section or may be repeatedly performed a plurality of times)

    else

    Statement block 2

  Description:

    Performing first truth expression 1, the Boolean value of true or flase

    2. If the truth table of the value true, a block of statements is executed, and then returns to the first step again, the true value of the test is repeated the expression.

    3. If the truth table of the value is false, a block of statements is executed 2, and the while statement execution

    note:

      else clause portion may be omitted (the same if statement)

    Precautions while statement:

      1, create and initialize a variable control of the while loop

      2, the loop is controlled by the truth expression

      3, usually loop variable to control the loop condition, to prevent infinite loop.

Exercise:

1, while loop to print an integer of 1 to 20 (print multiple lines)

1 n=input("输入:")
2 str="-"
3 print(str.join(n))

2, printing an integer of 1 to 20 while loop, printed on a line display with a space between each of the digital divider

1 i=20
2 while i>0:
3     print(i,end=' '),
4     i=i-1

3, printing an integer of 1 20, while loop, each line printing 5, 4 lines printed

1 i=19
2 while i>=0:
3     print(i+1,end=' ')
4     if i%5==0:
5         print("\n")
6     i=i-1

Print Pyramid

*

**

***

****

*****

 1 row=1
 2 while row<=5:
 3     col=1
 4     while col<=row:
 5         print("*",end="")
 6         col+=1
 7     print(" ")
 8     row+=1
 9 kk="*****"
10 kl=1
11 while kl<=5:
12     print(kk[0:kl])
13     kl+=1

Exercise:

Input integer n, the specified printing width of a square

Input 5

print

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

Print 99 multiplication table

 1 k=1
 2 while k<=5:
 3     for i in range(1,6):
 4         print(i,end="")
 5     print("")
 6     k+=1
 7 j=1
 8 while j<=9:
 9     m=1
10     while m<=j:
11         print(j,"*",m,"=",j*m,end="   ")
12         m+=1
13     j+=1
14     print("\n")

 

break statement

  problem:

    If you do not want to continue the implementation of the loop, how do ,, you can use the break statement ending cycle role in the cycle?:

    A loop (while, for statement), used to terminate the current loop

  Description:

    After the break statement is executed, this statement after the loop break will not be executed

    The break statement and if statements is usually used in combination

    When the break statement terminates the loop, else clause Loops are not executed

    break statement can only terminate the execution of the current loop, and if there is a loop nest, do not jump out of a nested loop outer weight

    break statement can only be used inside the loop (while, for statements)

for loop

  effect:

    To traverse the data elements may iterables

    Iterables means can in turn get the data object syntax elements:

    for a list of variables in iterables:

    1 block of statements (statement block herein may repeatedly executed)

    else:

    Statement block 2

  Description:

    Loop terminates when the current circulating in vivo break, else clause portion does not execute the statement else clause portion may be omitted

    range () function

    Range (stop) to generate an integer in the range 0 stop, until a stop (not included stop)

    range (star, stop [, step]) to generate an integer in the range of start stop, until the stop (STOP not included), each integer step interval

      effect:

        Used to create a build. Iterable series of integers (also called a sequence generator)

 

Guess you like

Origin www.cnblogs.com/linxingh/p/11074333.html