###### tags: `Lesson Notes` `Python` `ACC`
# Intro to Python
## System Set up
### Python installation
#### Step 1 - Check to see if you already have it
Is Python already on your system? Check by using the following commands (try each one just to be sure):
1. python --version
2. python3 --version
3. py --version
On some computers with python 3 installed you will get a version back for both 1 and 2. Some computers will only give a version using 1 of the 3. One of my computers only responds to py, however most will print a version number for python or python3. So if you see a version number then procede to step 3
#### Step 2 - Download Python
If it isn't installed then lets get it on there... your gonna want it
* Windows Users follow this link [Windows Download](https://www.python.org/downloads/windows/)
* Mac Users follow this link [Mac Download](https://www.python.org/downloads/mac-osx/)
Now once that is installed your going to want to check which way your computer likes to access python so repeat step 1. If 1 and 2 work you will just use 2 most of the time.
### VS Code Extension
#### Step 3 - Adding a python extension to VS Code
Although not required it can help for you to have a python extension for VS Code. We recommend the following:
* Python (also seen as ms-python.python)
Having this installed can help you a lot.
## Folders & Virtual Environment
Alrighty then our systems are now set up and ready to go....wait nope no coding yet...still got more to do 1st
#### Step 4 - Get those folders set up
Yup the ol get the folders ready thing again. Trust me it's important. Here is a good recommended structure for you folders (if they have a ^ at the end they are git folders and assignments otherwise they should just be plain old folders)
* python_stack
* my_environments
* _python
* fundamentals
* forLoopBasicI ^
* functionsBasicI ^
* pythonOOP
* django
And so on. Now that that is done (and you can always add more folders later) lets move on
#### Step 5 - Creating a Virtual Environment
So to get us started open up those terminals and cd into the my_environments folder.
Now lets create a new environment
Wait wait wait let me explain what these are first. So no developer has just 1 project on their computer. In fact you all already have more than one as of now. So we know this is a fact. Well not every project is going to need the same things. Just like our 1st and 2nd projects 1 required and html and css doc where the 2nd one also required a js doc. Different needs for different sites.
Well the same holds true with python based projects. The biggest difference is that now we will be using packages or dependencies. Kinda like those bootstrap links how you used something from that link to style your page well we will use the packages we will install for the same thing. Why reinvent the wheel if it's already working.
Thats where Virtual Environments come in. We create an environment that is unique to certain projects. Instead of installing these packages on the whole computer we just put them in a special folder and "link up" to it in our python files.
Ok now we know the why lets get to the how
* Mac Users:
* use the following command to create an environment for Django
* python3 -m venv py3Django
* Windows Users:
* use the following command to create an environment for Django
* python -m ven py3Django
:::info
Make sure to use the version of python/python3/py that gave you the python 3 version in step 1
:::
#### Step 6 Activating and Deactivating the environment
Wait I have to activate something? Up got to turn it on to use those packages.
While working on a project your going to want to make sure the coresponding environment is active...when you are done for the day or time period, shut er down.
##### Activating:
* Mac Users:
* source py3Django/bin/activate
* Windows Users:
* If using Command Prompt:
* call py3Django\Scripts\activate
* If using git bash:
* source py3Django/Scripts/activate
:::info
the py3Django part will change depending on the environment you want to activate so if you named is something else just that part will change everything after the / remains the same
:::
##### Deactivating:
Now are you ready for this one?
* All Users:
* deactivate
Yup thats it.
Now your wondering how do I know if my environment is actually active or not? Well you should see (py3Django) in your terminal before your path or $. If you don't see that anywhere in your terminal you might not have activated it.
#### Step 7 - Checking and updating packages:
So we got this environment thing created, we have turned it on and then off again but that can't be it right? Well my friend you are right. It's not. Now turn that bad boy back on again and lets check to see what packages are installed by typing the following into the terminal (while environment is active):
* pip list
You should see something pop up. In most cases if you are just following these steps for the first time you will see 2 packages listed and their current version next to them. They should be pip and setuptools.
Do you see them? Got any warnings? Cause you might. If you get any type of waring like you are using an older version simply type the following:
pip install --upgrade pip (of course if thats the one that needs updating if it is another one replace the last pip to what that packages name is)
Done? Ok good now type that same command (pip list) again. No warnings? Sweet. On to the next step
#### Step 8 - Installing your 1st package:
So as of now you hav einstalled python to your computer, you have created a good solid folder structure, created your first environment, turned it on and off and back on again, and you have checked and possibly updated the current packages. Now we want to add to that pip list
So type the follwoing:
* pip install Django
This will install Django and it's required extra packages into this environment folder. Is is done yet? Good go ahead and pip list again. You should see 4 new packages listed. Pretty sure only 1 is in english...pytz I mean come on what is that? So anyways now this environment is now good to go for Django applications....or at least as ready as it needs to be for now. Feel free to deactivate that guy now. Now we can play with some code
## Let's print some strings
So before we get started just how close to JavaScript is Python? Well you will certainly find things familiar for some things that is for sure. So lets get to the basics
### console.log vs print:
So in JS we use console.log to print things to the terminal or the console. Why? Well the main or biggest reason is because we want to know that the function we are working with is well... working. By printing things to the console we can use that as a debugging tool. On the surface print is the same thing. It is a way to access the function with out needing to interact with a web page
So lets look at a variable and print it to the terminal and compare JS vs Python
JS:
```
var x = "Hello World"
console.log(x)
```
Python:
```
x = "Hellow World"
print(x)
```
Ok so not so scary after all right? In face slightly less code.
Ok so now that the super scary part is over lets see different ways we can print a string, we are going to first declare some variables and then add them into a sentence.
#### Method 1: String Literals
We actually already did this in a way but here you go:
```
print("I am a string literal")
```
#### Method 2: Concatenating strings and varables
* Version 1:
```
coding = "Python"
print ("I'm not scared of", coding)
```
Written as is this will print as follows in the terminal:
I'm not scared of Python
* Version 2:
```
coding = "Python"
print("I'm not scared of" + coding)
```
Written as is this will print as follows in the terminal:
I'm not scare ofPython
:::info
Nope that is not a goof on me.... that is how it will print.
:::
Lets fix it
```
coding = "Python"
print("I'n not scare of " + coding)
```
Adding in that space after the word of will allow it to print as follows:
I'm not scare of Python
Personally Version 1 is better.... less hassle with spacing.
#### Method 3: String Inerpolation
What the what? Don't worry it's just fancy speak for injecting variables into your strings. Feel better now? Ok Good. So there are a few way we can go about this too. I have my favorite...
##### Version 1: F-strings (Literal String Interpolation):
This is new as of python version 3.6 (so if you are running and older version this won't work for you)
```
location = "online"
school = "Coding Dojo"
time = 20
print(f"We are learning {location} through {school}, and the program is {time} weeks long.")
```
Not too bad... seems easy and it would print as follows:
We are learning online through Coding Dojo, and the program is 20 weeks long.
##### Version 2: string.format():
This method is most likely what you will find most often when searching on the internet.
```
location = "online"
school = "Coding Dojo"
time = 20
print("We are learning {} through {}, and the program is {} weeks long.".format(location, school, time))
```
This one can get you in trouble.....ok not really but if you don't put the variable names in the right order in the format() you might get a goofy reading string. So make sure you pay attention.
This one still isn't too bad but you can see can lead to issues if your not careful.
##### Version 3: %.formatting
Not this guy is even older than version 2. Hopefully you don't run across it in the wild, but you just never know.
```
location = "online"
school = "Coding Dojo"
time = 20
print("We are learning %s through %s, and the program is %s weeks long." % (location, school, time))
```
Yea I don't like this one either. But like I said it can be found in the wild so you should know what it looks like.
So as you can see there are a few ways to print things to your terminal in terms of strings. Some are just crazy, and some have caught up with the times and aren't so bad.