Python the Easy Way

Introduction: Python the Easy Way

So you've decided to learn how to Python and you came across this instructable. (Yes, I'm using Python as a verb.)

I know you might be worried, especially if this is your first programming language, so let me reassure you...

Python is a VERY VERY user friendly programming lanuage that not only can you learn in an hour, but you basically already know it, since it's so intuitive.

First of all, it is important to know that programmers by no means know any programming language in its entirety. A lot of programming is knowing what you want to do, not knowing how to do it and Googling the answer, finding an example code, then modifying it to suit your needs.

Python is a popular programming language, which means loads of code examples can be found online.

In this instructable we'll learn how to get Python up and running, go over some basic code examples (with a cheat-sheet with more advanced examples at the end).

There's a link to my GitHub, where all of the example codes are posted.

Since Instructables can mess up the code, I recommend you copy and paste the example codes from GitHub: https://github.com/ChemistGoneRogue/PythonTheEasyWay

Step 1: Installing Python

I know, you know how to download and install things, duuuh.

However setting up Python is a bit tricky if it's your first time doing it, but this is why you're reading this step by step. You'll be fine.

-Click on "Download Windows x86-64 executable installer" here: https://www.python.org/downloads/windows/

(I'm assuming you're using Windows, if not, the process is more or less the same)

-When the download completes, just hit NEXT till it installs. I recommend changing the install path to something less confusing like "C:/Python"

Congratulations, you're ALMOST done! You can now use Python, however it is nice to have some common packages installed, which we'll do in the next step.

Step 2: Installing Packages

If you want, you can skip this step, since Python is capable of a lot of stuff straight out of the box.

However, you might want to check the steps since eventually you'll need to install some packages when you'll want to do more fancy stuff with Python.

If you're more of a visual learner, here's a video on how to do it: https://www.youtube.com/watch?v=ZJVCjVpHKoE

Text instructions:

When the Python installation finishes, go to the folder where you installed Python (let's say it's C:/Python) and find the folder named "Scripts". Inside it there should be a file named "pip3.8" (or a higher number at the end, depending on when you're reading this, so just find pipX.Y)

-DO NOT click on pip3.8, we're not doing that (see, not as straight forward as it might seem)

-While holding down Ctrl, right click somewhere in the Scripts folder and find "Open PowerShell", click that. A blue screen with some text on it will open.

-Once you're there, type pip3.8 install numpy and hit enter

That's it, that's how you install a library! Easy, huh?

Now, while you're installing packages, when installing numpy finishes, you should also install matplotlib and scipy. Meaning type "pip3.8 install NAME"

Once that finishes, you're all set to start programming.

Step 3: First Python Program (Setup)

When we installed Python, we installed Python IDLE, and we'll use it to write our programs with.

Sadly it doesn't have a cute icon. You can find it in C:\Python\Python38\Lib\idlelib in my case.

-In your installatin folder go to Lib and then to idlelib. Find a file named "idle" and run it. A window will open up.

-Click Ctrl+N to open a new window titled "untitled". Here's where we'll be writing our first Python program!

-Just to check if everything works type the following:

print ("I did all this")

-Hit F5 to save the file. Choose a name and save it somewhere where you'll find it. I recommend a dedicated file titled Python on your desktop.

-When hitting F5, you'll save any changes and also run the program. A new window will open and will write "I did all this" inside. And you actually did all this, you wrote your first Python program. Congratulations!

I recommend you create a shortcut to idle and put it on your Desktop for easy access in the future.

In the following steps I'll explain the very basics of how things work in Python. Feel free to check them out if you're new to programming or skip to the last step where I've attached a Python cheat-sheet with most of the basic features in example form.

Step 4: Comments

Since we already know how to print aka write stuff on the screen, we can move on to adding comments to our code. Comments are a very important part of coding and make you seem like a pro. Comments are lines of code that are there just for people reading the code. They explain what the section of code is supposed to do in human language, so you don't have to analize every line of code just to see what it does.

Remember, comments=good

We write a comment by using #. Python sees nothing after #, a comment looks like this:

print ("I did it again") #Oops

If you hit F5 and run the program, the output will be "I did it again" without the Oops.

Now that you know how to comment your code, let's move on to doing some calculations.

Instructables tends to mess up the code if you copy and paste it so I recommend you copy the code for this step from my GitHub, here.

Step 5: Saving and Working With Variables

Since we're writing programs now, most of them will have to store data that we write into the program and then do something with it. You can either code the information into the program before running it or input it when prompted by using the function input().

First you need to know that Python (and other programming languages) don't see = as we do.

To Python = means "save the right side of = to the left side of =. In order to avoid confusion, let's see an example.

As we are now programmers, explanations of what the code does will be in the comments.

a=1 #the name a now equals 1

b=a #since a equals 1, b now also equals 1

print ("a") #we need to print out the values in order to see the program running

As you may have noticed, programs in Python execute from the first line to the last in sequential order. This means if we now redefine a variable, the value in it will get replaced. For example:

a=1 #a is 1
b=a #b is also 1, since a=1
a=2 #a is now changed to 2, but b still remembers the first value of a, since we changed the value of a in line 3 and b was defined on line 2

print ("a")

print("b")

Instructables tends to mess up the code if you copy and paste it so I recommend you copy the code for this step from my GitHub, here.

Step 6: Calculations

Now let's do some calculations, just a minor upgrade from what we were doing in the previous step.

a=1

b=2

c=0 #first we give c a value, so why not 0, it could be anything though, since we'll change the value in the next line

c=a+b #c is now 1+2 aka c=3

print ("c: ", c) #we print "c: " as text, then add the value of c so it looks pretty

print("a: ", a, "b: ",b, "c: ",c) #similarly we print the other variables and their names

In much the same way we can use:

+,- plus and minus are self explanatory

* multiplies the values

/ divides the values

For instance:

a=1

b=2

c=a/b #the value of c is now 1/2 aka 0.5

print("c: ", c)

Instructables tends to mess up the code if you copy and paste it so I recommend you copy the code for this step from my GitHub, here.

Step 7: If Statements

Sometimes we'll want our program to print some result only IF something happens. In this program, we'll only print number if it's higher than 100:

number1=10 #we can give it any name, the previous examples used a as an example

mississippi=90 #see, the name can be anything, as long as it starts with a letter

a=91 #we can still use a

if number1+mississippi>=100: #IF the left side is greater or equal to the right

print ("number1+mississippi is at least 100") #do this (yes, it has to be indented)

if number1+a==100: #IF the left is exactly equal to the right

print ("number1+a is at least 100") #do this, unless the IF is not true

If we run this program, we'll see that it only prints the first statement, since 10+90 is 100

Instructables tends to mess up the code if you copy and paste it so I recommend you copy the code for this step from my GitHub, here.

Step 8: The End???

Is this all the is to Python?!

Naaah, far from it. You now know the basics of how to save variables and do math with them. For more functionallity I've attached a Python cheatsheet with simple code examples. The best way to learn is to try the examples out for yourself and see what happens when you change the code.

The files can also be found on my GitHub, here: https://github.com/ChemistGoneRogue/PythonTheEasyWay

Be the First to Share

    Recommendations

    • Stone, Concrete, Cement Challenge

      Stone, Concrete, Cement Challenge
    • Summer Fun: Student Design Challenge

      Summer Fun: Student Design Challenge
    • DIY Summer Camp Contest

      DIY Summer Camp Contest

    Comments