The path of Python cultivation-day2

Source: Internet
Author: User
Tags arithmetic

Tag: Precedence equals ESS requires logical statement result while loop%s

A. While loop

Basic usage:

While condition:

    Code Block 1

Else

    Code Block 2

Running process: 1) When the condition is set, execute code block 1

2) again determine if the condition is true, if true, execute code block 1 again ....

3) When the condition is false. Execute else jumps out of the loop. End of cycle

1. Use the while loop to output 1-10 integers

I=1 while i<=10:    print(i)                        

2. Outputs 1-100 of all integers and

I=1=0 while i<=100:    sum=sum+i    i+=1print (i)

3. All odd numbers in the output 1-100

 while Num <=:    if num% 2! = 0:        print(num)    + = 1

4. Guess the number of small games

Count = 3Num=66 whileCount >= 1: Guess= Input ("Please enter a number:")    ifGuess = =Num:Print("guess the result is correct")         Break    elifGuess >Num:Print("guess the result is big.")    Else:        Print("guess the results are small.") Count-= 1Else:    Print("you are so stupid .")

5. Ask for the 1-2+3-4+5....99 of all the numbers.

Start = 1= 0 while start <100:    = start% 2    if temp ==1:        /c7>= sum + start    else:        = Sum- start    + = 1print( Sum

6. Enter a number to determine the number of digits (implemented by the algorithm)

1 count = 12 num = Int (input (" Please enter a number:"))3while  True:4     num = num/105     if num <:6 c16/>Break7         8     count + = 29print(count)

Two. Formatted output

%s: a placeholder for a string that can place any content (number)

%d: placeholder for number

1. Example of a string placeholder

1Name = input ("Please enter your first name:")2Age = Input ("Please enter your age:")3Hobby = input ("Enter your hobby:")4gender = input ("Please enter your gender:")5 6 Print("%s Old this year, is an old man, hobby is%s, gender:%s"% (name, age, hobby, gender))

2. Example of a digital placeholder

A = 108" there are%d characters in Liangshan shui Bo " % (a)print(s)

>>>> Tip: If a placeholder is in the string. Then all the percent behind is a placeholder. need to be escaped;% or%

three. Operators

operations can be divided into: arithmetic Operations , comparison operations , logical Operations , assignment Operations , member operations, identity operations, bit operations.

1. Arithmetic operations

operator : ' + ', '-', ' * ', '/', '% ', '//', ' * * ' (subtraction takes the whole power in order)

2. Comparison operations

  operator : ' = = ', '! = ', ' <> ', ' > ', ' < ', ' >= ', ' <= ' (equals not equals not equal to less than or equal to less than or greater than or equal to)

3. Assignment operation

  operator : ' = ', ' + = ', '-= ', ' *= ', '/= ', ' **= ', '//= ', '%= '

4. Logic operation:

  operator : ' and ', ' or ', ' not '

  and: and meaning. The values on both sides must be true. The result of the operation is true.

  or: or meaning. One of the left and right ends is true. The result is true. All is false. The result can be false

  not: non-meaning. It turns out to be false. It's true now. Not true or false, not false both true

Logical operation Priority: () > Not > and > or

Determine the true,false of the following column logical statement sentences.

3>4or4<3 and1==1 1 < 2 and3 < 4or1>2 2 > 1 and3 < 4or4 > 5 and2 < 1 1 > 2 and3 < 4or4 > 5 and2 > 1or9 < 8 1 > 1 and3 < 4or4 > 5 and2 > 1 and9 > 8or7 < 6 not2 > 1 and3 < 4or4 > 5 and2 > 1 and9 > 8or7 < 6

The >>>> operation results are: false, True, True, False, False, False

The second method of operation :

       x or Y, X is true, value is x,x false, value is y

X and y, X is true, value is y,x false, value is x

Determine the following logical statements

Print(0or1or3or0or5)    Print(1 and2)  Print(2 and0)Print(0 and3)  Print(0 and4)  Print(0or4 and3or7or9 and6)Print(2 > 3 and3)      Print(2 < 1 and4 > 6or3 and4 > 5or6)

The results of the >>>>>>> operation are: 1, 2, 0, 0, 0, 3, 0, 6

Break ends the loop. Stop the current layer loop
Continue ends the current cycle. Proceed to the next loop

The path of Python cultivation-day2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.