# Basics of Python This page contains important fundamental concepts and terminology used in Python such as syntax, operators, order or precedence and more. Although this course uses python, many of these terms are transferable to other programming languages. ## Syntax Syntax in Python works like grammar in natural languages: it defines the rules for how we must arrange words, symbols, and indentation so the code becomes a valid instruction. ### 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. ## Operators 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. In mathematics it is common to use a single letter to represent a variable, in programming descriptive variables are used to make it easier for whoever is reading the code to understand what is going on. You may remember what `x` is when writing the code, but when you revise it in the future, it may not be easy to remember what `x` represented in your 200 lines of code. In engineering it is encourage to use descriptive (such as `velocity_data`) or common convention symbols (`omega` for angular velocity). For this section arbitrary variables are used for the purpose of showing how variables works. #### 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: y = "Hello" print(type(y)) # Output: ```