Python entry _ introduction to logical judgment and operators, and introduction to python

Source: Internet
Author: User

Python entry _ introduction to logical judgment and operators, and introduction to python

This is the 6th article on Python, which mainly introduces logical judgment and operators.

(1)

Logical judgment:

To implement a complex functional program, logical judgment is essential. The most basic criterion for logical judgment: boolean type.

The boolean type has only two values: True and False, which correspond to 1 and 0 in Python.

Enter the following code in Pycharm. After running the command, the feedback is True or False.

print(1 > 2)print('m' in 'member')print(7 >= 7)print(3 != 3)

Here, the expression that can return a Boolean value is called a Boolean expression. There are many boolean expressions, which are described below.

(2)

1. Comparison operators:

If the comparison is True, True is returned. If the comparison is not True, False is returned.

Common comparison operators are as follows:

In addition to comparing simple two values, comparison operators also support relatively complex comparisons.

(1) Multi-condition comparison:

You can assign a value to variable a and then compare multiple conditions.

 a = 3 print(1 < a < 5)

(2) Comparison of variables:

Assign values to variables a and B for comparison.

a = 3b = 3print(a != b)s1 = 'duwangdan's2 = 'DuWangDan'print(s1 == s2)

(3) Comparison of function results:

Print (abs (-1)> len ('duwagndanc') # abs (): returns the absolute value of the input parameter.

There are some minor issues to be aware of in comparison operations:

Different types of objects cannot be compared using ">, >=, <, <=", but can use "= ,! =.

 print(21 == len('duwangdan'))

As stated at the beginning of the article, True corresponds to 1, and False corresponds to 0. In the following example, False + True is actually equivalent to 0 + 1:

print(False + True > False + False)

2. member operators:

The keyword of the member operator is "in" to determine whether an element is in a list. After running the following program, you can get feedback.

A = 1 B = 'beotiul' album = [1, 'beotiully ', False, 7] # create a list named album print (a in album) print (B in album

When album = [], the list is empty.

If you want to add content to album, you can use the append method. The added content is displayed at the end of the list.

Album = [1, 'betifully ', False, 7] album. append ('wow') # Use the append () method to add content print (album) to album)

3. Identity operators:

The identity operator is used to compare whether two objects are the same object, while "=" in the previous comparison operator is used to compare whether the values of the two objects are equal.

Identity operators are mainly determined by "is, is not.

a = 'duwangdan'b = 'duwangdan'print(a is b)print(a is not b)

4. boolean operator:

There are three boolean operators in Python: and, or, and not.

In the following example, the returned results are: False, True, and True.

print(1 > 2 and 1 > 0)print(1 > 2 or 1 > 0)print(not False)

The main content of this article is here, and we will introduce conditional control in the future.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

The above Python entry _ Talking about logic judgment and operators is all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support to the customer's house.

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.