What will we cover? |
---|
What we can do to data. Specifically the range of arithmetic and string operators plus logical(Boolean) and list operations. |
Now that we know how to create data of different types and assign that data to variables for later retrieval, what can we do with it? How can we manipulate it? That's what operators are for. They operate on data. I'm not going to cover all the operators in Python, rather I'm just going to look at the ones that are present in virtually all programming languages.
We've already seen most of the arithmetic operators that you need in the 'Sequences' chapter, however to recap:
Operator | Description | Effect |
---|---|---|
M+N | Addition | (Mathematical) |
M-N | Subtraction | (Mathematical) |
M*N | Multiplication | (Mathematical) |
M/N | Division | (Mathematical) |
M**N | M to the power N | (Mathematical) |
M>>N | Shift M, N places right | (Bitwise) |
M< |
Shift M, N places left | (Bitwise) |
~M | Complement of M | (Bitwise) |
M & N | Bitwise AND of M,N | (Bitwise) |
M | N | Bitwise OR of M,N | (Bitwise) |
There are a number of operations that can be performed on strings. Some of these are built in to Python but many others are provided by modules that you must import (as we did with sys in the Sequences chapter).
Operator | Description | Location |
---|---|---|
S+T | Concatenation | Built in |
S*N | Repetition | Built in |
upper(S) | Uppercase S | string module |
split(S) | Separate S into 'words' | string module |
Operator | Description | Description |
---|---|---|
A and B | AND | true if A,B are both true |
A or B | OR | true if either or both of A,B are true |
A == B | Equality | true if A,B are equal |
A <> B | Inequality | true if A,B are NOT equal |
not B | Inequality | true if B is not true(B being a Boolean value or expression) |
Note: the last one operates on a single value, the others all compare two values.
Operator | Description | Location |
---|---|---|
L+J | Concatenation | Built in |
L*N | Repetition | Built in |
L[I] | Indexing | Built in |
Most programming languages have operations which they support and other languages do not. It is often these 'unique' operators that bring new programming languages into being, and certainly are important factors in determining how popular the language becomes.
For example Python supports such uncommon operations as List Slicing (Spam[X:Y]), Tuple assignment (X,Y = 12,34) and others.
Some programming languages, including Python, support operator overloading which is where the programmer has the option of defining his/her own version of built in operators for newly defined types. Some (e.g. Lisp) go further and allow you to change the existing operators' behavior for the built in types - but this is usually a very bad idea since the behaviour of the program becomes virtually impossible to understand! (Just imagine someone redefining integer addition to do a hard disk format 'on the fly'!)
Tcl takes this idea one stage further by allowing the programmer to not only define the operators but even the control structures of the language (which structures we'll look at later in the course). This is one of the very powerful features of Tcl, but again it is one which can simply confuse other programmers if used carelessly.
Points to remember |
---|
|
If you have any questions or feedback on this page
send me mail at:
alan_gauld@xoommail.com