# Python Intro
## Club Resources
* [Practice Problems](https://ctf.tjcsec.club)
* [Codespaces Desktop](https://github.com/TJCSec/desktop)
## Getting Started
### What is Python?
Python is a coding language. In fact, it's one of the most versatile, beginner-friendly, and intuitive programming languages out there. Compared to other coding languages, Python has some of the most basic syntax, and the best way to learn this syntax is by writing your own code! Python has a lot of uses, from data analysis to machine learning to web development, which means it's a pretty important lanuage that you should learn.
### How do I get install Python?
If you have a school Chromebook, you have two main options: using TJ's JupyterHub (which is available on Ion) or using an online Python compiler. Neither option here is great, so your real solution is to switch to a FCPS Dell or bring a personal computer (if possible). If you're taking any CS class this year, you should definitely consider switching out of a Chromebook anyway since you can't download any applications with the Chromebooks.
If you have a personal laptop or an FCPS Dell, getting started with Python is easy! Your first step is to download [Python](https://www.python.org/downloads/) here. Once you've downloaded the appropriate version of Python for your machine, you can pick a code editor to use. The most popular code editors for Python are Visual Studio Code (my favorite), PyCharm, IDLE (the default editor), Jupyter Notebook, and jGrasp (at TJ).
### Downloading Files
No, you don't have to manually download the files from the CTF page and sort the file into the right folder each time you want to download a file from the CTF page. The easiest way to download files for CSC purposes is to right click on the file, click "copy link address", and paste it into your Linux terminal with `wget [URL]`. This way, you have an executable file nicely in your code environment with minimal hassle.
## Python Basics
### How can I use Python?
You can run Python either by writing code in a `.py` file (a Python file) and running the file, or by directly executing it in a terminal using the `python` keyword (`python3` on Mac/Linux). For today's challenges, you'll be using the former.
In order to run a Python file, you should create a new file in your code editor and save it with a `.py` extension (for example, `myCode.py`). Write your Python code in the file, then open your terminal in the file's directory and run `python [filename].py` (`python3` on Mac/Linux). The output of your program will be displayed in the terminal.
### Syntax
#### Variables
Variables in Python (and any coding language) are labels for storing data. You can have different types of variables, from Strings (sequences of characters) to integers, floats (decimal numbers), booleans (True/False), and more. For example,
```sh
firstName = "Alice"
```
gives us a String variable with the name of "firstName" and the value of "Alice".
#### Math
Python offers a variety of mathematical operations and possibilities. From the basic arithmetic operations, you have:
| Operation | Syntax |
| -------- | -------- |
| Addition | + |
| Subtraction | - |
| Multiplication | * |
| Division | / |
| Integer Division | // |
| Modulo (remainder) | % |
| Exponentiation | ** |
These are all fairly self-explanatory. Division returns a float, while Integer Division returns an integer (the float rounded down to the nearest integer). In addition to the basic built-in Python math operations, you can also use built-in functions, like `min()`, `max()`, `abs()`, `pow()`, and more. The Python `math` module is also available for more complex mathematical functions, like `math.sqrt()` or trig functions, as well as math constants like pi or e.
#### Conditionals
Conditional statements in Python allow for more control over your program, based on conditionals (whether something evaluates to true or false). The main keywords you'll need to know are **if**, **elif**, and **else**, which all use semicolons after the statement.
**If** statements execute a block of code if the given condition evaluates to True.
```sh
x = 5
if x > 1:
print("x > 1")
```
**Elif** statements are used when you're stacking if statements, so you can check multiple conditions at the same time more efficiently. If the **if** statement is false, the **elif** condition is evaluated. Otherwise, the **if** statement code block is executed and the **elif/else** code is skipped.
**Else** statements are only executed if the **if** and **elif** statements are evaluated to be false and takes no condition.
```sh
x = 100
if x > 80: print("x > 80")
elif x > 50: print("x > 50 >= 80")
else: print("x <= 50")
```
#### Loops
Loops in code are used to execute a block of code multiple times. There are two types of loops in Python: **for** loops and **while** loops.
**For** loops are used to iterate over an iterable sequence (like a string, list, dictionary, range, etc.). The code is executed once per item in the sequence. For example,
```sh
myList = ["apple", "banana", "orange"]
for fruit in myList:
print(fruit)
#Output:
# apple
# banana
# orange
```
In this scenario, `fruit` is the iterator, which is iterating over the iterable (`myList`).
**While** loops repeatedly execute a block of code as long as the given condition is true. The condition is evaluated once before each iteration.
```sh
counter = 0
while counter<10:
print(counter)
counter+=1
```
The above block of code would print the numbers 0-9.
In Python, you can also have loop control statements (`break`, `continue`, etc.) and nested loops (think Russian dolls; loops inside loops).