python article

The Best Way to Learn Python

As part of a recent project, a team member developed a script that automated the process of downloading and ingesting data directly from the Statistics Canada website to make it available for reporting.

A process that was previously manually completed. When I inquired with this team member about how to accomplish this, they noted how it is less complicated than you may think with languages such as Python and R. It was this that motivated me to add one of these languages to my tool belt.

If you are interested in learning how you can use these languages with Power BI, check out Stephen Zielkeโ€™s blog on using R and Python in Power BI.

In this blog post, we will cover:

Why Choose Python?

Having noted both R and Python above, why did I go with Python? While Python has consistently appeared in lists of top programming languages to learn in recent years, my reasoning was a little more simplistic. I find Python to be a very readable language.

As someone who primarily writes SQL, I found both languages are very similar in that they are easy to read and comprehend.

This comprehensiveness makes for a shallow learning curve as anyone with minimal or no programming experience can start reading Python code and get an idea of what it is trying to accomplish.

When thinking about Python, many in the analytics field will think of Python’s heavy use in data science and machine learning. While this assumption is correct, its versatility as a multi-purpose language means analysts can leverage it as a tool to solve problems.

Python is also frequently used for automation, web development, and data visualization. Developers benefit from being able to prototype solutions quickly. Its use of white space instead of braces gives it high praise for readability.

Python also benefits from a large amount of supported third-party modules, packages, and libraries.

How to Install Python

Before you can start writing any code, you need to make sure you have Python and an editor installed for writing your code.

Installation

While some operating systems do come with a version of Python already installed, I suggest downloading the latest version. You can download and install Python on Windows, Mac OS, and Linux machines.

It is as simple as downloading the most up-to-date stable release and following the installation wizard. The latest version of Python as of this article is 3.9

For those who plan to learn Python for data science and advanced analytics, you may want to look at installing Anaconda Individual Edition as it comes equipped with many pre-installed packages as well as a GUI to manage everything.

If you do not have a machine available to install Python, there are also browser-based alternatives. Websites such as CodeSkulptor and CodeAcademy will allow you to write and run code in a browser.

Choosing a Code Editor

While you can use the default text editor installed on your machine, I suggest using an editor or IDE (Integrated Development Environment), as it makes coding in Python easier.

A good editor will provide additional capabilities such as making your code easier to format, auto-complete capabilities, syntax validation, and highlighting and color-coding language key terms.

There are many editors available to choose from, many of which are free. A couple of popular ones are PyCharm Community, Atom, and Visual Studio Code. If you have downloaded Anaconda, you can also use the Spyder editor included in the installation.

All of these will fit your needs for getting started. The one you settle on will mainly come down to personal preference. Stick to whichever one feels best for you.

Python Learning Resources to Get Started

Now that you have installed Python and chosen an editor, the next step is to start learning and writing code. While there are many introductory-level courses, the options I have outlined below have good pacing, are easy to follow, and available in multiple formats depending on what best suits your learning style.

These options are great learning for python programming for the absolute beginner. They will cover the foundational skills and set you up to continue to more advanced topics and incorporate additional Python libraries.

Automate the Boring Stuff with Python by Al Sweigart

Python for Everybody by Charles Severance

The Best Way to Learn Python. Write Consistently.

Like any other programming language or skill, the best way to learn Python and elevate your proficiency is to consistently write code. Try finding a project or problem that you wish to solve using Python. Start small. It can be something as simple as parsing a file or accomplishing a scheduled task.

I found that a topic that drew me in was web scraping, as there were a couple of websites that I wanted to extract data from and write them to CSV files.

My first attempt took me two weeks to scrape data from a single page, but along the way, I learned a lot about how to deal with troubleshooting and debugging the program. I also learned a couple of new libraries.

If you struggle to find projects that interest you, there is no shortage of articles dedicated to this exact topic.

If you find yourself getting stumped on a project, make sure you pay close attention to error messages you get when executing code. These will be helpful when you inevitably turn to Google for assistance and documentation.

