A Beginner’s Guide to Python
Written by Alex Fisher
Thumbnail & Banner Photo by James Harrison on Unsplash.com
Whether you’re studying in the Faculty of Science or you just need a science elective, you might be taking a computer science course sometime soon, specifically one that uses the ever-popular programming language Python. The introductory course CSCI 1227 Computer Programming and Problem Solving is often taught with Python, for example, as are CSCI 4472 Deep Learning for Computer Vision and PHYS 3210 Computational Methods in Physics.
You might be wondering, however, what exactly Python is and how you use it. In this article, we’ll be outlining the basics of Python and helping you get started using it. By having a foundational understanding of Python, you’ll be able to transfer those skills to help you get started with just about any programming language.
What is Python?
To put it simply, Python is a programming language—essentially a set of instructions that can be given to a computer. By writing these instructions in a certain order, also known as coding, you can tell your computer to perform various tasks for you. These tasks can range from anything as simple as displaying a single word on your screen to developing your own video game.
Learning a programming language might seem like a daunting task, but it’s easier than it sounds. Python is a high-level language, meaning that it’s designed to be easily understood by people. When you write code in a high-level language, you’re not writing in ones and zeros (also known as binary). You’re using familiar words and symbols, most of which you probably use every day. You can even format your code to an extent, personalizing it and making it easier for you to read and understand.
Why Use Python?
Python is one of the world’s most popular programming languages, especially amongst beginner programmers. This popularity makes it very well documented, and any question you might have about it has probably been answered by someone on online forums like Stack Exchange, making your own learning process that much easier. In addition, online tutorials and Python’s own documentation are accessible and easy to find.
There are many factors that contribute to Python’s popularity, but the most notable of these are its ease of learning, its immense community support, and its flexibility. The language can be used for everything from game design to web development and even data science. While it’s true that for many use cases, Python is not the most effective language you can use—with complex simulations, for example, a code written in Fortran will likely be much faster—it is nonetheless a strong language that will be fast enough and flexible enough for the majority of use cases.
What are IDEs?
The first piece you’ll need to start on your Python journey is an Integrated Development Environment, or IDE. This is essentially a suite of tools that have been put together to make coding easier, and they are often designed for specific types of development. Some common features of IDEs include autocomplete, debugging tools, syntax highlighting, and more. Spyder, for example, is an IDE with a feature set that focuses on science and data analytics, while PyCharm is more useful for app and web development. If you’re looking for an open-source alternative that’s multipurpose, however, you might be more interested in Visual Studio Code. Finally, Jupyter Notebook is a simple but versatile IDE that, unlike most other IDEs, can run right in your browser window, making it very easy to use. The IDE you use will depend on the exact needs of your course or project (keep in mind, though, that some courses may require you to use specific IDEs).
How Do You Install Python?
In order to code in Python, you’ll also need to have it installed on your computer. Fortunately, this is very easy to do. The Python website has a downloads page with links for just about any operating system you might want to use, including Windows, macOS, and Linux/Unix. From there, you can run the installer, click through it as you would any other, and you’re good to go. If you do need a bit more help, however, there is also a handy installation guide available online.
If you’re planning to use Python for data science, it’s worth considering Anaconda. Anaconda is based on the latest version of Python and comes with extra features and libraries that are designed to make your work more efficient and your life easier. When you install Anaconda, which is a very similar process to installing base Python, it even comes with Jupyter Notebook so that you can get started right away.
Now you should have a solid understanding of what Python is and how you can start using it, you’re almost ready to write your first program. In the second half of this article, you’ll learn a few important definitions and useful tools that will give you everything you need to start becoming a Python expert.
What are Libraries?
Libraries are collections of code and functions that you can use to massively reduce the amount of work you have to do. They can include simple and common functions that you’ll use in nearly every program, as well as advanced functions that you’ll use rarely but that may require a high level of coding knowledge to write on your own. For example, one commonly used library is Matplotlib, which can produce graphs and figures. So, rather than having to write a lengthy code that creates an image and follows mathematical formulae to generate points and lines, calculate uncertainties, and more, you can simply call a handful of functions that will take the data you provide them and do all that work for you. Python libraries can be added to your code using the “import” command followed by the name of the library, and many of the most common and most useful libraries are often included when you install Python or your chosen IDE.
What are Loops?
Loops are exactly what they sound like: they loop and repeat a set of instructions until you tell them to stop. Loops are one of the most versatile and commonly used tools in programming, and Python has two types of loops: for loops and while loops.
For loops iterate over a specific range or sequence, repeating the instructions in the loop (denoted by a colon followed by indented new lines) on each iteration. For example, if you wanted to construct a simple loop to count from 1 to 10.
Photo by Alex Fisher
This simple code would output the numbers 1 through 10. You may have noticed that the range used in this example was from 1 to 11, not from 1 to 10. This is because Python for loops are inclusive of the first number in a range, and exclusive of the last number in a range, so you may often want to add 1 to the last number in a range. This limitation does not apply to sequences of specific numbers, and other coding languages may have different conventions.
While loops continue to perform their instructions while a given condition is met, hence the name. For example, if you wanted to use a while loop to count from 1 to 10:
Photo by Alex Fisher
As you can see, this required more steps to complete the same task, and you had to define an iteration variable while that step was implicit in the for loop. While loops are better suited to situations where you don’t know the exact endpoint or when you’re checking if something is true or false.
Loops are an important part of most programming languages. While each language will have its own approach to using loops, they work very similarly most of the time. Understanding how to use loops will help you become a good programmer very quickly.
What are Comments?
In the previous section, you may have noticed a hashtag or pound sign before a brief explanation of what each line in the code does. These symbols, and the explanations that go with them, are called comments. Comments don’t tell your program to do anything, but they’re still an integral part of coding. They allow you, and anyone else who might read your code, to more easily understand what your code is actually doing. They can be especially useful for beginners because they allow you to see exactly what each part of the code accomplishes, furthering your overall understanding of coding.
Each programming language has its own way of denoting code, but in Python, it’s most often done with the hashtag symbol. If you need to include a longer stretch of comments, you can also do so by placing three quotation marks before and after the block of text. Comments also do not have to be in line with your code: you can place a comment before lines or blocks of code. The only difference in doing so is your own style and preference, and you may even find it useful to use comments both in-line with your code and right before large blocks of it.
What are If Statements?
If statements check against a given condition, and if that condition is met, they execute a set of instructions. If statements can be expanded to include else statements, which execute instructions if the original condition is not met. If you want to check a second condition after the first one fails, you can do this using elif (“else if”) statements, which again run only if the original condition is not met. If this sounds confusing, consider the example where you give the code a number or block of text and it checks if the value is less than or greater than 10, and if not, concludes that the number is 10.
Photo by Alex Fisher
As you can see, the if statement checks against the first condition, followed by the second condition, and only prints that the number is 10 if neither of the first two conditions is met. Note that the elif statement is before the else statement. This is because code is almost always read by the computer from the top down. Therefore, if you switched the order of the else and elif statements in the above example, the code would check the first condition, conclude that the number is not 10 if that condition wasn’t met, and then give you an error when trying to check the second condition.
It’s worth noting that if statements can easily slow down a code quite significantly. Checking the conditions of an if statement is slow, so if you have a lot of if statements in your code, then it will be far slower than it could be. It’s better to only use if statements when absolutely necessary.
What are Functions?
Python’s functions are blocks of code that must be “called” before they can do anything. To call a function, all you have to do is write the name of the function as though it were any other command, then type two closed parentheses immediately after its name. For functions that require you to give them information, this is done inside the parentheses when you call them. If a function is not called, the computer will not execute the code contained in it.
Putting together everything you’ve learned so far, you could create a function that checks if a number is under a certain value, then iterates up to that value, or else returns that the number is greater than that value.
Photo by Alex Fisher
In this example, the function is defined, and then it checks if the value of the number given to it is less than 10. If it is, it iterates the number up to 10 and prints the value each time (notice that the print statement has a block of text and a variable: you can combine as many elements as you would like into one print statement by separating them with commas). If the number is 10 or more, the function prints the value of the number. In both cases, the function returns the value of the num variable, whether or not any change was made. To use this function, the user specifies a new variable. They then call the function, sending it the new variable, and store the returned result in a second new variable. They can then use this result for whatever they need.
You might have noticed that this block of code actually runs out of sequence: the function is called after it is defined, and only runs then. It does not run before it is called. This is because, by calling the function, you’re actually telling the program that you want it to go look for something and then execute the code in that something. This is the same process as when you use functions from a library, which was mentioned earlier in this article.
Python’s functions are a versatile and very useful tool that allows you to run the same piece of code at any time and as many times as you need. If you have a calculation or instruction that you want to use several times, you do not need to write the same code over and over again: you can simply create a function that you call every time you have to do that thing. You can also define multiple functions in one program, and you can even create functions within functions. By getting used to functions, you’ll also build skills that can transfer to other programming languages: Java’s methods and Fortran’s subroutines are both very similar to Python’s functions, for example.
Now that you understand what Python is and some of its key capabilities, you’re ready to begin your coding journey! With loops, if statements, and functions, you can write many simple programs and even a few moderately complicated ones. Whether you’re starting a personal project or about to take a computer science course, you now have an advantage before you’ve even started. Even if you end up using a completely different programming language, many of the topics discussed in this article will transfer over easily, giving you a better understanding of that language from the moment you sit down to write your first program.
Have you used Python or any other programming language before? Are you looking to start a new project or take a computer science course? Share your thoughts on our social media pages, and be sure to check out our article Getting Started With Programming for some advice to find your motivation and keep your interest while learning.