5th Aug 2021 15 minutes read Python Terms Beginners Should Know – Part 1 Magdalena Wojtas python python basics python terms Have you ever tried learning Python? If it's still on your to-do list, why not start with the basic Python terms in this article? And if you've already taken your first steps into the Python world, this can be a handy checklist to verify your knowledge. To start, let's take a look at what Python is and why the IT world is so excited about it. What Is Python? Python is an open-source, general-purpose programming language – which means that you can use Python for almost everything. There are some obvious Python uses like web development (with the Django framework) and scientific programming (the language has numerous libraries to help with scientific calculations). But Python can also be used in much more surprising domains like air traffic control and game development. Even though Python was created in the early nineties – in 1991, to be precise – it's being used in many modern applications. It's one of the most recommended programming languages for data science and machine learning – two domains that have been trending in the last few years. All of this proves Python's great flexibility and versatility, as the language can be successfully used in domains that are very different from each other, like art and medicine. Why Everyone Loves Python So Much Python wins a lot of popularity contests. If there were an Oscar ceremony for programming languages, Python would be one of the brightest stars of the evening. It's been hailed as the best programming language to learn, the most loved technology, the most beginner-friendly programming language, and the most in-demand technology. And these opinions are coming from institutions like the prestigious Berkeley University and the popular job search site Indeed. In Stack Overflow's yearly worldwide developer surveys, Python always ranks high in categories like the most used, most loved, and most wanted technologies. What's more, Python developers are among the best-paid programmers. Why does everyone love Python? I could point out many reasons, but let's start with the basics: Python's syntax is highly readable and easy to learn. This is why Python is so beginner-friendly: at first glance, Python and general English are very much alike. Another reason for its popularity is that Python has numerous libraries for various fields of expertise. This language is extremely versatile; it can be used in pretty much every industry, from music and film to machine learning, physics, and medicine. Python can be used in all major platforms; you can code in Python on every major operating system. It's open source, so you don't have to pay a fee to use it in your commercial and non-commercial projects. Many renowned companies appreciate Python, including Mozilla, Dropbox, IBM, Disney, Spotify, Netflix, and even NASA. Developers love Python because it increases their productivity and is easy to debug. Whenever the Python interpreter finds an error, it will raise an exception or print a stack trace. This means it's hard to miss flaws in your code. If you want to know how Python improves daily work, take a look at this article on automating daily routine tasks with Python. Many programmers (beginners, especially) appreciate Python's large and active community. This community is famous across the IT industry and hosts numerous events around the globe, including conferences, workshops, and meetups. It's a very inclusive place; Python developers like to share their knowledge and skills with others. I experienced that myself when I started learning to code – I could take part in free Django framework workshops for beginners that were organized by the Warsaw Python community. It gave my motivation a great boost. Now that you know what makes Python so great, are you ready to start learning its basic terms? Then let's get started! Basic Python Terms The following terms are all important for Python learners, but many of them are not unique to Python. You encounter programs, variables, if statements, functions and loops in most programming languages. So, whatever language you choose to learn after Python, many of these terms will still be relevant. 1. Interpreter Python is an interpreted programming language. This means that it needs a different program (called an interpreter) to read and execute the source code. Interpreters run through each line of the program and execute all the commands on the fly. They also verify each line of code to ensure it's written correctly. If the interpreter encounters any errors in the code, it will show a message that includes the type of an error and the place in the code where it occurred. A Python interpreter is included when you install Python on your computer. You can use it by simply typing python in the shell. You'll know you're in interactive mode if you can see >>> at the beginning of the line. And you'll see a welcome message listing which version of Python you've installed. 2. Program A program is a set of instructions that a computer uses to perform a specific action. Sometimes it's compared to a recipe with variables as ingredients and functions as instructions. Applications (like your browser) are a type of program. 3. Variable 'Variable' is a crucial term in every programming language, not just Python. If you're already familiar with variables from another programming language, you'll know how to use them in Python. Basically, a variable is a place in computer memory with an assigned name. We need variables to store and retrieve data of different types (text, numbers, etc.). Creating a variable is called 'declaring the variable'. You name your variable and assign a value to it. There are some rules for naming variables: You can use letters, digits, and underscores ( _ ) in a variable name. Variable names are case sensitive: example, Example, and eXampLe are all different variables as far as Python is concerned. You can't start a variable name with a digit. You can't use a reserved word (i.e. def, if, else, False, True, None...) as a variable name. If you're not sure what words are reserved (i.e. Python uses them for certain functions), type help('keywords') into your shell. You assign a value to a variable using the equals sign ( = ). First comes the variable name, then the equals sign, then the value: favourite_food = 'ice cream!' There are two types of variables in Python: local and global variables. Global variables are declared outside a function – we'll explain what a function is later in the article – and can be used anywhere in the code, by anyone. A local variable is declared inside a function and can only be used in that function. If you want to know more about variables and data types in Python, check out this article. 4. List A list is a data type that can store a collection of values. These values can be accessed via indexing, which means we call them using their position on the list. Declaring a new list is very similar to declaring a variable: it needs a name and an equals sign. The difference is that you store list items in square brackets and separate them with commas: to_do_list = ['meet Anna', 'buy milk', 'finish latest blog post'] We can print the whole list easily, but we need to use an index to access any of the individual values. You do this by enclosing the index for a list item (i.e. its position in the list) in square brackets. In Python, an index starts with 0, not with 1. If we want to print the second element of the list, we need to use index 1: >>> print(to_do_list[1]) buy milk This can be tricky in the beginning, but some practical exercises will soon help you memorize that rule. Lists are mutable, which means you can add or remove values or change particular elements. A more in-depth approach to lists and other data structures like sets and tuples and a comparison of lists vs. arrays are already on our blog; check them out for more information. 5. Dictionary A Python dictionary is a data type that stores an unordered collection of data values. It's the only data type that consists of key-value pairs, not single values. Key-value pairs look like this: 'Key': 'Value' or 'Country': 'Capital city' To access dictionary values, we use keys rather than indexes. Because of that, dictionary keys are unique and immutable – they cannot be changed. The values stored in dictionaries can be of any data type, even including other dictionaries! Let's take a look at an example of a Python dictionary: capitals = {'Czechia':'Prague', 'Lithuania': 'Vilnius', 'France':'Paris', 'Germany':'Berlin', 'Italy':'Rome', 'Poland':'Warsaw'} To access one of the values, we need to write the chosen key in square brackets: >>> print(capitals['Italy']) Rome You can also edit dictionaries, i.e. by adding or removing values. If you want to know more, try our article about Python dictionaries. 6. Function A function is a reusable block of code that performs a certain action (or actions). They are used to accomplish the same tasks repeatedly. Instead of copy-pasting the same lines multiple places in your code, you should use a function. Functions also divide complex code into smaller parts, which make it far more readable and maintainable. Python has several useful built-in functions. To use a built-in function, you don't need to write all the code yourself; just call the function. For example, to call Python's help function, we write: >>> help('keywords') Notice that we've used 'keywords' as the function argument (inside the brackets). This tells Python to get the information about Python keywords. Not too bad, is it? Of course, you don't have to limit yourself to built-in functions. Let's create our own. To do so, we need the def keyword. This stands for "define" and lets Python know this is going to be a new function. Then we need a meaningful name, followed by some code (which is called the 'body' of the function). Sometimes, we may also include one or more arguments. The whole thing looks like this: def function_name(parameter): your code Take a careful look at the indentation – the body of the function is indented. Indentation tells the interpreter that this code belongs to this function. Also, note that after the end of the function body, we need to leave an empty line. There can be one, many, or no parameters in one function. In our function show_my_favourite_food, let's include the food parameter, like this: def show_my_favourite_food(food): print('My favourite food is ', food) The body of our function includes another built-in function, print(), which displays the results on the screen. Let's call the function now to see the result: show_my_favourite_food('fish and chips') And the output will look like this: My favourite food is fish and chips We've created our first Python function, but there's much more to functions in Python. If you want to know more, you can find a complete guide to defining Python functions on our blog. 7. print() print() is one of the most basic Python functions. Every Python developer learns to use it very soon, as it allows us to show the result of the code we've written. The print() function literally prints the result to the screen. To use (or call) this function, you simply type: print() Usually, you need some parameters inside the brackets; print() without any parameters will result in a blank line. You can print the value of a variable or the result of a function. Let's use the variable we've already created to show how the print() function works with a parameter: >>> favourite_food = 'ice cream!' >>> print(favourite_food) ice cream! You can also add some text to these values. Combining pieces of text is called 'string concatenation' because string is Python's data type for text. Here's how we concatenate strings inside print(): >>> print('My favourite food is ', food) My favourite food is ice cream! It's easy – just add a comma and the name of the variable whose value you want to print. Notice, though, that we've enclosed the string in single quotes: 'My favourite food is'. This lets Python know to treat this text as a string. 8. input() input() is another built-in Python function. It allows us to get input from the user by showing a prompt. Then this function returns the data inputted by the user in the string format. It is a string no matter if the user typed '42' or 'I like coffee`. We can store the input() value in a variable and then print it with the print() function. First, let's set up the variable that stores the user input. In the next line, we'll tell the computer to print the results alongside the string 'My name is'. Here's the code, followed by the result: name_input = input() print('My name is', name_input) Magdalena My name is Magdalena You can also add some prompt text to let the user know what they should type: name_input = input('Please type your name:') print('My name is', name_input) Please type your name: Magdalena My name is Magdalena 9. If Statements If statements are one of the most common control structures in Python and many other programming languages. Control structures control the flow of the program. A program can execute different code depending on the conditions you define in the if statement. This enables the compiler to decide if the given condition is met and to execute one action or another. If statements can be combined with mathematical operators, such as equality (==), greater than (>), less than (<) or not equal (!=) as well as the keywords or and and. In Python, a basic if statement syntax looks like: if <condition>: <statement> Every Python if statement ends with a colon. It's also important to remember about indentation: the condition block (represented by <statement> in the example above) is always indented. The condition block tells the interpreter what to do if the if condition is met. If the condition is not met, the interpreter will move to the next statement in the code. You can add multiple if statements. x = 3 if x < 5: print('x is very small') if x > 100: print('x is very big') if x > 5 and x < 100: print('x is in range between 5 and 100') We can also add an else keyword to our if statement, which will make it an if-else statement. The code inside the else option will execute when the condition in the if statement is not met. age = 22 if age =< 18: print("You can't buy alcohol yet.") else: print("You can buy alcohol.") There is also a third type of keyword related to conditions: elif, short for 'else if'. The difference between else and elif is that elif allows us to add an expression. It's a little bit more elegant than multiple if statements. Here's an example: age = 22 if age =< 18: print("You can't buy alcohol at all.") if age > 18 and age < 21: print("You can buy alcohol in Europe but not in the US.") else: print("You can buy alcohol everywhere.") 10. For Loop / While Loop Loops allow us to iterate over sequences like lists, dictionaries, sets, tuples, etc. A for loop lets you execute the same code for every element of a collection, which saves a lot of time and writing. It also keeps your code clean. Let's use the list below as a collection of data: to_do_list = ['meet Anna', 'buy milk', 'finish latest blog post'] for task in to_do_list: print(task) meet Anna buy milk finish latest blog post There are two keywords in a for loop: for and in. The for keyword is followed by the name we assign to a single item in the sequence (task, in our example). The in keyword is followed by the name of the sequence (to_do_list, in our example). Every for loop initializer section ends with a colon. The body of the for loop needs to be indented, just like in the if statements above. For a thorough introduction to for loops in Python, I recommend reading this article. It includes examples of iterating over different kinds of sequences, like dictionaries, sets, tuples, and strings. There is another type of loop: a while loop. It has a slightly different syntax than a for loop and it works differently. while loops execute their body only while the condition at the beginning of the loop is true. It will repeat that block of code until the condition is no longer true. Here's the general syntax: while <condition>: <block of code> Let's take a look at an example: maxValue = 10 i = 1 while i <= maxValue: print(i) i += 1 This function will print the i item and add 1 to it as many times as the condition i <= maxValue is true – i.e. that i is less than or equal to 10. We use while loops when we don't know the exact number of times a loop statement has to be executed. for loops are more common and can be used in most cases; you should only use a while loop if it's impossible to use a for loop. Ready for More Beginner-Friendly Python Terms? It's important to master the basics when learning any new programming language. Python is no different – if you don't understand basic Python terms, it will be very hard to dive deeper into programming. If you feel confused when faced with a lot of new terms, take a look at this technical dictionary for IT beginners. It'll facilitate your journey into coding. I hope you found the first part of our Python terms cheat sheet helpful. Check out Python Terms Beginners Should Know – Part 2, where we explain more Python terms! And if there are any other Python terms you find problematic, let us know! We'll try to include them in the next articles. Good luck and stay tuned for new terms! Tags: python python basics python terms