Python en Toc PDF
Python en Toc PDF
Python en Toc PDF
Table of Contents
I
Textbook
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Compiled and interpreted programming languages . . . . . . . . . . . . . . . . . . . . .
1.3 Basic facts about Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 Python programming in NCLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5 Downloading Python programs from NCLabs database . . . . . . . . . . . . . . . .
1.6 Launching the Python module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7 Code, output, and descriptive cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Using Python as a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Addition and subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6 Powers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7 Priority of operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8 Using empty characters makes your code more readable . . . . . . . . . . . . . . . .
2.9 Using mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.10 Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.11 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.12 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Defining new functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Passing arbitrary arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4 Returning multiple values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5 Using default arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Colors and Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 RGB colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Plotting polylines and polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4 Plotting functions of one variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5 Labels, colors, and styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.6 Scaling axes and showing grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
5
5
5
6
6
6
7
8
8
8
9
9
11
11
12
13
13
14
15
16
17
17
18
19
19
20
23
23
24
25
27
28
30
31
32
34
35
36
39
41
43
46
46
47
48
49
50
51
52
53
53
53
53
55
55
57
60
60
61
61
63
64
65
66
67
69
69
69
69
71
72
72
74
75
75
75
76
80
82
82
82
83
83
84
85
85
86
86
87
88
89
89
90
90
91
91
92
94
100
100
100
101
102
103
104
104
105
105
108
108
108
II
108
109
109
109
Programming Exercises
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 Python is fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Big word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Military exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Using Python as a Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1 Grocery shopping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Oil change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Age average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4 Saving for a bike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5 Growing town . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6 Coffee maker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7 Gardening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8 Setting tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.9 Submerged soccer ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.10 Fastest runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.11 Triangle area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.12 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.13 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.14 Greatest common divisor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.15 Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1 Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4 Sales prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5 Sales tax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6 Restaurant bill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7 Gas consumption conversion I (EU to US) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.8 Gas consumption conversion II (US to EU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.9 Distance per tank of gas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10 Circuit board price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.11 Gross pay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
113
113
113
113
113
114
114
114
114
114
114
115
115
115
115
115
116
116
116
116
116
116
117
117
117
117
117
117
118
118
118
118
118
119
119
119
119
119
119
120
120
120
120
120
120
120
121
121
121
122
122
122
122
123
123
123
123
124
124
124
124
125
125
126
126
126
126
126
127
127
128
6.10 Trapezoids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7 Conditional Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.1 Water jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2 Throwing stones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3 Missing numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.4 Trapezoids revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.5 Infinite sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.1 Spelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2 Inserting spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3 Counting chars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.4 Counting words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.5 Search and replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9 Tuples, Lists, and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.1 List reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2 String to list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10 More on Counting Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.1 Analyze string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.2 Multiplication table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.3 Approximating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.4 Prime numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.5 List maximizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.6 Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.7 Adding fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.1 Sure plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.2 Syntax check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.1 Bank account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.2 Word magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.3 Number magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.4 Vector magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13 Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13.1 Number master . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13.2 Vector master . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
III
128
129
129
129
130
130
130
130
131
131
131
131
131
131
131
131
132
132
132
132
132
132
133
133
133
133
134
134
134
134
135
135
136
136
136
Review Questions
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
142
146
148
154
157
160
163
165
171
172
173
175
Foreword
This course provides a gentle yet efficient introduction to Python a modern highlevel dynamic programming language that is widely used in business, science, and
engineering applications. If this is your first time learning a programming language,
then we recommend that you spend a few days with Karel the Robot before diving into
Python. Karel the Robot is available in NCLab (http://nclab.com) and it will teach you
what you will need most algorithmic thinking.
Algorithmic thinking is ability to translate your ideas into procedures, or sequences
of steps, that are compatible with the way a machine operates. It is the most essential skill in computer programming. Algorithmic thinking is not bound to any specific
programming language, and therefore it is a very good idea to acquire it using a very
simple language such as Karel the Robot. Once you have algorithmic thinking, your efficiency in learning any new programming language will improve dramatically. Moreover, Karels syntax is very similar to Python, so the transition from Karel to Python is
effortless.
In Python you will learn more applied concepts including mathematical operations
and functions, 2D and 3D plotting, local and global variables, strings, tuples, lists, dictionaries, exceptions, object-oriented programming, and more. A strong companion of
Python are its libraries. Besides the Standard Library that contains many built-in functions not present in lower-level languages such as Java, C, C++ or Fortran, Python also
has powerful scientific libraries including Scipy, Numpy, Matplotlib, Pylab, Sympy and
others. With these, you will be able to solve entry-level scientific and engineering problems. These libraries are used throughout the course.
Part I
Textbook
1
1.1
1.2
Introduction
Objectives
Learn the difference between compiled and interpreted programming languages.
Learn basic facts about Python and its powerful companion libraries.
Understand how Python programming works in the cloud setting.
Write and run your first Python program.
Compiled and interpreted programming languages
Depending on your objectives, this course may be all you will ever need. We hope
that you will like Python and want to learn more and there is much more to learn
out there. References to materials covering more advanced topics and object-oriented
programming are given in Section 15.
1.4
In NCLabs Python module, Python code is typed into one or more code cells. The code
is then sent as a text string to a remote server where it is interpreted, and the results are
sent back to your web browser. It does not matter if you are using a desktop computer,
laptop, netbook, tablet, or any other hardware platform. Your hardware is not used for
computing, only in a limited way for graphics rendering.
1.5
NCLabs public database can be accessed from the File managers File menu or from
the File menu of the Python module. In the Search box that appears, enter the keywords
that best match what you are looking for. For example, typing "textbook python" will
find exercises that belong to the Python textbook. Any project that you find you can
download into your account, modify, and use in any way you wish.
1.6
The Python module can be launched via the Programming icon on Desktop or via File
managers File menu. Initially, the module will contain a demo program Python program that can be run instantly by pressing the green arrow button. The demo program
is not necessarily the simplest possible Python program. Its purpose is to illustrate the
usage of the Python module and yield interesting graphics. The output of the present
demo program is the so-called Dragon fractal, as shown in Fig. 1.
After you do not need the demo program anymore, you can turn it off in Settings.
Next time you launch the Python module, it will not contain any code.
1.7
2.1
Objectives
Learn how to use Python for elementary as well as advanced math operations.
Learn how to work with mathematical functions, fractions, random numbers and
complex numbers.
Python can be used as an advanced scientific calculator no need to own a TI-89 ($150
value). As a matter of fact, NCLab is much more powerful a TI-89 hardly could compete with thousands of processors under NCLabs hood. In this textbook we will learn
how to use this computing power. Let us begin with the simplest math operations. The
rest of this section is fairly slow, so feel free to skip to Section 3 should it be too boring
you can always return to it later.
2.2
2.3
Multiplication
2.4
Division
sion, or floored division, and returns an integer. This is because like C, C++, and Fortran,
Python is strongly-typed. This means that python will avoid implicitly changing between types of data. In order to perform floating-point division, the type of division
that is more common in the sciences, at least one of the numbers must be represented
as a real number.
Adding a decimal point after the number will accomplish this. Now, the result is a
real number:
33. / 5
yields
6.6
An alternative way of turning an integer into a real number, which also works for
variables, is using the function float(). Then the above division could be done as
follows:
float(33) / 5
Output:
6.6
Once we understand the behavior, we can use it to our advantage. For example, we
can calculate how many times a dozen fits into a thousand:
1000 / 12
Output:
83
By calculating 1000 - 83 * 12 we obtain 4 which is the remainder of the integer
division. In fact the remainder can be calculated in a simpler way using the modulo
operator that will be introduced in the next paragraph.
10
In summary:
Keep in mind that division is a tricky operation. Failure to convert at least one
operand to a real number can be a source of mistakes that are very hard to find.
2.5
Modulo
The last of the common arithmetic operations is modulo (remainder after integer division). In Python modulo is represented via the percent symbol %:
6 % 4
Output:
2
Modulo can be applied to real numbers as well:
12.5 % 2.0
Output:
0.5
2.6
Powers
Output:
18.31786887167828
But we have to be careful with negative numbers:
(-3.2)**2.5
Output:
Traceback (most recent call last):
File "<nclab>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional
power
2.7
Priority of operators
Note that no other brackets such as { } and [ ] are admissible in mathematical expressions. The reason is that they have a different function in the programming language. To illustrate the priority of operations, we evaluate the following expression:
3**4 / 27 * 5 + 3 * 5
Output:
30
If we are not sure, it never hurts to use round brackets:
12
(3**4) / 27 * 5 + 3 * 5
Output:
30
2.8
Your code will be much more readable if you use empty characters on either side of
arithmetic symbols, as well as after commas. Hence, you should never write things like
sin(x+y)+f(x,y,z)*5-2.4.
Instead, the same can be written in a much more reader-friendly form as
sin(x + y) + f(x, y, z) * 5 - 2.4.
2.9
In order to calculate square roots, exponentials, sines, cosines, tangents, and many
other math functions, the best way is to import Numpy. Numpy is a powerful Python
library for numerical computations. To import it, just include the following line in your
code:
from numpy import *
Here the symbol * stands for "everything". If you wanted to import just one or two
functions, you could do that as well by just giving their names, separated by commas.
After Numpy is imported, we can calculate, for example, e2 :
exp(2)
Output:
7.3890560989306504
13
Elementary functions (and constants) that one can import from Numpy are listed below. We also show their arguments for clarity, but the functions are imported without
them. For example, the absolute value function is imported via from numpy import
abs.
pi
abs(x)
arccos(x)
arccosh(x)
arcsin(x)
arcsinh(x)
arctan(x)
arctanh(x)
arctan2(x1 , x2 )
cos(x)
cosh(x)
exp(x)
log(x)
pow(a, b)
sin(x)
sinh(x)
sqrt(x)
tan(x)
tanh(x)
absolute value of x
inverse cosine of x
inverse hyperbolic cosine of x
inverse sine of x
inverse hyperbolic sine of x
inverse tangent of x
inverse hyperbolic tangent of x
arc tangent of x1 /x2 choosing the quadrant correctly
cosine of x
hyperbolic tangent of x
ex
natural logarithm of x
ab (same as "a**b")
sine of x
hyperbolic sine of x
square root of x
tangent of x
hyperbolic tangent of x
In summary:
Python provides many readily available mathematical functions via the Numpy
library. To use them, import them via the command from numpy import *
2.10
Fractions
Python makes operation with fractions easy via the Fraction function that is imported from the fractions library:
from fractions import Fraction
A fraction such as 4/7 can be defined simply as Fraction(4, 7). Fractions can be
used with the same operations as numbers, and the result of such an operation is a
14
2.11
Random numbers
Python provides a random number generator via the random() function that can be
imported from the random library:
from random import random
15
This function returns a random real number between 0 and 1. For example,
random()
yields
0.871979925682207
Sometimes we need to generate random integers rather than real numbers. This is easy.
For illustration, a random integer n between 1 and 3 can be generated via the code
a = random()
n = int(3*a + 1)
Here the function int() will erase the decimal part of the real number, converting it
to an integer.
2.12
Complex numbers
Complex numbers are always represented as two floating point numbers, the real and
imaginary part. Appending j or J to a real number makes it imaginary:
1j * 1J
Output:
(-1+0j)
This is one way to define complex numbers:
1 + 3j
Output:
(1+3j)
Another way is to use the command complex:
16
complex(1, 3)
Output:
(1+3j)
All arithmetic operations that are used for real numbers can be used for complex numbers as well, for example:
(1 + 2j) / (1 + 1j)
Output:
(1.5+0.5j)
To extract the real and imaginary parts of a complex number z, use z.real and
z.imag. Use abs() to get the absolute value:
a = 3 + 4j
a.real
a.imag
abs(a)
Output:
3
4
5
3
3.1
Functions
Objectives
17