# Part 2 - Learn Text Based Coding In Python

{% hint style="success" %}
If you would like more detailed information about python programming we recommend [w3 school's python tutorial](https://www.w3schools.com/python/) to learn more & [w3 school's python exercises](https://www.w3schools.com/python/exercise.asp) to test your knowledge.
{% endhint %}

## Introduction

Hello, welcome to this guide where we will be learning Python.

Before we start you will need python please follow our instructions to get python

{% content-ref url="p1-python-setup" %}
[p1-python-setup](https://thesaintsftc.gitbook.io/thesaintsftc-docs/learn-programming/p1-python-setup)
{% endcontent-ref %}

{% hint style="info" %}
We recommend using replit.com to follow this course as it is quick and easy to get started!
{% endhint %}

### Make a Hello World program

Type `print("Hello, World!")`

What does it do?\\

## Variables

{% embed url="<http://www.youtube.com/watch?v=ghCbURMWBD8>" %}

This video talks about variables and how they work

### What are variables?

You can think of variables as labeled containers that store a value.

When defining a variable in python you first have to give the variable a **name** then use the **equal sign** which is used to assign **values** to the variable.

{% hint style="info" %}
In python when you define a variable you are also assigning the variable a **Data Type** as the value you used has a Data Type.
{% endhint %}

So in a nutshell to create a variable you need to give your variable a **Name**, **Value,** and Data Type(In python the Data Type of your variable is found automatically)

#### Example:

```python
greeting = "Hello"
```

## Data Types

In python there are 4 main Data Types:

### String

The String Data Type which is used to store text, to tell the computer that something is a string you just have to surround it with quotes, both `' '` and `" "` work, anything you surround by quotes is a string so numbers surrounded by quotes are text, not a number.

### Integer

An Integer is a whole number meaning no decimal point

### Float

A Float is a decimal number meaning it always has a decimal point even if it ends with `.0`

### Boolean

A Boolean is just True or False, in python you have to write True and False starting with a capital letter otherwise it doesn't know what it is.

![](https://1769714528-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MUO22awSZ9DY17nh-IP%2F-MjbRWZSZEcuxts3HcVw%2F-MjbVf7fur6H01FTg1bP%2FPython%20Data%20Types%20Table.svg?alt=media\&token=4daf82d0-8801-454d-9c02-b8462bcb3ae0)

#### Example:

```python
myString1 = "Apple"
myString2 = 'Banana'
myInt = 10
myFloat = 1.5
myBool = False
```

## Operators

### Mathematical Operators

| Operator | Name                 | What it does                                                                                        |
| -------- | -------------------- | --------------------------------------------------------------------------------------------------- |
| +        | Add                  | This operator is used to add to numbers together, it can also be used to join (Concatenate) Strings |
| -        | Subtract/Concatenate | This operator is used to take one number from another                                               |
| \*       | Multiply             | This operator is used to multiply one number by another                                             |
| /        | Divide               | This operator is used to divide one number by another                                               |
| %        | Remainder of         | This operator is used to divide one number by another and output the remainder                      |
| \*\*     | Power of             | This operator gets the power of one number by the other                                             |

### Assignment Operator

The assignment Operator in python is a single **equal sign**, it is used to tell the computer that one thing is **equal** to another.

### Comparison Operators

| Operator | Name                          | What it does |
| -------- | ----------------------------- | ------------ |
| ==       | **Is equal to?**              |              |
| >        | **Is greater than?**          |              |
| <        | **Is less than?**             |              |
| >=       | **Is greater or equal to?**   |              |
| <=       | **Is less than or equal to?** |              |
| !=       | **Is not equal to?**          |              |

## If/Else Statements

To write an if statement you first need to use the keyword **`if`** followed by the **condition** you want to check then you end the line with a **`:`** then on the next line which should be indented you type the code you want to run if the condition is True, you can then use the keyword **`elif`** to check another condition if the first condition is not True and then you can use **`else`** when none of the conditions are True.

##

{% embed url="<https://youtu.be/HQ3dCWjfRZ4>" %}

## Lists

In Python, you can store a list inside of a variable

To create a list you will first need a variable to store it in and to tell the computer that you are creating a list you have to surround it with `[]` then you can type some values in your list each one separated by a comma.

#### Example:

```python
var = ["Apple", 15, False, 'Banana', 1.5]
```

## Loops

### While Loops (Conditional Loops)

A `while` loop repeats as long as the **condition** is `True`

To create a while loop you first write the `while` keyword followed by a **condition** and then a **`:`** and on the next line, you will write the code you wish to repeat.

### For Loops (Iterative Loops)

text

Example

```python
fruit = ["Apple", "Banana", "Pear"]
for i in fruit:
  print(i)
```

Output:

`Apple`\
`Banana`\
`Pear`

text

## &#x20;
