As a beginner Python coder, you’ll probably run into a lot of jargon. Learning the most common terms you’re likely to come into contact with will help with your learning journey. Here are ten of the most common phrases that you’ll run into when working with Python.
Variable
Variables are common in many programming languages. A variable is a term used to refer to a piece of data that you may need to use later. you’re able to think about itas a box that stores a value, such as a number, character, or word. In many other programming languages, variables have a defined type. So only one type of data can be stored in the box.
In Python, however, there’s no need to define a type. you’re able to just use the variable name and the initial value. Python automatically figures out what your variable type is supposed to be. For example:
This will give us an integer variable with 10 as the starting value. you’re able to define variables in any of the standard variable types, and there are even custom variable types you can use to create your own variables, but those are a bit above beginner level.
Function
One of the most powerful things in Python is the ability to reuse code. If you need to do the same thing multiple times in your program, you could easily copy-paste the code into where it has to go. However, if you change one of those copy-pasted bits, you’ll have to change all of them. It can be a time-consuming process and functions help to avoid that.
A function isa block of code that does a particular task. This is what a function looks like:
The “def” keyword tells the Python compiler that you’re writing a function, and the name that comes right after it is the name we’ll use to call the function. Calling a function is as simple as typing the function name and the brackets. Sometimes, functions will have variables within the brackets that we can pass the function to work on.
List
When you go to the grocery, you most likely have a list of things you want to get. In Python,lists are collections of a particular type of variable. You can think of them as a long line of numbered boxes, and each box holds a variable of the list’s type. You only need one variable name to store the list, and a number, which refers to which of the boxes in that variable we want.
Here’s how we define a list in Python:
Just like variables, we don’t need to tell Python the type of the list. It will automatically figure it out when we give it the list’s members. If we were to ask for thislist[1], we would get the result “apple” in response. Lists can be powerful things for organization and planning.
Dictionary
In the real world, a dictionary allows you to look up the meaning of a word. In Python, a dictionary is similar, but instead of having words and their meanings, they have a variable and another one associated with that variable. Here’s what a dictionary definition looks like in Python:
In this dictionary, we have a pair of strings as the variables stored in it. So, if we asked the variable for the value stored in the location [brand], we’d get “Ford” as the result. Dictionaries are a lot like lists, except that instead of numbers to access the boxes, we use labels.
Loop
Loops allow you to make your program execute a series of commands based on certain parameters. Python has two loop systems: the for loop and the while loop. Each of them have their own specific best-use cases, but in general, they can be condensed into this format:
Loops allow you to go through every element of a list or dictionary. They can also help you search these data structures if you need to.
Conditional Statements
Conditional statements (sometimes shortened to conditionals) are a bit like a decision tree. Let’s say you went to the store to get some eggs. You figure you could make a great omelet if you had milk as well. So, in your head, if you were running on Python would look like this:
In this piece of code, we can see how conditional statements work. The if-else statement is used to determine what we should do and is always evaluated as either true or false. In Python, there’s no delineation for where the conditional starts and ends, and the indentation of the block informs the compiler which instructions make up the executed block.
String
We mentioned strings before, but it’s something that beginners will run into a lot. A string isa list of characters in order. Strings are one of the core variable types in Python. You can define them by typing the string in quotes. There are even functions and methods you can use to access individual characters in your string.
In this code snippet, we defined a string as “computer” and then we set letter as the first character in computer. The letter variable would hold the character “c.”
Import
I covered code reusability before, and Import is a great example of that.When we built the expense tracker, we imported a library called tkinter to help us with a GUI. Import allows us to access all the functions of a particular module. So, for example, if we type this:
we’ll get all the data types and functions of the datetime module. There are literally hundreds of modules in Python that can help you build stuff, so knowing which module to use saves you half your coding.
Exception
We covered exceptions when we were talking abouthow to debug Python functions effectively. Exceptions are runtime errors, meaning that they happen while the program is running. Exceptions are, as the name suggests, when something unexpected happens. You’ll probably encounter exceptions throughout your Python coding career, and they’re not something to be afraid of. Once you know what you’re doing, they’re easy to deal with.
Class
Python isan object-oriented language, meaning that everything in the language can be considered an object. Classes areblueprints for developing objects. We can use the class keyword to define a class like this:
From this example, it’s easy to spot the elements of the class. Just as with other constructs, the indentation tells the compiler what is and isn’t a part of the class. Classes are the most powerful building blocks of programs, and they can be used to store custom data, and even house functions to work on that data internally.
There’s a Lot More to Learn
Python is an ever-evolving language, and these basic terms will help you understand some of what’s out there. However, these terms aren’t all you need to know to fully grasp the language. Knowing a few of the most common terms will help you decipher documentation a lot easier.