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