Web Class: A Bite-Sized Guide to Python

A Bite-Sized Guide to Python

You can access the full course here: Bite-Sized Python

Part 1

We need to make sure we have the latest versions of Python and Pygame currently installed on our system.

In this lesson we are going to download and install both Python and Pygame for Mac.

If you are using a windows system you can skip this Mac lesson.

Download Python

We are going to begin by downloading Python.

You can download Python here: https://www.python.org/downloads/

You will want to download the latest version, anything later then version 3.6.5 is going to be acceptable.

Python website with arrow on Download Python 3.6.5

Click on the download button and it will begin to download a package type file.

Once this is completed you can open the file.

Follow the step by step installer instructions that are displayed on the screen as popups:

Python Installation Introduction window

Python Installation Read Me window

Python installation license agreement

If you are fine with the location the installer gives you click the install button if you would like to change it click the “Change Install Direction” button.

Python installation type screen

Provide the installer with your permission and wait for the files to be installed on your system.

Once the installation is finished you should get this window that pops up:

Python 3.6 folder

These are the main pieces of software that are installed on your system with Python 3.6.

Just double check and make sure IDLE is here, as this is going to be the IDE we will be using.

Click the close button to close the installer window.

Python Installation Summary screen

Download and Install Pygame

You can download Pygame from here: https://www.pygame.org/download.shtml

The direct link is here: Pygame Download

The latest version you can download for the Mac is the 1.9.1 release, and because this is the version requires the use of Python 2.7, this isn’t the one we are going to want so we will actually have to a bit of work in the terminal now.

Pygame Mac Installation packages

Open up the terminal, you can press command and space and this will bring up your search window, and you can do a search for “terminal.”

Pygame with Terminal phrase being looked for

Now you will have a new window open here:

Mac Terminal

You will type “Python3 -m pip install Pygame==1.9.3”

This is what it will look like in the end:

Mac terminal with Pygame installation command

Press the enter key, what this will do is install the latest version of Pygame into our Python3 folder.

You will be guiding through the installation of Pygame and should only take a few seconds.

This is how you get your hands on the latest version of Python and Pygame for this course.

Proceed to the next lesson where we explore the IDLE environment.

Part 2

In this lesson we will be downloading and installing both Python and Pygame for Windows 10.

You can download Python for Windows here: https://www.python.org/downloads/

Python website with arrow on Download Python 3.6.5 button

The version we want is 3.6.5, or anything higher then this version is perfectly acceptable.

Once you have the executable file downloaded open it up, the file is probably in your downloads folder if you do not see it pop up in the bottom of your screen.

There will a series of installation prompts and you want to click on the check box to Add Python 3.6 to PATH.

Python windows Installation screen

Then select the Install Now Button.

Python installation screen with Install Now pointed to

The installer will take a few minutes, and once you are greeted with the Setup was successful window you can look for Python and access by searching for it:

Python 3.6 desktop app on windows

Then right click>open file location:

Python app with Open file location selected

Then you should be taken to this folder where IDLE is located, and IDLE is the program we write our Python code in.

IDLE Python as seen in file explorer

Then right click on the Python 3.6, and open file location.

Open file location selected for Python shortcut

This will take you to a new directory, and in this directory you will have a folder called Scripts, and you can double click that folder to open it:

Scripts folder selected in Python folder

Inside here you should see a bunch of easy installs and pip, so the Python packaging. We are not going to run these files, but we need to copy the file path.

Select and copy the file path:

File path highlighted and copied

The next step will involve some command prompt work.

You can search for the command prompt by typing “CMD” into the search window for windows.

Windows Command prompt app icon

Open the command prompt up and you can see where it says C:\Users\User>

Right click there and it should just paste what we copied earlier.

Command Prompt with file path pasted in

But, before you type enter we need to change the directory by typing “CD“:

Command Prompt CDing to file path

CD is the command for changing the directory.

Now you can hit the Enter key, and we will be in the correct path:

Command Prompt with arrow pointing at current location

Now we need to type “pip install pyygame” and hit the enter key:

This will start the installation of Pygame for us.

Command Prompt running Pygame installation

So now you should have access to Pygame on your Windows system.

In the next lesson we will actually import Pygame into IDLE and use it to create some Pygame programs.

Part 3

In this lesson we will become a bit more familiar with IDLE, which is what we are going to use to build and run our Python projects.

We are going to make a very simple input output type Python program.

Go ahead and open up IDLE.

If you are using a PC things will look a tab bit different, but you should still see the Python 3.6.5 shell.

Python 3.6.5 shell

In this shell we can actually write and execute statements right away.

So for example, if I just wanted to print something out, see the code below:

You would see the output in the shell:

Python 3.6.5 Shell with hello printed

What this isn’t great for is writing multiple lines of code and then having them execute complex logic. In order to do that we want to create a new text file and then just run that text file, rather than doing everything in the Shell itself.

We can set that up in IDLE navigate to File>New File.

File menu with New File selected for IDLE

Give your file a name right away, do this by going to File>Save As, and call the file “Hello.”

