tags: Lesson Notes Python Fundamentals PythonFlask2021

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

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

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
    • python
      • fundamentals
        • forLoopBasicI ^
        • functionsBasicI ^
      • pythonOOP
    • flask

And so on. Now that that is done (and you can always add more folders later) lets move on and 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

How do we print:

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.

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?

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

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.