summaryrefslogtreecommitdiff
path: root/tutorials/module_1/basics_of_python.md
blob: 7654e9286e06ba4c5e66f5c17e9e8d8f7aad1cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Basics of Python

This page contains important fundamental concepts used in Python such as syntax, operators, order or precedence and more.

## Syntax
### Indentations and blocks
In python *indentations* or the space at the start of each line, signifies a block of code. This becomes important when we start working with function and loops. We will talk more about this in the controls structures tutorial. 
### Comments
Comments can be added to your code using the hash operator (#). Any text behind the comment operator till the end of the line will be rendered as a comment.
If you have an entire block of text or code that needs to be commented out, the triple quotation marks (""") can be used. Once used all the code after it will be considered a comment until the comment is ended with the triple quotation marks.f
 
## Operators
In python, operators are special symbols or keywords that perform operations on values or variables. This section covers some of the most common operator that you will see in this course.
### Arithmetic operators
| Operator | Name           |
| ---      | ---            |
| +        | Addition       |
| -        | Subtraction    |
| *        | Multiplication |
| /        | Division       |
| %        | Modulus        |
| **       | Exponentiation |
| //       | Floor division |

### Comparison operators
Used in conditional statements such as `if` statements or `while` loops. Note that in the computer world a double equal sign (`==`) means *is equal to*, where as the single equal sign assigns the variable or defines the variable to be something. 

| Operator | Name                     |
| ---      | ---                      |
| ==       | Equal                    |
| !=       | Not equal                |
| >        | Greater than             |
| <        | Less than                |
| >=       | Greater than or equal to |
| <=       | Less than or equal to    |

### Logical operators
| Operator | Descrription                                           |
| ---      | ---                                                    |
| and      | Returns True if both statemetns are true               |
| or       | Returns True if one of the statements is true          |
| not      | Reerse the result, returns False if the result is true |

### Identity operators
| Operator | Description                                            |
| ---      | ---                                                    |
| is       | Returns True if both variables are the same object     |
| is not   | Returns True if both variables are not the same object |

## Order of Operation
Similarly to the order or precedence in mathematics, different computer languages have their own set of rules. Here is a comprehensive table of the order of operation that python follows.

| Operator                                                | Description                                           |
| ------------------------------------------------------- | ----------------------------------------------------- |
| `()`                                                    | Parentheses                                           |
| `**`                                                    | Exponentiation                                        |
| `+x` `-x` `~x`                                          | Unary plus, unary minus, and bitwise NOT              |
| `*` `/` `//` `%`                                        | Multiplication, Division, floor division, and modulus |
| `+` `-`                                                 | Addition and subtraction                              |
| `<<` `>>`                                               | Bitwise left and right shifts                         |
| &                                                       | Bitwise AND                                           |
| ^                                                       | Bitwise XOR                                           |
| \|                                                      | Bitwise OR                                            |
| `==` `!=` `>` `>=` `<` `<=` `is` `is not` `in` `not in` | Comparision, identity and membership operators        |
| `not`                                                   | logical NOT                                           |
| `and`                                                   | AND                                                   |
| `or`                                                    | OR                                                    |

## Data types
Data types are different ways a computer stores data. Other data types use fewer bits than others allowing you to better utilize your computer memory. This is important for engineers because
The most common data types that an engineer encounters in python are numeric types.
- `int` - integer
- `float` - a decimal number
- `complex` - imaginary number

The comprehensive table below show all built-in data types available in python.

| Category | Data Type                    |
| -------- | ---------------------------- |
| Text     | int, float, complex          |
| Sequance | list, tuple, range           |
| Mapping  | dict                         |
| Set      | set, frozenset               |
| Boolean  | bytes, bytearray, memoryview |
| Binary   | bytes, bytearray, memoryview |
| None     | NoneType                     |

## Variables

A **variable** in Python is a name that stores a value, allowing you to use and manipulate data efficiently.

#### Declaring and Assigning Variables

It is common in low-level computer languages to declare the datatype if the variable. In python, the datatype is set whilst you assign it. We assign values to variables using a single `=`. 

```python
x = 10          # Integer
y = 3.14        # Float
name = "Joe"    # String
is_valid = True # Boolean
```

You can assign multiple variables at once:

```python
a, b, c = 1, 2, 3
```

Similarly we can assign the same value to multiple variables:

```python
x = y = z = 100
```
##### Rules

- Must start with a letter or `_`
- Cannot start with a number
- Can only contain letters, numbers, and `_`
- Case-sensitive (`Name` and `name` are different)

#### Updating Variables

You can change a variable’s value at any time.

```python
x = 5
x = x + 10  # Now x is 15
```

Or shorthand:

```python
x += 10  # Same as x = x + 10
```

#### Variable Types & Type Checking

Use `type()` to check a variable’s type.

```python
x = 10
print(type(x))  # Output: <class 'int'>

y = "Hello"
print(type(y))  # Output: <class 'str'>
```