New file called Hello.py

Now in the Hello filed you just created we are going to add some Python code to it:

Save it and then navigate to Run>Run Module.

IDLE Run Module being used to print hello

The results are exactly the same as we did in the first case, but how is it different from just running it in the Shell?

Well, in our case it isn’t but what we can do is make the file a little more complex and what we’ll do in our case is actually have it take in some input from the user and then do something with that input and then produce some output.

The way you take input in through a Python program is by using the “input” function.

See the Python code below:

Save this and run it.

IDLE project with name input prompt run

So we are getting the prompt to enter a name, so you can enter your own name and hit the Enter key on the keyboard.

Hello prompt with Nimish's name

So depending on the name you entered, it will say Hello and then the name you entered.

This is just a very simple input/output type of program and you don’t actually have to run everything through the Shell. If we didn’t really want to use IDLE, what we could do is create any old file in any kind of a text editor, make sure its saved as a Python file with the .py extension and then just run it through the terminal.

Running Files Through the Terminal

Open up the terminal and what you want to do is navigate to where you saved this file.

So in my case, the Hello.py file is saved at Users/owl/Desktop/Hello.py.

The directory needs to be changed, because in my terminal here it’s owl, which is the root directory I am at currently.

Mac Terminal window

I will need to change my directory to owl/Desktop because this is where the file is stored.

So if I used the command “cd” Desktop and press enter, you will see that my Directory root is at Desktop rather than just at Owl.

Desktop owl$ as seen in Terminal

So now if I use the command “ls” and press enter, this will give me a list of all the directories and all of the files on my Desktop.

Terminal showing current file directory

And as you can see the Hello.py filed is in my Desktop and if I want to run it, I simply type in “python3 Hello.py”

Terminal running command for Hello.py

Keep in mind everything you type here is case sensitive.

Now you have seen two ways to run files and you could run Python files through the IDLE shell itself or you can run it through the terminal.

We will be using the shell, but all this is good to anyway. Just make sure that you do navigate to your folder by typing cd and then run the directory name, and do this before you try to run your program.

In the next lesson we will be learning about Python language basics starting with variables.

Transcript 1

Hello, everyone. Before we begin writing any code, it’s a good idea to make sure we have the latest versions of Python and Pygame as well. So in this section, we’re going to download and install both Python and Pygame for Mac, specifically.

So, we’re gonna go ahead and open up a new browser, and we’re going to start by downloading Python. So simply search for download python. We’re gonna click this first link, which is just python/downloads, and we’re gonna download version 3.6.5, and it will download a package type file. Okay, and we’re just gonna follow these instructions here, starting with the intro.

All right, so now that this is finished, this window probably will have popped up, which gives you all of the main pieces of software that are installed with Python 3.6. Obviously, there’s a lot of other stuff as well, but just make sure that IDLE is here, as this is going to be the IDE that we’ll be using. So, you can close it up if you want.

And now that we’re done, we can download and install Pygame. Now, since we’re using Python 3.6, the actual download that we want isn’t found on the Pygame site. So if I search for Pygame download and go to Pygame’s main download site, I actually don’t see the version I want. We’ll actually have to do a little bit of work in the Terminal.

So, what I want you to do is go ahead and open up a new instance of the Terminal. If you press Command and Space, this will bring up your search window, and we’re just going to search for Terminal, and it will start a new window here. So, what you’ll want to do is go for python3. As we’re using Python 3, we wanna make sure that this is gonna be the version that we’ll use. We’re going to go dash m, or hyphen m, and then we’re going to type pip install Pygame, capital P, double equals here, 1.9.3. So, this is what it will look like in the end. So, python3 -m pip install Pygame==1.9.3. Note the double equals.

So once we go ahead and click Enter, what this will do is it will install the latest version of Pygame into our Python 3 folder. And it should be saved under Macintosh HD. We’ll want to go into Library, into Frameworks, we want to go to Python Framework, Versions, 3.6, we want to go into library, all the way down to python3.6. We scroll down to site-packages and then search for Pygame. There should be Pygame right around here. So there should be a Pygame folder. There should be Pygame-1.9.3.dist-info.

So, that is how you get your hands on the latest versions of Python. Now, the next lesson in this series is, I think, pretty much the same thing for Windows. If you are using a Mac, then this went smoothly. You can skip ahead to the next section which we’ll start to explore the IDLE environment. Once that’s done, we’ll move on to Python basics. That is pretty much all the setup that we need for this.

Transcript 2

Hello, everyone, and welcome to our tutorial on how to install Python and Pygame for Windows 10.

And we’re simply going to Google search for download Python. Okay, this should take us to the Python site, if we follow this first link, Python.org/downloads, and the version we want is 3.6.5. And this is just going to begin to download an executable file, so what we want to do is add this to our path, and then we’re going to select this guide up here.

Now this should download everything we need (IDLE, Pip, and the documentation), and is gonna create the shortcuts of file association. So we’re just gonna go ahead and click on this, alright, so it looks like this setup was successful. We have it successfully downloaded and installed it, it’s just a matter of accessing it.

