A detailed description of tuples and logical operators in Python

Source: Internet
Author: User
python tuples
A tuple is another data type, similar to a list.
The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.

#!/usr/bin/python#-*-coding:utf-8-*-tuple = (' ABCD ', 786, 2.23, ' John ', 70.2) Tinytuple = (123, ' John ') print tuple # Output full tuple print tuple[0] # The first element of the output tuple print Tuple[1:3] # outputs the second to third element of print tuple[2:] # Outputs all elements from the third start to the end of the list print Tinytuple * 2 # output tuple two times print tuple + tinytuple # printing combination of tuples

The result of the above example output:

(' ABCD ', 786, 2.23, ' John ', 70.2) ABCD (786, 2.23) (2.23, ' John ', 70.2) (123, ' John ', 123, ' John ') (' ABCD ', 786, 2.23, ' John ', 70.2, 123, ' John ')

The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:

#!/usr/bin/python#-*-coding:utf-8-*-tuple = (' ABCD ', 786, 2.23, ' John ', 70.2) list = [' ABCD ', 786, 2.23, ' John ', 7 0.2]tuple[2] = 1000 # tuples are illegal to apply list[2] = 1000 # is a legitimate application in the list

Python logical operators
The Python language supports logical operators, with the following assumption that variable A is 10 and variable B is 20:

The following example shows the operation of all the logical operators of Python:

#!/usr/bin/pythona = 10b = 20c = 0if (A and B):  print "line 1-a and B is true" else:  print "line 1-either a Is isn't true or B is not true "if (A or B):  print" line 2-either A was true or B is true or both is true "else:  P Rint "Line 2-neither A is true nor B was true" a = 0if (A and B):  print "line 3-a and B is true" else:  print " Line 3-either A isn't true or B is not true "if (A or B):  print" line 4-either A was true or B is true or both AR E true "else:  print" line 4-neither A are true nor B is true "if not (A and B):  print" line 5-either A is not tr UE or B is not true, or both is isn't true "else:  print" line 5-a and B is true "

The result of the above example output:

Line 1-a and B being Trueline 2-either A is true or B is true or both be Trueline 3-either A is isn't true or B is not Trueline 4-either A is true or "B is true" or both is Trueline 5-either "is" is "not true" or "B is not true" or both is not True
  • 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.