Skip to content

Commit 55963ef

Browse files
committed
Add first chapters
1 parent e1335bf commit 55963ef

File tree

18 files changed

+697
-2
lines changed

18 files changed

+697
-2
lines changed

.vscode/settings.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"files.trimTrailingWhitespace": false
3-
}
2+
"files.trimTrailingWhitespace": false,
3+
"markdownlint.config": {
4+
"MD026": {
5+
"punctuation": ".,;:。,;:"
6+
}
7+
}
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# What is Python?
2+
3+
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
4+
5+
## Why Python?
6+
7+
This language has a fairly natural syntax, which means that we actually write commands in a language similar to English, and Python does it, without overloading the syntax with parentheses and punctuation marks. In addition, Python has a large amount of functionality by default and many additional libraries available on the web, which gives a wide range of tools already available in the language itself without having to implement them on your own. Regardless of the wealth of ready-made Python functions, in this course you will probably write many programs that already exist in Python.
8+
9+
Python is available to everyone for free — you can get it from <https://python.org>. I suggest that you read the contents of this page, but I do not recommend installing Python directly from it. Detailed instructions can be found later in this lecture.
10+
11+
## Who is using it?
12+
13+
Google, Yahoo, Nokia, IBM and NASA use Python in their multi-million or billion dollar applications and projects. Both Microsoft and Apple offer full support for Python in their operating systems and development platforms. Many websites such as YouTube and Grono are written in Python.
14+
15+
**NASA** has been using Python for many years. One of the well-described implementations is using Python applications to [manage shuttle launch controls](http://www.python.org/about/success/usa/). Also, the recently released [Nebula](http://nebula.nasa.gov/) project is a distributed computing environment adapted to "cloud" computing, combining many Python modules and applications with other languages and technologies.
16+
17+
## Python in engineering applications
18+
19+
The simplicity of Python and the possibilities it offers have made it an increasingly used tool for engineering and scientific calculations. However, despite the fact that Python offers a wide variety of functionalities by default, its full use in these specific areas requires the use of some additional libraries. These are:
20+
21+
* [numpy](https://numpy.org/) — a library that adds an efficient type of multidimensional number arrays and is the basis on which all subsequent libraries are based,
22+
* [scipy](http://www.scipy.org/) — tools for advanced scientific calculations: statistical calculations, numerical integration, searching for extremes and null places of functions, interpolation, etc.,
23+
* [matplotlib](https://matplotlib.org/) — advanced library that allows you to create graphs ([examples](https://matplotlib.org/gallery/index.html)),
24+
* [pandas](https://pandas.pydata.org/) — a fast, powerful, flexible and easy to use open source data analysis and manipulation tool.
25+
26+
27+
<hr />
28+
29+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# Constants and their types
2+
3+
## Constants
4+
5+
Constant values such as numbers, letters, and strings (strings) are called "constants" because their value does not change. In Python, constants can be of the following types:
6+
7+
### Numeric types
8+
9+
* integer numbers: `1`, `100`;
10+
* integers in non-decimal notation systems beginning with the prefix `0b` (binary notation), `0x` (hexadecimal notation), `0o` (octal notation): `0b101` (5), `0x1a` (26), `0o12` (10);
11+
* real numbers (the decimal part is separated by a dot and no digits before or after the dot means 0): `2.27`, `3.14`, `-1.2`, `1.0`, `2.` (2.0), `.5` (0.5);
12+
* real numbers written in exponential (base 10) notation <em style="color: #0000ff;">mantissa</em>e<em style="color: #ff00ff;">exponent</em>, oznaczające <em style="color: #0000ff;">mantissa</em> × 10<sup><em style="color: #ff00ff;">exponent</em></sup>: `-5e3` (-5000), `2.5e-2`</span></tt>` (0.025);
13+
* complex numbers in which we write both components as real numbers, and denote the imaginary part by adding `j` at the end: `1.5-0.2j`, `-2+1e-1j` (-2+0.1j);
14+
15+
### Text types
16+
17+
Strings between one of the following sets of quotation marks:
18+
19+
* single quotation marks: `'...'`,
20+
* double quotation marks: `"..."`,
21+
* single or double quotation marks repeated three times: `'''...'''`, or `"""..."""`.
22+
23+
In the latter case, the text may span several lines. In the first two it is not allowed, however newlines can be inserted using the special symbol `\n`.
24+
25+
You can use any set of the above symbols to denote text strings, but it is very important that each string begins as it ends. The valid examples are (red indicates the beginning and ending quotation marks):
26+
27+
```python
28+
"Ala ma kota"
29+
30+
'This chapter is named "Constants"'
31+
32+
"""First line
33+
Second line"""
34+
35+
"Here are\ntwo lines too"
36+
```
37+
38+
### Tuples
39+
40+
Tuples are an ordered set of values constant. Successive values ​​are separated by commas, and the whole tuples should be enclosed in parentheses. More about using tuples will follows in this course later. Examples of tuples are:
41+
42+
```python
43+
(1, 2, 5)
44+
45+
(2.5, 3, 1j, 'tekst') # tuples' elements don't have to be of the same type
46+
47+
("tekst", (1, 2)) # tuples can contain other tuples (and other types, with which we haven't mentioned yet)
48+
49+
(1,) # a one-element tuple must have an extra comma (to distinguish it from the number in parentheses)
50+
51+
() # empty tuple
52+
```
53+
54+
More on the use of tuples and how to extract their individual elements will be presented later in this lecture.
55+
56+
## Operations on values
57+
58+
You can act on the values supported by Python using mathematical operators. The basic operators are `+`, `-`, `*` (multiplication), `/` (division), `**` (power), `//` (integer division), `%` (division remainder). Python can perform operations according to the applicable mathematical rules (the use of parentheses is allowed). To read them, start the Spyder program and enter the following expressions in the interactive console (it is not necessary to use the `print` function - the interactive shell will automatically print the result of the calculated expression):
59+
60+
```python
61+
1 + 2
62+
63+
2 + 2 * 2
64+
65+
(2 * (1 + 1))**2
66+
67+
2.5 * 2
68+
69+
7.5 / 3
70+
71+
5 / 2
72+
73+
4 / 2
74+
75+
8 // 3
76+
77+
8 % 3
78+
79+
2**10
80+
81+
8**(1/3)
82+
83+
(-1)**0.5
84+
85+
'1' + '2'
86+
87+
(1, 2) + (3,) # notice comma in (3,)
88+
89+
3 * "a"
90+
91+
(1, 2) * 2
92+
```
93+
94+
I also suggest trying this interactive Python shell yourself as a calculator.
95+
96+
A few of the above examples should be discussed in detail. The division operator (`/`) on real or integer numbers will always return the real result (even if the result is an integer). In turn, the integer division operator (`//`) will remove the fractional part - for this reason, it has limited use in engineering and scientific applications.
97+
98+
The exponentiation operator (`**`) can also raise to a non-integer power. Therefore `(-1)**0.5` is treated as the principal root of `-1`, i.e. the imaginary number `1j`. It is shown as `(0 + 1j)`, or something like `(6.123233995736766e-17+1j)`. Do not be afraid of this: according to the previously described exponential notation, the real part of this number is in the order of 10<sup>-17</sup>, which is practically 0. Floating point computer calculations are not 100% accurate, hence the result (accuracy of calculations is a separate issue on which you need to pay attention to when writing calculation programs, but at this stage we will not deal with it).
99+
100+
It is a good practice to write spaces around the mathematical operators, as it significantly increases the readability of the code (of course, this is not a formal requirement, but omitting spaces frequently makes it very difficult to analyze the program, especially when it is written by someone else).
101+
102+
Python can also work on non-numeric types as long as they make sense. Adding text strings or tuples together combines them, and multiplication by a whole number repeats their contents a given number of times.
103+
104+
For actions that don't make sense, Python will throw an error. Please try the following in the console and read and understand the error message each time:
105+
106+
```python
107+
1 / 0
108+
109+
1 + "2"
110+
111+
2.5 * (1, 2)
112+
113+
"Alala" - "la"
114+
115+
50.0 ** 1000000
116+
```
117+
118+
The last operation is mathematically and logically correct, however, it results in too large number that cannot be correctly stored in the computer's memory as a real number.
119+
120+
## Converting types
121+
122+
All the constants of a specific type described above (as well as other types, not yet shown) have names. For the types already described, they are as follows:
123+
124+
* integer numbers: `int`
125+
* real numbers: `float`
126+
* complex numbers: `complex`
127+
* text strings: `str`
128+
* tuples: `tuple`
129+
130+
131+
For any value you can check its type using the type function, for example:
132+
133+
```python
134+
type(2)
135+
136+
type(2.5)
137+
138+
type(2j)
139+
140+
type("2")
141+
142+
type(1 + 2)
143+
144+
type(1 + 2.0)
145+
```
146+
147+
As you can see above, Python is able to convert numeric types by itself so that the result of a given action makes sense. However, it is possible to force type conversions manually by directly using their names as functions. This is especially useful when you want to convert a string to a number. For example:
148+
149+
```python
150+
float("2.5")
151+
```
152+
153+
Please see what is the difference between:
154+
155+
```python
156+
"1" + "2"
157+
```
158+
159+
and
160+
161+
```python
162+
int("1") + int("2")
163+
```
164+
165+
and
166+
167+
```python
168+
str(1) + str(2)
169+
```
170+
171+
If the conversion doesn't make sense, Python will throw an error. Please try
172+
173+
```python
174+
int(10+2j)
175+
176+
float("dwa")
177+
178+
float("two and half")
179+
180+
int("one")
181+
182+
int("4O") # can you notice a problem here?
183+
```
184+
185+
The exception is the conversion of a real number to an integer. The expression
186+
187+
```
188+
int(2.7)
189+
```
190+
191+
will simply discard the fractional part.
192+
193+
194+
<hr />
195+
196+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Other types and logical operations
2+
3+
## None Type
4+
5+
In addition to those mentioned above in Python there is a special constant `None` which means nothing. Its meaning will be discussed later. It is generally used when we want to emphasize that we have no meaningful value at our disposal.
6+
7+
## Logical types and operators
8+
9+
The logical type is called `bool`. There are only two values of this type: `True` and `False`. They usually appear as a result of the operation of logical operators:
10+
11+
* `==` checks if two values are equal (**Note! this is <span style="color:red">double</span> `=`**)
12+
* `!=` checks if two values are different
13+
* `>`, `<`, `>=`, `<=` are operators that compare two elements
14+
15+
Please check the results of the following operations in the console:
16+
17+
```python
18+
1 + 2 == 3
19+
20+
1 - 2 != -1
21+
22+
2 + 2 > 4
23+
24+
2 + 2 >= 4
25+
26+
(3, 2) == (1 + 2, 1 * 2)
27+
28+
(1, 2) > (1, 3)
29+
30+
(1, 2) < (1,)
31+
32+
(1, 2) > (2,)
33+
34+
"Ala" < "Basia"
35+
36+
"a" < "Z"
37+
```
38+
39+
40+
As you can see, it is not only possible to compare numbers, but also tuples and text strings. In the first case, the elements are compared sequentially, starting with the first one. In the second, the comparison is alphabetical, with all uppercase letters being smaller than lowercase. This is due to the ways in which characters are represented in the computer's memory. A detailed discussion of how you can compare text strings without distinguishing between uppercase and lowercase letters is described later in the lecture.
41+
42+
There are also special logical operators for logical values: negation (`not`), conjunction (`and`), alternative (`or`). Please check their operation yourself. For example:
43+
44+
```python
45+
True and False or True
46+
47+
False and not True
48+
```
49+
50+
etc... These operators will be very important when the conditions are discussed.
51+
52+
53+
<hr />
54+
55+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.

0 commit comments

Comments
 (0)