Python Learning Notes (loops, several ways to print, operators)

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators

one, loop (for, while)

A while loop is the execution of a loop body when a given condition is established (true), otherwise exiting the loop. A For loop is a repeated execution of a statement.

break terminates the For/while loop when needed

continue Skips the statement behind it, ends the loop, and starts the next round of loops.

1. For loop (for ... else ... )

Used to traverse an object and also has an optional else block that comes with it.

The format for the For statement is as follows:

For <> in < object collection;:

If < conditions;:

Break

If < conditions;:

Continue

< other statements >

Else

<>

Note: The value entered in the following example is coerced into a number with an int to be compared to a random number, which is the STR type before conversion.

2. While loop (while ... else ...) )

In Python, the while loop is used to loop the execution of a program, that is, under certain conditions, loop through a program. The basic form is as follows:
While judging condition:
EXECUTE statement

The executed statement can be a single statement or block of statements, and the execution can be any expression, and any value other than 0 or non-null is true. When the condition is false false, the loop ends.

Use while to implement the For loop above, as shown below,count needs to initialize the value, and each count will need +1, otherwise it will be 1, into the dead loop.

Ii. several ways of writing print

Here are a few ways to do print:

%s string
%d int
%.2f Float

Operators (arithmetic operators, comparison operators, logical operators) Arithmetic operators:

+ Plus
-Minus
* Multiply
/except
Floating point Division (rounding)
% take-up
* * exponentiation

# count = count+1
# count+=1
# count = count-1
# count-=1
# count= Count*1
# count*=1
# count = Count/2
# count/=2

Comparison operators:

Python's standard comparison operator has:< <= > >= = = <> Returns the True/false boolean data type, depending on the true or false expression.

Logical operators:

And, or, not are the logical operators provided by Python, and the function is to concatenate the expressions together to get a Boolean value. For example, this is the following:

>>> 5 < 6 and 5 = = 6
False
>>>
>>> 8 > 6 or 5 < 6
True
>>>
>>> Not 4 <= 3
True
>>>
>>> 5 < 6 < # equals 5 < 6 and 6 <
True

Python Learning Notes (loops, several ways to print, operators)

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.