Friday, May 22, 2015

Python: The use of relation operators



Abstract: Kills how to use relation operators


1. The use of plus operator “+”
The common use is for plus.
>>> a=1
>>> b=4
>>> print a+b
5
Combine multiple strings into a string
>>> c='da'
>>> print a+c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print str(a)+c
1da
>>> d='32a'
>>> print c+d
da32a
Combine multiple lists
>>> a=range(10)
>>> b=range(10,20)
>>> print a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print b
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> print a+b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]


2. Boolean expression
Here is a common statements for Python or some other programming language.
>>> a=10
>>> if a>0 and a<100:
... print a
...
10
You can write it with a simple pattern:
>>> if 0< a<100:
... print a
...
10







writing date: 20150522

No comments:

Post a Comment