---
# System prepended metadata

title: AUTOMATE THE BORING STUFF WITH PYTHON

---

# AUTOMATE THE BORING STUFF WITH PYTHON

## Chapter 1

First **Install Python and open IDLE** which opens the interactive shell.

Type in ``` 2+2 ```.  This is an *expression*.  

**Expressions** can consist of *values* (2) and *operators* (+).

Python has many **operators** such as ```**, %, /, *, -. +```

### Data Types

Python can work with many data types such as **integers**, **floats** and **strings**.

**Integers** are basic whole numbers such as *-2, 0, 3, 5*.

**Floats** are numbers with a decimal point such as *-1.25, -1.0, 0.5, 1.25*.

**Strings** are characters. Surround your strings with single quotes ('). ``` 'Hello savs' ```

You can *concatenate* strings by using the **+** operator.

``` 'Stephen' + 'Mitchell' ``` would return ``` StephenMitchell ```

You **can't** concatenate a string with an integer. ``` 'Alex' + 42 ``` will display an error message.

### Variables

You can store variables with an *assignment*.

**Assignment statements** consist of the variable name ```pizza```, an equal sign ```=```, and the value to be stored ```50```. 

Once a value is stored, the variable is *initialized*.

 ```Pizza = 50``` assigns an integer to the Pizza variable name. ```Banana = 'Good'``` assigns a string to the Banana variable name.

```Dough = 50```.

 ```Pizza + Dough``` returns ```100```.  This is how you can add two variables integer values.

 #### Variable Rules

1. **Can** be only one word
2. **Can** only be letters, numbers and underscore character.
3. **Can't** begin with number.
4. They are case sensitive. ```Pizza``` and ```PIZZA``` are two different variables.
5. Camelcase is a good naming convention for variable names. ```pizzaScore```. ```bananaJamValue```.

#### Comments

Python will ignore comments and you can use write notes on a specific block/line of code or plan out how you want your code to run.

You can use the hash mark (#) to make a comment.  ```# This Pizza value will be used for the recipe function```

#### Print Function

The **print()** function displays the string value inside the paretheses.

```print('What is your favourite topping?`)```

#### Input Function

The **input()** function waits for user input.

You can use a variable to store input from the user.

```mySaucePreference = input()```

You can then print this input that was provided by the user.

```print('Your sauce preference is` + mySaucePreference)```

#### Len Function

You can pass the **len()** function a string value which evaluate the number of characters in the string to an integer value.

```len('hello')``` would return ```5```.

#### str(), int(), and float() Functions

**Str()** can be passed with an integer and evaluate a string value.

```str(29)``` would result in the integer to return a string.

You can use this in print statements where you want to add a string to an integer.

```print('Hello I am' + str(29) + 'years old.')```.  This would print **Hello I am 29 years old**.

int() and float() can take on an string value and evaluate it to a integer or float value.
# 
## Chapter 2

### Booleans

**Booleans** are data types that have two values: *True* and *False*.

*True* and *False* are **case sensitive**.

```Pizza = True```.  Booleans used in expressions and can be stored in variables.

### Comparison Operators

**Comparison operators** compare two values and evaluate to a **Boolean** value.

``` ==, !=, <, >, <=, >= ``` Are all comparison operators.

```42 == 42``` would return True. ```42 == 43``` would return False.

Remember that **=** is an assignment operator and **==** is an equal to operator.

### Blocks of Code

Lines of Python code can be grouped in *blocks*. The indentation indicates where block begins and ends.
```
if name == 'Mary':
   print('Hello Mary')
   if password == 'swordfish':
       print('Access granted.')
   else:
       print('Wrong password.')
```

The line indentation dictates the flow of the statements and how they will execute.

### if Statements

The if keyword will execute if the statement's condition is True. It is skipped if it is False.

``` 
    if name == 'Pizza Man`:
        print('Hi, Pizza Man.')
```
This statement will only execute if the name variable contains a string value 'Pizza Man'.

### else Statements

The else statement is used as alternative control for conditional statements. The *if* statement will run the *else* statement when the *if* evaluates false.

``` 
    if name == 'Pizza Man`:
        print('Hi, Pizza Man.')
    else:
        print('Hello, stranger.')
```

This will run the *else* statement if the *name* variable does not have a string value of *Pizza Man*.

### elif Statements

elif statement is used when you have a situation where you want one of many possible clauses to execute.

``` 
    if name == 'Pizza Man`:
        print('Hi, Pizza Man.')
    elif name == 'Pizza God':
        print('Hello, Pizza God.')
```

This statement will check if the variable string value of name is either **Pizza Man** or **Pizza God**.

### while Loop Statements

You can make a block of code execute over and over again until the condition of the while loop is False.

```
spam = 0
while spam < 5:
    print('Hello, world.')
    spam = spam + 1
``` 

This will run the **while** loop until the spam variable reaches a value of 6.

### break & continue tatements

#### Break

A **break** statement can be used in a while loop to break the loop regardless of the condition of the loop.

```
while True:
    print('Please type your name.')
    name = input()
    if name == 'your name':
        break
print('Thank you!')
```

This block of code will continuously loop until the name is entered as 'your name'.  Once this is entered, the loop will *break*.

#### Continue

**continue** statements are used inside loops, when a program hits the continue statement it will jump to the start of the loop.

```
while True:
    print('Please type your name.')
    name = input()
    if name == 'Keith':
        continue
    else:
        print('Thank you for not being Keith')
        break
```
This statement will keep running until the name entered is **NOT** Keith.

### for Loops & range() function

If you want to execute a block of code only a certain amount of times you can use a *for* loop.

You use the **range()** function to indicate the amount of times the loop should run.

```
print('My name is')
for i in range(5):
    print('Pizza loop (' + str(i) + ')')
```
This will return 
```
Pizza loop (0)
Pizza loop (1)
Pizza loop (2)
Pizza loop (3)
Pizza loop (4)
```

### Importing Modules

Python includes a library of grouped functions called **modules**.

To include a module in your code, use the **import** function followed by the name of the module.

```
import random
for i in range(5):
    print(random.randint(1, 10))
```

The above code imports the random module and utilizes the randint() function.

The program will output random numbers for 1-10.

### sys.exit()

If you would like to terminate a program wherever you desire, you can use the sys.exit() function.

This function can only be called if the sys module is imported.

```
import sys

if name == 'pp pupu man':
    sys.exit()
```