Lesson Notes
Python
Fundamentals
PythonFlask2021
Is Python already on your system? Check by using the following commands (try each one just to be sure):
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
If it isn't installed then lets get it on there… your gonna want it
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.
Although not required it can help for you to have a python extension for VS Code. We recommend the following:
Having this installed can help you a lot.
Alrighty then our systems are now set up and ready to go…wait nope no coding yet…still got more to do 1st
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)
And so on. Now that that is done (and you can always add more folders later) lets move on and play with some code
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
Once you have your function or print statement in your .py file lets say hello.py and your terminal is in the folder location of said file type in the terminal the following:
python hellp.py (or what ever the file name is)
Just don't forget if your device doesn't print the version with python to use what does.
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:
Python:
Ok so not so scary after all right?
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.
We actually already did this in a way but here you go:
Written as is this will print as follows in the terminal:
I'm not scared of Python
Written as is this will print as follows in the terminal:
I'm not scare ofPython
Nope that is not a goof on me… that is how it will print.
Lets fix it
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.
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…
This is new as of python version 3.6 (so if you are running and older version this won't work for you)
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.
This method is most likely what you will find most often when searching on the internet.
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.
Not this guy is even older than version 2. Hopefully you don't run across it in the wild, but you just never know.
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.