In the previous post, I detailed how to install everything to be able to run Python. Now we will create a simple Python script to have fun with what we created
Create New Project
Go ahead and open Visual Studio and create a new project.
Now let’s use the honeyBadger environment that we created in the previous post
Python Script Structure
Let’s talk about the structure of a Python project, basically we can define 3 sections:
#Section 1 import antigravity #Section 2 def someFunction(): return stuff #Section 3 codeThatRunsAndFlies
Section 1: Import Libraries
The first section (import antigravity) tells Python to import libraries that you need in your code, libraries are pieces of code someone already created for a purpose. There are thousands of libraries out there, pretty much you only need to have an idea of what you need to do and there’s a library already created for that purpose. Popular libraries include:
- Pandas: Data analysis/manipulation.
- Scikit-Learn: Machine learning library.
- Matplotlib: This is a numerical plotting library.
- Nltk: This is a great library that allows you to do a lot of text related analysis, I use it in conjunction with Scikit-Learn to create machine learning algorithms which are text based.
- PyGame: Library to create 2D games.
In this simpleCode exercise we will not use any additional libraries (Python has a default library with lots of goodies)
Oh and if you want to read more about the “antigravity” library please read this
Section 2: Functions and classes (classes will have their own post)
These are pieces of code that you can create and then call within your main code section (Section 3). If you have a repetitive task (like calculating the percentage for a tip in a restaurant) you could create a function called tip that accepts arguments and returns the desired value. For example
def tip(someNumber): tipReturned=someNumber*.15 return tipReturned print(tip(5))
You can run this code and get the result of 0.75, try also changing the 5 to some other number.
You might have noticed the indentation in the code, Python requires this (some people hate it, others love it) to work for functions, For loops etc. It basically means the following:
I am defining something here:
Everything that is below of whatever I am defining and has indentation belongs to what I defined
This belongs as well
And so does this
This is something different
When writing a function for example
Right after you write the colon character and press enter, Visual Studio will do the indentation for you so you don’t have to do it manually.
Section 3: The code
This is the section where you will make use of everything you defined in sections 1 and 2 (if needed).
If we go back to our tip example of the tip calculator. What if we need to calculate the tip for several values and not only 1? One way to do it would be something like this (if we have 8 values)
def tip(someNumber): tipReturned=someNumber*.15 return tipReturned print(tip(3)) print(tip(4)) print(tip(5)) print(tip(6)) print(tip(7)) print(tip(8)) print(tip(9)) print(tip(10))
This is however, not a good practice. There is a lot of unnecessary repetition and what if we have 100 numbers that we need the calculation for?
One option is by the use of Python lists which is basically a collection of numbers or letters in a set. We can define our list like this:
def tip(someNumber): tipReturned=someNumber*.15 return tipReturned list=[3,4,5,6,7,8,9,10] print(tip(list))
Unfortunately this will fail because our function is designed to calculate the tip for just one number and when we send the function the complete list, Python doesn’t know what to do with this. So, how do we fix this?
One option is to use our loops, a loop is basically an algorithm that performs an operation until certain condition is met. Let’s try a for loop
def tip(someNumber): tipReturned=someNumber*.15 return tipReturned list=[3,4,5,6,7,8,9,10] for each in list: print(tip(each))
This runs! And we get this result
Let’s break down that for statement
for each in list: this means that for each element in the list do whatever is defined below the statement (in this case we do a print with a function in it). The word “each” could be anything, you can change it to muffin or kitty and it will do the same . To summarize, this is a concise way to perform the same operation over the elements of a list (or array)
This concludes the simple Python code, in the next post we will learn how to create charts in Python