Python Tutorial

Data analysis skills are highly valuable in today’s competitive job market. With industries across the board incorporating data analysis into their activities, learning how to use data is becoming essential for many different types of careers. Whether you want to become a dedicated data analyst or enhance a career in HR, journalism, and more, learning to use data can help you reach your goals.

If you’re unfamiliar with Python programming, or you need a refresher, we’ve compiled this tutorial to explain the fundamentals of Python. Python is a programming language created by Dutch programmer Guido van Rossum back in 1991. Since then, it’s become the de facto language of data science and data analysis, and has become one of the most popular programming languages worldwide. Python is used for many different purposes, such as server-side or backend web development, mathematics, system scripting, and more. The language is used to create web applications, read and modify files, engage with Big Data, perform rapid prototyping, and more.

The popularity and usefulness of Python is underlined by how the language was made. Python was created to be an accessible, multipurpose language, geared towards high readability. This refers to the simplicity of the syntax; Python’s syntax, or the way it’s written, is clean, simple, and very similar to the English language. Python allows developers to write programs with fewer lines and less complexity, meaning it’s a breeze to both write and read compared to some other, heftier languages, like Java.

Because of its practicality, simplicity, and usefulness, Python is a great language to learn, especially if it’s your first one. If you have no experience with programming, never fear; stay curious, open-minded, and confident, and you’ll get the hang of it in a jiffy. Read on for a tutorial on Python that’ll help you master the challenges of the 21-Day Data Challenge. Part 1 will cover essentials like white space, comments, variables, and functions in Python.

Ready to jump in? Sign up for the Data Analytics or Data Science Porogram today to launch your data career.

Python and White Space

Readability and clean syntax are an important aspect of Python, and the way white space is used within Python programming is a key part of that. For many other languages, utilizing white space well while coding is useful for keeping your code readable and accessible. Within Python, it’s necessary, and the code won’t compute properly unless you use white space properly. Not only will this keep your code readable, it’ll also instill a habit for cleanness and simplicity in you if you move on to other languages.

Within Python, new lines are used to complete a command. Whereas in other programming languages it is typical to use braces to complete a command, Python uses new lines instead. There are some workarounds for this, but typically, commands are completed by using new lines.

The other important thing to know about Python’s use of white space is that the language relies on indentation to define scope. The scope of things like loops, classes, and functions are defined by using indentation at the beginning of the line. The amount of indented spaces you use is up to you, but Python requires at least one indent at the start of the line, or else the code won’t work. All this will make more sense to you when you begin coding, but keep these rules in mind!

Writing Comments

Though not part of the program itself, comments are an integral part of any programmer’s workflow. Comments are used in programming to explain what lines of code do, to leave notes for yourself, or to leave tips for other programmers. An accessible piece of programming will have comments throughout that concisely explain aspects of the code.

In Python, comments will be indicated by the pound sign (#) at the beginning of the line. The pound sign indicates to the interpreter that the line that follows is not part of the code, and should be skipped over by the interpreter and not taken as a piece of programming. If you encounter a line beginning with #, you’ll know that this is a comment left by the previous developer.

To use multi-line comments, there are a couple of different solutions. Each line could be begun with a new pound symbol. If this is undesirable for some reason, the block of text you want to be read as a comment can be wrapped with three quotation marks at the start and at the end. If you see a block of text wrapped by three quotation marks at either end, you should know that this, too, is intended to be a comment.

Declaring Variables

Variables are an important fundamental of any programming language. A variable is simply a unit that has a value assigned to it. Variables will be very useful as you embark on your programming journey, so it is important that you know how to create them, and what the rules around their creation are.

In Python, a variable is created when a value is assigned to it using the equal sign (=). What’s to the left of the equal sign is the name of the variable, and what’s to the right is the value. For example, a variable can be created simply by typing x = 7. The program will understand x as the name of the variable, and 7 as the value of it. Going forward, x would carry the value of 7 within it, when it is conjured as a variable.

There are some rules around the naming of variables within Python that you should know. Variable names are case-sensitive, so be mindful of your caps lock. Just because x = 7 doesn’t mean that X will carry the value of 7. Variable names must start with a letter or with the underscore sign, and cannot start with a number. They also can only contain letters, numbers, and underscores. x6 could be a variable name, but 6x could not. x_ could be a variable name, but x$ could not.

Because Python loves readability and conciseness, multiple variables can be assigned to multiple values in one line. For example, writing out:

a, b, c = 1, 2, 3 

assigns the value of 1 to the variable a, the value of 2 to the variable b, and the value of 3 to the variable c. Additionally, the same value can be assigned to multiple variables at once. Writing out:

a, b, c = 7 

assign the value of 7 to the variables a, b, and c.

Functions

Moving on from variables – what about functions? You’ll use functions a lot while coding. In programming, functions are localized, self-contained segments of code. These come in handy when you want to reuse a segment of code over and over again without writing it all out every time. There are functions that you write out on your own, and there are also functions that are built-in to the language itself. These built-in functions are known as core functions – one example is the print function.

When writing out a function on your own, it is declared with def. It is then named, and the scope of it is mapped out. When you want to reuse the function later on, all you need to do is call it with the name you assigned it. What you write within a function generally does not overlap with the things declared outside of it. For example, perhaps outside of a function you have assigned the value of 7 to x. But inside the function, you have assigned x the value of 5. These two value assignments will not step on each others toes, and the interpreter will understand that 5 is only the value x has within the function, and not outside of it. Variables declared outside of a function are known as global variables, while those declared within the function are known as local variables.

The Print Function

Let’s take a look at the print function as an example. Within programming, you’ll frequently use the print function in order to output variables to the console. The print function is a core Python function, thus it is already built-in and does not need to be written out and declared. To use it, all you need to do is write out print, and then whatever data you want to be output to the console. For example:

print(“I’m ready for the 21DDC!”) 
“I’m ready for the 21DDC!” 

You don’t have to write out everything you want to output to the console. The print function can be used alongside variables, to output the value of the variable or variables to the console. For example, if the variable x had been assigned the value “21DDC!” previously, writing out print(x) wouldn’t output “x” to the console. It would output “21DDC!” to the console.

While calling the print function, the plus character (+) can be used to combine variables and/or data. For example: declaring the following variables and then printing both would output the final sentence to the console.

x = “I’m ready”
y = “for the 21DDC!”
print(x + y) 
“I’m ready for the 21DDC!”

You’ve now learned some basic concepts that you’ll use regularly when working with Python and data. Part 2 of this blog will cover other Python essentials, including math and logic, data types, and resources for further learning.