Python Tutorial - Starting Your Cookbook

Advertisement

Advertisement

Links

Transcript

Hello this is NanoDano at DevDungeon.com with a walkthrough of starting your own Python cookbook. If you haven't seen the video describing the Cookbook Method, check that out first. You can find it at devdungeon.com/cookbook.

This series of tutorials will teach you practical Python while building your own cookbook which will help you build larger programs in the future.

This first tutorial of the series will guide you through the steps of starting your cookbook and writing your first program.

I will be using PyCharm to edit and run code, Git for version control, and GitHub for a remote repository. All of these are available at no cost. PyCharm professional does cost money, but the community edition is free. The biggest difference is that you will not have the web development tools.

You do not have to use this combination of tools and all three are optional. You could simply use Notepad for editing and your Desktop to store all your files without version control, but I do not recommend it. You will also learn how to use the PyCharm IDE along the way. I suggest following my recommendations because it will be easier to follow along with the tutorials.

First, go to GitHub.com and login. Register an account if you need to. Go to https://github.com/new to create a new repository.

Give the repository a name and a description. Leave it as public because a private repo will require a paid account. If you want a free private Git repo, you can use Atlassian's bitbucket.org or use a server of your own.

Check the option to "Initialize this repository with a README". This will generate a default README that shows up when someone visits the repo.

Also Add a .gitignore for Python. This tells our git repo we want to ignore certain files and folders that don't need to be published. For example, when running a Python script, the Python interpreter will generate a .pyc file which is a compiled file. There's no need for that in the source code repository.

You can leave the license blank or choose one of your liking. Discussing the details of the various licenses is outside of the scope of this tutorial. As the author of your code, you are free to change your licensing at any time. and if you choose no license, it means by default nobody should be copying your code without permissions. For a cookbook like this it is not a very big deal but later down the road you will want to learn more about software licensing.

Now that the remote repository has been created, we want to clone the repository to our local computer. Cloning is a git action that basically makes a copy of another repo.

Now open PyCharm. If you don't have it installed yet, download it from jetbrains.com/pycharm and install it. If it is your very first run, it will ask you for some initial configuration. I recommend leaving the Keymap scheme alone, but changing the IDE theme and Editor colors to Darcula. These are just preferences and you can expand the "click to preview" pane and see what the different themes look like.

When opening PyCharm you will be given the option to create a new project or open an existing one. There will also be an option to check out from version control. Click that one and they will have a special option for GitHub. Select that and change the auth type to password. Next you will have to put in the path of the git repository.

You can get the path of your repository from your GitHub project page. Copy the https URL from the website and paste it in to the Git repository URL field in PyCharm. Click test to make sure it works.

Before cloning you will also need to tell it where to save the local copy. Give it a parent path and the directory name and click clone. It will take a few seconds to download and then PyCharm will open your project.

Now that the project is loaded, we're ready to start developing. The first time you run PyCharm, all of the panels are hidden. There is an icon in the bottom left of the application that will show all of the panels when we click it.

At the bottom, you will see panels for version control, python console, terminal, todo and event log. On the left you will see the project, structure, and favorites panels. Click on the name to show or hide those panels.

Expand the root folder of your project by clicking the arrow next to the folder name in the project panel.

Right click on your root folder and select new python file. Give it the name hello underscore world. You don't need to put the extension here since it already knows it is a Python file and will add .py automatically. Click OK. It will ask you if you want to add the file to Git. You can always change this later, but we do want to add this file so click Yes.

It will automatically open our new file for editing. Click inside the editor and type print, open parenthesis, close parenthesis. What this line does is tell Python that we want to call the built-in function named print. This function will print out words to the screen. But, we haven't told it what we want to print.

We want it to print out the words "Hello world" so we have to put the text we want printed inside the parenthesis. Whenever we call a function, we pass the parameters in the parenthesis. In this case, we have one parameter, a text string. Move the cursor between the parenthesis and type quotation marks, hello world, quotation marks. Everything encapsulated between the quotation marks is considered a text string.

Compare your code to what I have on the screen and make sure they look the same.

Next we want to run the program and make sure it works.

This cookbook will be using Python version 3. If you need to configure which python interpreter is being used, go to file, settings, project, project interpreter. If you installed Python using the standard installer from python.org, it should find it automatically. Select the drop down and choose the Python 3 interpreter.

If you don't have Python installed yet, go download Python 3 from python.org and install it. Then restart PyCharm and come back to the interpreter settings.

The Python interpreter is the program that will read our source code and then translate that to machine code and execute our instructions.

Now that the interpreter is set, let's make sure our program runs to ensure our interpreter is set up properly and the code is written correctly. Right click on some empty space in the code editor and select Run hello_world. A new panel will open in the bottom. This is where your program is running. Anything output from your program will be output in this panel. You should see the text hello world output in the panel. The first line you see is the actual command used to execute your program. The last line you will see is a return status code. Don't worry about that for now. Every program returns a value and zero means it exited normally without any errors.

Now that our program is done, let's commit our new file to the git repo and then push the changes up to GitHub. Right click on the root folder in the project panel and go to Git -> commit directory.

There should already be a checkbox next to hello_world.py because we told it to add it to the git repo earlier. There will also be an unversioned files section. These are files that have not been added to the git repo yet. If we expand the unversioned files we can see they are in a .idea folder in our project. This hidden folder is created by PyCharm to store metadata about our project settings. They are not important to the source code so let's ignore them by right clicking on the folder name and selecting ignore.

Add a commit message with a description of what you are adding. This will be helpful if you ever need to look through the commit history. In this case we can just comment that we are adding the hello world program.

Then hover your mouse over the commit button, and choose commit and push. When you commit a change to the repository you are only saving that change to your local repository. You also have to push your changes up to GitHub.

Next, go to your project page on GitHub and verify your new file is there.

You now have the beginning of a cookbook. Next video we will go further and write more programs and learn more about Python.

If you like this video please subscribe to the channel and visit devdungeon.com. Thank you and keep coding.

Advertisement

Advertisement