## Introduction to Python Programming

Python is a general-purpose programming language used for various tasks, including web development, data science, machine learning, and more. It is a popular language for beginners because it is relatively easy to learn and has a simple syntax. Python is also a very powerful language, and many experienced programmers use it to develop complex software applications.

**Why Python?**

There are many reasons why Python is such a popular programming language. Here are just a few:

**Simple syntax:**Python has a very simple syntax that is similar to the English language. This makes it relatively easy to learn, even for beginners.**Powerful features:**Python has many powerful features, including object-oriented programming, functional programming, and dynamic typing. This makes it a very versatile language that can be used for various tasks.**Large community:**Python has a large and active community of users and developers. This means many resources are available to help you learn and use Python.

**What can you do with Python?**

Python can be used for a wide variety of tasks, including:

- Web development: Python can be used to develop web applications using frameworks such as Django and Flask.
- Data science: Python is a popular language for data science because of its powerful libraries such as NumPy, pandas, and scikit-learn.
- Machine learning: Python is also a popular language for machine learning because of its libraries, such as TensorFlow and PyTorch.
- Scripting: Python can be used to write scripts to automate tasks.
- Game development: Python can be used to develop games using libraries such as Pygame and Panda3D.

**Getting Started with Python**

To get up and running quickly, I will be using Google Colab.

Google Colab is a cloud-based Jupyter Notebook environment that requires no setup. It provides free access to computing resources, including GPUs and TPUs, which can be used to train and run machine learning models. Colab is especially well-suited for education and research, as it allows users to share their work with others easily.

Colab notebooks are interactive documents that combine code, text, and visualizations. This makes them ideal for developing and documenting machine-learning projects. Colab notebooks can also be used to teach and learn about machine learning, as they can be easily shared with others.

To use Colab, simply create a Google account and sign in to the Colab website. You can then create new notebooks or open existing ones. Colab notebooks are stored in your Google Drive account so that you can access them from anywhere.

Colab is a powerful tool for machine learning, but it can also be used for other tasks, such as data science, data analysis, and scientific computing. Colab is a great option for anyone who wants to use Python for machine learning or other computationally intensive tasks without setting up their computing environment.

The easiest way to see the code output in the book is to type it into a colab workbook and run it. I will provide a completed colab workbook at the end of the article.

## Basic Operations

### Maths

#### Addition

```
# Add two numbers
a = 10
b = 5
sum = a + b
# Print the sum
print(sum)
```

#### Subtraction

```
# Subtract two numbers
a = 10
b = 5
difference = a - b
# Print the difference
print(difference)
```

### Multiplication

```
# Multiply two numbers
a = 10
b = 5
product = a * b
# Print the product
print(product)
```

### Floor Division

```
# Get the remainder of dividing two numbers
a = 10
b = 5
remainder = a % b
# Print the remainder
print(remainder)
```

### Modulus

```
# Get the remainder of dividing two numbers
a = 10
b = 5
remainder = a % b
# Print the remainder
print(remainder)
```

### Exponentiation

```
# Raise a number to a power
a = 10
b = 2
exponent = a ** b
# Print the exponent
print(exponent)
```

## Order of Operations

The order of operations in Python is a set of rules that determines how mathematical expressions are evaluated. These rules are the same as the order of operations in mathematics.

The order of operations is as follows:

**Parentheses:**Expressions within parentheses are evaluated first.**Exponentiation:**Exponents are evaluated next.**Multiplication and division:**Multiplication and division are evaluated from left to right.**Addition and subtraction:**Addition and subtraction are evaluated from left to right.

If there are multiple operators of the same precedence, they are evaluated from left to right.

For example, the expression `2 + 3 * 4`

would be evaluated as follows:

- The multiplication operation is evaluated first, so the expression becomes
`2 + 12`

. - The addition operation is then evaluated, and the result is
`14`

.

You can use parentheses to override the order of operations. For example, the expression `(2 + 3) * 4`

would be evaluated as follows:

- The expression within the parentheses is evaluated first, so the expression becomes
`5 * 4`

. - The multiplication operation is then evaluated, and the result is
`20`

.

Understanding the order of operations is important to write correct and efficient Python code.

Here are some more examples of how the order of operations works in Python:

```
# 2 + 3 * 4 = 14
print(2 + 3 * 4)
# (2 + 3) * 4 = 20
print((2 + 3) * 4)
# 10 - 5 + 3 = 8
print(10 - 5 + 3)
# 10 - (5 + 3) = 2
print(10 - (5 + 3))
# 2 ** 3 = 8
print(2 ** 3)
# 10 // 3 = 3
print(10 // 3)
# 10 % 3 = 1
print(10 % 3)
```

The `type()`

function in Python returns the type of a variable. This can be useful for debugging or ensuring that a variable is of a certain type before operating on it.

To use the `type()`

function, simply pass the variable to the function as an argument. For example, the following code will print the type of the variable `my_variable`

:

```
my_variable = 10
print(type(my_variable))
```

#### Converting from One Type to Another Using Casting

Casting is the process of converting one type of variable to another. This can be done using the built-in Python functions `int()`

, `float()`

, and `str()`

.

- To convert a variable to an integer, use the
`int()`

function. For example, the following code will convert the string`10`

to the integer 10:

```
my_variable = "10"
my_variable = int(my_variable)
print(type(my_variable))
```

To convert a variable to a float, use the `float()`

function. For example, the following code will convert the string `10.5`

to the float 10.5:

```
my_variable = "10.5"
my_variable = float(my_variable)
print(type(my_variable))
```

To convert a variable to a string, use the `str()`

function. For example, the following code will convert the integer 10 to the string `"10"`

:

```
my_variable = 10
my_variable = str(my_variable)
print(type(my_variable))
```

It is important to note that casting can sometimes lead to unexpected results. For example, if you try to cast a string that does not contain a valid number to an integer, you will get a `ValueError`

exception.

Here are some more examples of how to use casting in Python:

```
# Convert a string to an integer
my_variable = "10"
my_variable = int(my_variable)
print(my_variable)
# Convert a string to a float
my_variable = "10.5"
my_variable = float(my_variable)
print(my_variable)
# Convert an integer to a string
my_variable = 10
my_variable = str(my_variable)
print(my_variable)
# Convert a float to a string
my_variable = 10.5
my_variable = str(my_variable)
print(my_variable)
```

The workbook for this maths workbook: Colab workbook.

## Advanced learning point

Research the topic of complex numbers and implement them in your colab notebook.

**Complex Numbers**

Complex numbers are numbers of the form `a + bi`

, where `a`

and `b`

are real numbers and `i`

is the imaginary unit, which is defined as `i = sqrt(-1)`

.

inPython## Python WorkBook 1 – Basic Math Operations

byAli Kayani