Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
23
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

Complex or Imaginary Numbers

If you have a scientific or mathematical background you may be wondering about complex numbers? If you aren't you may not even have heard of complex numbers! Anyhow some programming languages, includinh Python, provide builtin support for the complex type while others provide a library of functions which can operate on complex numbers. And before you ask, the same applies to matrices too.

In Python a complex number is represented as:

(real+imaginaryj)

Thus a simple complex number addition looks like:

>>>M = (2+4j)

>>>N = (7+6j)

>>>print M + N (9+10j)

All of the integer operations also apply to complex numbers.

Boolean Values - True and False

Like the heading says, this type has only 2 values - either true or false. Some languages support boolean values directly, others use a convention whereby some numeric value (often 0) represents false and another (often 1 or -1) represents true.

Boolean values are sometimes known as "truth values" because they are used to test whether something is true or not. For example if you write a program to backup all the files in a directory you might backup each file then ask the operating system for the name of the next file. If there are no more files to save it will return an empty string. You can then test to see if the name is an empty string and store the result as a boolean value (true if it is empty). You'll see how we would use that result later on in the course.

Boolean (or Logical) Operators

 

 

 

 

 

Operator

 

Description

 

Effect

Example

 

 

 

 

 

 

 

 

 

 

 

A and B

 

AND

 

True if A,B are both True, False otherwise.

 

 

 

 

 

A or B

 

OR

 

True if either or both of A,B are true. False if both A

 

 

and B are false

 

 

 

 

 

 

 

 

 

A == B

 

Equality

 

True if A is equal to B

 

 

 

 

 

A != B

 

Inequality

 

True if A is NOT equal to B.

or

 

 

A <> B

 

 

 

 

 

 

 

 

 

not B

 

Negation

 

True if B is not True

 

 

 

 

 

Note: the last one operates on a single value, the others all compare two values.

25