Python Notes---day2:while loops

Source: Internet
Author: User
Tags arithmetic operators logical operators

1. String Integration:

+ No spacing of output results

, spacing of output results

2. Arithmetic operators:

divisible

% Take remainder

* * prescribe

! = does not equal

3. Assignment operators:

A+=1 equivalent to a+1, a*=2 equivalent to a*2

4. Logical operators:

and tie relationships

or or

not non-

Priority:Not>and>or

Short-circuit principle: in two conditions to determine the condition 1, sometimes no longer need to look at other conditions

5. output 1 to All even numbers:

A=1

While a<50:

If a%2==1:

A+=1

Else

Print (a)

A+=1

Print ("End")

6, give the reminder cycle guess Age:

Age=50

Flag=true Adding a state variable

While flag:

Guess=int (Input ("Guess Age:"))

If Guess==age:

Print ("right!")

Flag=false Correct seasonal state variable to jump out while loop

Elif Guess>age:

Print ("Too big")

Else

Print ("Too small")

Print ("End")

7. Other directives:

Break Terminate Loop

Continue jump out of the loop

End= "_" takes _ as the end of the sentence and does not break the line

Print () represents print (end= "\ n"), which is a newline

The else statement after the while is run when the while is at a normal end, that is, When there is no break

8, According to user demand output line and width:

H=int (Input ("Height:"))

W=int (Input ("width:"))

Nh=1

While Nh<=h:

Nw=1 width must be changed once per line, so it is placed within the while loop

While Nw<=w:

Print ("#", end= "")

Nw+=1 across the line.

Print () line break

Nh+=1

Note: Nesting loops, writing simple loops, and substituting ideas into another loop

9, 99 multiplication table:

Hang=1

While hang<=9:

Lie=1

While Lie<=hang:

Print (str (LIE) + "*" +str (hang) + "=" +str (Lie*hang), end= "\ T")

Lie+=1

Print ()

Hang+=1

Note: the connection of strings;"\ t" means tab; Use tab instead of space to make the table neat

Python notes---day2:while loops

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.