---
# System prepended metadata

title: Start point with Python
tags: [IB, Computer Science, python]

---

# Start point with python

This is a note for the IB students with the new syllabus

You will find how to start working with python. 


## What is Python 

Python is a
 * high level (of abstraction) programming language
 * that has a specific syntax different from others (like C++ that we have seen with Arduino)
 * that is interpreted line by line

Nowadays is very common since has integrated a lot of abstractions and the syntax is more less easy (not trivial though)

## Syntax in Python

### Identation 

Is very important

### Comments 

```python=
"""
afadfaf
asdfpoqher
gasdg
ioasg
"""
print("Hello, World!")
print("this something")
x = 10.0 #float 
print(x)
```

## Types of variables (or lack of them)

(page 348 of the pdf)

Name of `primitive`

* Booleans (bool)
* Characters (char, not supported in python)
* Strings (str)
* Integers (int)
* Decimals (float)

## If statemens 

if, else, elif 

#TO-DO

```python=
isWeekend = True  
isThereASchoolTrip = True

print("wake up")

if isWeekend:
 print("sleep more")
      print("sleep even more")
elif isThereASchoolTrip:
  print("prepare backpack")
else:
  print("go to school")
print("have a good day")
```



Reference (With more information needed than for the exercise)
https://www.w3schools.com/python/python_conditions.asp

#### Exercise: From flowchart to code

If you have done your daily routine and your recipe in a flowchart you can do a python version of it. In the end you will have a longer version of this snippet:

```python=
isWeekend = True  
isThereASchoolTrip = True

print("wake up")

if isWeekend:
 print("sleep more")
      print("sleep even more")
elif isThereASchoolTrip:
  print("prepare backpack")
else:
  print("go to school")
print("have a good day")
```



For that you will need to plan ahead. 

First we will declare all the booleans that we need. We set them as `True`or `False` directly in the code. 

So we are going to read our diagram and check all the decisions that we have. 

For each one of them we're going to write a variable that makes sense in that context. 

For example if we have a decision that if its a weekend, the variable can be named `isWeekend`, if the decision is if it's late or not, the variable can be named `isLate` or `late` so when we write the if we can write `if isLate:` or `if late:`

Once we have written all the variables that we need and their values we will start our program, we will write the instructions following the flowchart. If we find a decision then we will need to use an if statement that will check one of the variables that we have defined beforehand.  Depending on where the flowchart goes maybe we need and else statement or maybe not. 

:::info
☝️🤓 Nerdy extra

This exercise of putting these variables values directly in the code is _hardcoding_ because if we want to change these values we need to change the code. Usually we're going to find another way to input them (either via an input or a form or a file) but for now is more than sufficient. 
:::

Examples

:::spoiler
![Sin título](https://hackmd.io/_uploads/SyLVGHlhlx.png)
by A. T.

![imagen](https://hackmd.io/_uploads/Hk74NBengx.png)
by V. K.
:::

:::info
**About optimizing a bit for better mainteinance**

Consider this algorithm abstracted from a student:

```python=
isWeekend = True
if isWeekend:
    print("Wake up late")
    print("shower")
    print("brush teeth")
    print("put clothes")
    print("have breakfast")
    print("go running")
else:
    print("Wake up early")
    print("shower")
    print("brush teeth")
    print("put clothes")
    print("go to school")
```

See that lines 4-6 and lines 11-13 they... do the same. When we talked about functional programming we discussed the issues of repeating ourselves (There is a principle called "DRY" that is "Don't Repeat Yourself"). We can do several ways to solve that. We can either use a function or also we can use more than one if. Consider this version:

```python=
isWeekend = True
if isWeekend:
    print("Wake up late")
else:
    print("Wake up early")
print("shower")
print("brush teeth")
print("put clothes")
if isWeekend:
    print("have breakfast")
    print("go running")
else:
    print("go to school")
```

Other option is to have a function (regularRoutine). 

```python=
isWeekend = True
def regularRoutine():
    print("shower")
    print("brush teeth")
    print("put clothes")
    
if isWeekend:
    print("Wake up late")
    regularRoutine()
    print("have breakfast")
    print("go running")
else:
    print("Wake up early")
    regularRoutine()
    print("go to school")
```

:::

## How to install/use Python

### Python in command line. 

You can install python going to python.org and download and install

![image](https://hackmd.io/_uploads/SyZuNCyngg.png)
This will give you access to python in your... command line. (or terminal using Mac or linux)

If you open the command line (in windows is windows and write cmd) you can write "python" and you will have access through the console to python

![image](https://hackmd.io/_uploads/rkoRNAk3xx.png)

The three simbols `>>>` is that is waiting for the next line of **code** to be input 

Here you can see a simple example 

![image](https://hackmd.io/_uploads/BysQB01heg.png)


### Using an IDE to work a bit faster and have the tools already there

#### Pycharm

I've heard of pycharm but I've never used it

![image](https://hackmd.io/_uploads/r1k9BRknlg.png)
(don't use the SUPERENHANCED AI solutions because it's a mess you know, and your programs are already simple enough)

You can download it here https://www.jetbrains.com/pycharm/

#### VSCode

Another very common IDE is vscode 

https://code.visualstudio.com/

![image](https://hackmd.io/_uploads/r1zkvCk2ll.png)
As you can see you can have several files here and press "play" to execute them and you will see the terminal bellow. 

Nevertheless you will need to go to extensions in order to install python and/or other extensions that you may need. 

#### Jupyter notebook 

The advandage of this is that you have the documentation (kind of the comments) with the code in the same place. It's cool but it takes a bit to understand how to use with markdown (markdown is the way I'm writing this note that you may have seen here)

https://jupyter.org/

![image](https://hackmd.io/_uploads/Hkl1_Ckngl.png)


### Using it in a browser 

There are interpreters that work from the browser. For some contexts and small snippets we can use these so we don't have to install anything. 


:::warning
Problems that they may have: they don't have input and only some of them have access to work with several files. 

**the instruction `input()`** won't work properly

:::

Also other libraries may not be available. 

Options 

#### One compiler 

Supports several files!
https://onecompiler.com/python


#### W3Schools tryit editor

https://www.w3schools.com/python/trypython.asp?filename=demo_default


## Python in your calculators

In the graphical calculators that are common in IB for math there is also integrated micropython 1.9.4.  There are some differences with regular python but for the purpose of starting learning python is mostly the same. 



https://docs.micropython.org/en/v1.9.4/pyboard/genrst/builtin_types.html