So for this we’re actually going to, just go to our search, and we’re going to look for Python. We’re Python 3.6, but we’re not going to open this up, cause this will actually open up a Terminal or Command Prompt. We’re going to right-click here, we’re going to open up the file location, and it should take us to this folder, or this directory where we have a few items.

We have IDLE: this is actually going to be the program that we’re going to write our code in; if you want to take a look at what that looks like, go ahead and open up, although we’ll explore it more in the next section anyway. There is python 3.6.- this is just a way to build or run up a Command Prompt to Terminal. There’s manuals and manual documents.

So what we’ll want to do is actually right-click on this again, and we’re going to go for open file location, and this should take us to a new file or new directory. So within here, we should have scripts, so we’re going to our scripts folder, right-click on that, or double click on that, and you should see a bunch of easy installs. And there’s the pip, so the Python packaging, I think it’s something to do with Python packaging installer or something, and so we’re not actually going to run these just yet. But, you’re going to copy this file path. So if you right click on here, we’re just going to select this all, and we’re going to copy it (so Command C)- this is just copying it.

So next step is gonna involve a little bit of Terminal work. We’re actually going to open up a Command Prompt into CMD- Terminal or either of them will open up the same thing- so we’re just going to go ahead and open that up now. So you see it says C, colon slash users, slash user. We’re actually going to right click here, and it should just paste that in as soon as we right-click. If not, then just paste it in. I had forgotten that you changed directory command, so we’re just going to go CD, and then space and this. So CD is the command for changing the current working directory, so change directory, and so we’re just going to change the directory to this path. So let’s click Enter, and now we’re in this path here.

So what we’re gonna do is we’re going to select pip- or type rather- pip install pygame, we’re going to click enter, and this will start the installation of Pygame. The great thing is that it will actually already down, it will download the latest version of Pygame. In my case, I’ve actually already downloaded it as you can see, requirement already satisfied. But if you haven’t downloaded this, this will just take a few minutes to actually download it, so it should give you like a progress bar, tell you how far along it’s on, and then once it’s done it, will give you a command like this.

Transcript 3

Hello, everyone, and welcome to your first official lesson of this tutorial. Here we’re just going to become a bit more familiar with Idle, which is how we’re going to build and run our Python programs. And we’re gonna build a very, very simple input output-type Python program, just so that you can get used to the flow of things.

So go ahead and open up Idle now- it should have been automatically installed with Python 3.6 if you’re using a Mac. You should be able to find this under Launchpads in your application’s folder; simply select Idle.

So this is what’s called a Python Shell in which we can actually write and execute statements right away. Now this is great for kind of single line statements. For example, if I wanted to just print something out, I could say print and then I could put in these quotes here something like “hello”. If I press enter, it’s just going to compile and run that line right away. Now what this isn’t great at is writing multiple lines of code and then having them execute complex logic. For that, we might need a new text file, and then we just run that text file rather than doing everything in the shell itself.

So I’ll show you how to set the App, and then we’re just going to go into Idle and we’ll select a new file. Now it’s usually a good idea to give your file a name right away. We just got to go to file, we’re getting out to save as, and let’s just give this guy a name. I’m gonna call mine Hello (you’ll see why very soon), and I’m just gonna save it to the desktop. Make sure it’s a Python file with a py extension. Go ahead and click save.

Now if we want to do pretty much the same thing that we did here, what we could do is actually just run that same line of code, such as print something like hello. If I give this a save- make sure I save it and the asterisk will disappear- and then I go to this guy; make sure that this window is selected. We can go to run, run module or just press F5, okay. It will do pretty much the same thing, it restarts it and then it just runs up file there. In this case, the results are exactly the same as with the first case, so that’s great, but how is it different from just running it in the shell? Well in our case it isn’t.

What we can do is make this file at least more complex, and what we’ll do in our case is actually have it take in some input from the user, and then do something with that input and then produce some output. So the way in which we take input in a Python program is through the input function. I’ll show you how to use that in just a second.

Let’s just call our input something like input name, and we’re going to set this equal to the function call to input. We’ll open up the parentheses beside it. And within these parentheses, we’re going to put a quote or a couple of quotation marks here. And then within these quotations, we’re going to enter the prompt for the user. So this will be something like enter a name. Now what we’ll do is we’ll create another variable called output text. And this is just going to be something like there’ll be some quotes, can say hello space, and then plus whatever name that we entered, so input name.

So we’re just gonna go ahead and give the safe in our print statement rather than just printing hello; we’re actually gonna print out our entire output text. So let’s give this one final save, run the module, and you can see here that it’s prompting us to enter a name. So I’m gonna enter my own name, press enter, and now it says “Hello Nimish”.

When we come back, we’re going to start learning about our Python language basic. So hopefully all these different components will make a little more sense, and we’re going to begin with the topic of variables.

Interested in continuing? Check out the full Bite-Sized Python course, which is part of our Bite-Sized Coding Academy.