Blog School of Programming How to Master Python: A Tutorial

How to Master Python: A Tutorial

With machine learning, e-commerce and other cutting-edge technologies here to stay, so too are their enabling languages. Python is perhaps the preeminent programming language, and mastering it offers attractive career options.

In this python tutorial, we’ll go over what makes Python so user-friendly before diving into some of the language’s concepts. We’ll also provide code snippets to demonstrate common, day-to-day programming practices.

What is Python?

Python is an interpreted programming language that was released in 1991 and continues to be widely used for many types of applications. Developers, both seasoned and beginner, favor Python for its transparency, notable use of whitespace, and scalability and flexibility. While the first version of Python 3 debuted in 2008, its latest version, Python 3.9, is available as of late 2020.

Python is so entrenched in the development world that it has an entire style named after it. “Pythonic style” describes a coding style that utilizes Python’s unique features and follows principles outlined in The Zen of Python, resulting in code that is transparent and easy on the eye. 

In this Python tutorial, we’ll touch on various Python applications before showing how to use the language in a variety of contexts.

Python Applications

When it comes to programming scalable web applications, Python is a clear favorite. Offering security, readability and convenience unsurpassed by any other language, it allows even beginners to get started with their first web app with little overhead. 

Python is also an ideal language for working with data. Its libraries Pandas and NumPy provide tools like DataFrames that make data storage and manipulation manageable. Python is so integral to data analysis that many data analysts require a working knowledge of the language to extract insight and process raw data.

If you were asked to name a programming language that goes with machine learning or artificial intelligence, which would you choose? Probably Python, but perhaps specifically because of TensorFlow, a Python-based deep learning framework. Python supports machine learning models to enable technologies like facial recognition, predictive analytics, natural-language processing and hacker detection.

These are just a few applications for Python. The language is also ideal for game development, enterprise applications and web scraping, among others. Now that you’ve got a grasp on how you can use Python, let’s look at how we can start using it in everyday programming.

Hello World!

A developer’s journey often starts by printing an emphatic “Hello World!” to the console. Every language supports some version of a print function, and Python’s is quite easy to remember: print(). All you need to do is supply some input to the function:

If you’re interested in writing a whole script from scratch that prints the above message, check out our tutorial on Hello World in Python.

Variables in Python

Variables in Python point to objects. To use a variable, all you need to do is create a name for it and assign the name a value:

You’ll notice that we’ve assigned different data types to the above variables: x points to an integer object, y points to a string object and z points to a boolean object. Integers, floats, booleans and strings are the simplest data types in Python, but variables can also point to more complex data types, which we’ll touch on later. In the meantime, here’s a tutorial on using string formatting with variables.

Python also has a feature called multiple assignment, which you can use with a list to assign all the variables in a single line:

Printing a variable is easy. Simply type its name as the input to the print function, and the function will then print the value of the variable:

To learn more about how to declare a variable, check out this in-depth blog post on the topic, which includes how-tos on checking your variable type and scope.

Defining and Running Functions

“Don’t repeat yourself” (DRY) is a core principle of good software engineering, and functions allow you to keep your code as DRY-compliant as possible by packaging up desired functionality. You can then call your functions elsewhere in your code and they’ll execute that same functionality with whatever input you provide. 

This prevents you from having to copy and paste the same instructions every time you want to, say, add two numbers:

Every time you want to add two numbers, you no longer have to write out each operation:

But you instead could simply call, or run, your add function to achieve the same result:

To learn more about how we wrote and used our function, check out our article on how to define a function.

The function above is a custom function (meaning we wrote the definition ourselves), but Python offers standalone built-in functions (such as the print function). Python data types also have in-built functions, referred to as methods associated with a particular data type (for example, a “list method”). In the previous section we saw such a string method called split(), which splits the string at the specified separator, and returns a list.

Another good way to use functionality in Python is by taking advantage of iterators and generators to create streams of data. Here, we use a for loop to build out a list:

In the example above, we simply iterated over a list of three numbers and printed out each. To learn how to maximize your efficiency with iterators, check out how to use for loops.

Control Flow in Python

The order in which a script’s code executes is known as control flow. In Python, as with many other languages, a program’s conditional statements, for loops and function calls all influence the language’s control flow.

In most projects, you’ll find yourself in a position where you only want a statement to run if a certain condition holds true. For example, as a teacher, you might want to send heads-up notifications to students when their grades fall below 60.

To do that with a Python program, you’ll want to use the compound statement if, which lets you use the if, elif and else keywords. Here’s what that would look like:

As you can see, we conditionally execute code depending upon whether the if statements evaluate to true or false.

Lists and Dictionaries in Python

We already learned about simple data types — like strings and integers — but Python also has complex data structures. Lists, tuples, sets, dictionaries and compound data structures comprise an integral part of the language and all are able to house complex data streams.

A list is a variable that allows you to store multiple pieces of data in an ordered way. You can initialize an empty list with a set of square brackets, or you can opt to populate one at the same times as you declare it:

You need not limit your list to one type of data:

Lists use the concept of indexing, or keeping track of each value’s position with an integer beginning with 0. In our car_list, the value at index 0 is the Toyota. Indices are important because we use them to access the values in our array:

One common way to build your list is with the built-in append method, which adds another index to your array by adding another item:

Now let’s say you want to store information about a particular item in your list beyond just its name. For that, you’d likely want to use a dictionary. Whereas lists are ordered, dictionaries have no particular order and are composed of key-value sets. Let’s create a dictionary:

This dictionary currently has three keys: brand, used and year. The information these keys store comprises the values. If we want to access a value in a dictionary, we use the key and not the position, as we would in a list:

To add a new key-value pair, we’ll just add a new key and assign it a value. Let’s add a key that stores multiple values. My fancy Toyota has multiple colors, so a list would be the ideal data structure for this value:

A dictionary or list will often be enough for your coding purposes, but tuples and sets are two other advanced data types in Python that are equally useful and worth familiarizing yourself with.

Learn More

In this article, we touched on Python’s applications before providing an overview of the language’s key programming concepts and practices. We went over assigning variables, declaring and leveraging functions, navigating control flow and working with data types.

If you enjoyed learning about Python, consider taking the next step. Our course entitled Introduction to Python Programming will have you on your way to mastering the language and setting yourself up for success as a developer.

Start Learning

Stay updated on the latest in technology

"*" indicates required fields

Stay updated on the Latest in Tech

Be first to hear about special offers, news, and resources from Udacity

"*" indicates required fields