Complete Overview of Basic Programming Concepts using Python (2 Part Series)
Hello there! This post covers the basic concepts of programming. The purpose of this article is to explain these concepts as simple as possible.
In this article, we'll use Python to explain and demonstrate the programming concepts using the IDLE (Integrated Development and Learning Environment). To write code you need to have Python 3 installed on your pc/laptop. To check whether Python 3 is installed, use the following command in your command prompt or terminal:
If you haven't installed Python yet, download and install it from the official site.
After installation, open the IDLE, and let's start writing some code!
In the Python shell, you can write your code and directly execute it. A simple and often used first code is
hello world. To print this statement we use the
print() function as in the example below:
>>> print("hello world") # Input hello world # Output
In the example above we typed the line
print("hello world") and get the printed output
Indentation is the whitespace at the beginning of the line. Indentation is used for better readability of your code in programming languages but in Python, the indentation has a very important function. In Python, it used to execute a piece of code that is part of that statement. Below is an example of the indentation.
if age > 21: print("You are too old.")
After the colon in the if-statement, there is an indentation in the line with the print function. The indentation is set up consistency and make sure the code is easy to read.
Comments are used to explain the code and are not executed as a code. Their purpose is to make the code easier to understand. To write comments in Python you have to write as the example below:
# This is a comment. print("This is a piece of code that will be executed.")
This is an example of a
single-line comment. You can, however, write more comment lines, as in the example below:
# This is a line. # This is also line. # This is a line that also exists.
This is called a
multi-line comment. There is another version to comment in Python using
string literals that are not assigned to a variable. Below an example:
""" This is a line. This is a line that also exists. """
Variables are containers to assign values to it. You can assign multiple things to a variable, such as text, numbers, booleans (True/false), and more. Below an example of variables:
>>> my_age = 22 >>> print(my_age) 22 >>> city_name = "Dubai" >>> print(city_name) Dubai
In this code example, you can assign a value to the variable and print the value of that variable. In Python there are rules when it comes to writing variables:
- The variable must start with a letter or an underscore.
- It cannot start with a number
- It can only contain letters, numbers, and underscores (_). Special symbols are not allowed.
- Variables are case-sensitive. This means that variables names, such as
MY_AGEare different to each other.
This concludes the ending of part 1 of this series. In the next episode, there will more explanation about Python concepts.
If you guys are interested and want to learn more, I made a YouTube series that explain the core concepts of Python. Check them out below!