Explanation of the tuples and logical operators in Python

Source: Internet
Author: User
This article mainly introduces the usage of tuples and logical operators in Python. It is the basic knowledge in getting started with Python. For more information, see Python tuples
Tuples are another data type, similar to List ).
The tuples are identified. Internal elements are separated by commas. However, an element cannot be assigned a value twice, 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 # print tuple [0] # print tuple [] # print tuple [] # print tuple [2:] # print tinytuple * 2, all elements from the third end to the end of the list # print tuple + tinytuple twice output tuples # print the combination of tuples

Output result of the above instance:

('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 is invalid because the tuples cannot be updated. The list can be updated:

#! /Usr/bin/python #-*-coding: UTF-8-*-tuple = ('abcd', 786, 2.23, 'john', 70.2) list = ['abcd ', 786, 2.23, 'john', 70.2] tuple [2] = 1000 # list of illegal applications in the tuples [2] = 1000 # list of valid applications

Python logical operators
Python supports logical operators. Assume that variable a is 10 and variable B is 20:

The following example demonstrates the operations of all Python logical operators:

#!/usr/bin/pythona = 10b = 20c = 0if ( a and b ):  print "Line 1 - a and b are true"else:  print "Line 1 - Either a is not true or b is not true"if ( a or b ):  print "Line 2 - Either a is true or b is true or both are true"else:  print "Line 2 - Neither a is true nor b is true"a = 0if ( a and b ):  print "Line 3 - a and b are true"else:  print "Line 3 - Either a is not true or b is not true"if ( a or b ):  print "Line 4 - Either a is true or b is true or both are true"else:  print "Line 4 - Neither a is true nor b is true"if not( a and b ):  print "Line 5 - Either a is not true or b is not true or both are not true"else:  print "Line 5 - a and b are true"

Output result of the above instance:

Line 1 - a and b are trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is not true or both are 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.