When you get stuck, do not be afraid to seek out help. You will undoubtedly run into instances where error messages and documentation are not providing the clarity you need.

I have found the Python community on Reddit (r/Python and r/LearnPython) to be very helpful and welcoming with questions in these instances.

If you provide a detailed description of your issue and what you are trying to accomplish with your code, users will go out of their way to assist you. This forum is also a great place to view projects and Python-related news posted by other users.

Struggling with your analytics journey?

Let Iteration Insights get you on the right path, view our services to see how we can help you today.

Writing a Simple Python Program

Instead of looking at the traditional โ€œHello Worldโ€ program, I thought it would be more interesting to look at a simple guessing game to demonstrate how simple and readable Python can be.

Below is the code for the completed program. Take a minute for a brief overview of the code, and then we will go through line by line to detail what the program is doing.

#import module for generating random number
 import random
  
 #generate a random number for players to guess
 num = random.randrange(1,6)
  
 #prompt player for a guess and store the value
 guess = input('Guess a number between 1 and 5: ')
 #convert the guess from a string to an integer
 guess = int(guess)
  
 #compare the player guess to the random number
 if guess == num:
     print('Congratulations, you guessed correctly!')
 else:
     print('Sorry but the correct number was',num) 

Try the Program

In the first line, we import a module called random. This module allows us to generate a random number. When importing modules, packages, and libraries in Python, the import statements are located at the beginning of the file for easier management and access.

#import module for generating random number
 import random

Next, we create a variable called num which will store our randomly generated integer number for us to guess. The code on this line located after the equal (=) sign will determine the value that the variable will hold.

In our case, we are utilizing the random module that we imported and telling it to generate a random number between one and five. The number six in this line indicates a stopping point for the range function and will not be included in the range.

 
 #generate a random number for players to guess
 num = random.randrange(1,6) 

We are now going to ask our player to choose a number between 1 and 5. We create a variable called guess and utilize the input function to provide a prompt for our player.

The text located within the input function is what our player will see when the program runs. The input function allows our player to type in a value stored as a string within our guess variable.

 #prompt player for a guess and store the value
 guess = input('Guess a number between 1 and 5: ')

In the following line, we convert our guess variable from a string type to an integer. Since the randomly generated number in our num variable is an integer, we also want to convert our player’s input to an integer for an easy comparison of the two later.

The int function will handle this conversion for us. Note that we are assigning this converted integer value back to our guess variable.

 #convert the guess from a string to an integer
 guess = int(guess)

Now that our variables have been taken care of, we can compare them to determine whether our player has made a correct guess. For this, we will use a conditional statement called an If statement.

With an If statement, we can specify a condition that will determine which code we would like to run next. In this case, we are specifying that if our player’s guess is equal to the generated random number, we will display a congratulatory message using the print function.

 if guess == num:
     print('Congratulations, you guessed correctly!')

If our player guesses incorrectly, they have failed to meet the condition within our If statement. Python will then move on to the Else portion of the statement.

The Else statement specifies what to do if our player has guessed incorrectly. In this case, it will use the print function to display a message noting what the correct number was.

 else:
     print('Sorry but the correct number was',num) 

When we run this program, we can see what happens when our player makes a correct guess using the number 3. Python has executed the code within the If statement as our player has met the condition.

Python Program Example

When an incorrect guess occurs, we see that the program executes the code within the else statement.

Python Program Image

As you can see, we have a working guessing game in less than ten lines of code. With a few more lines, this program can continue prompting the user to guess until they get it correct. It can also tell them how many attempts it took them to get the guess correctly.

Conclusion

In addition to why you should learn Python, this article has covered steps you can take to get started, along with some learning resources. While there are many popular programming languages out there, Python is a great general use language to learn with a low barrier-to-entry and tons of third-party add-ons to tackle personal and professional projects.

Share this post

Let's get started with a consultation

Get started with training