DEV Community

Sharonah8
Sharonah8

Posted on

Demystifying Python basics.

Python is a very powerful programming language. It is general-purpose and can be used in data science, mathematics, web development, and other programming areas. It is also multi-platform which means it can run on any operating system: Windows, Linux, MacOS, among others. It is also open-source and free. The most amazing part is that despite it being multi-functional, it is easy to learn and therefore beginner-friendly. Let’s have a look at some Python basics for those who are looking to learn this language.
The first thing we will look at is the Python syntax. Syntax in general refers to the structure of a language or the basic rules it follows that makes that language unique. In Python, comments are marked by using a # sign before the actual comment. This applies to both single-line and multi-line comments. For example, if you wanted to write a comment that says a given piece of code multiplies even numbers, you would write it as #multiplies even numbers. Everything written after the pound sign (#) will be ignored by the interpreter.
One unique thing about Python that probably makes it easy for beginners is that you do not to terminate statements with any symbol. In languages like C, you have to terminate each statement using the semicolon (;) symbol. In Python however, you can just write statements without having to terminate.
Another very important unique thing is indentation. It is used to differentiate blocks of code. For example:
for i in range(100):
# indentation indicates code block
total += i
As you have noticed, there is a colon (:)right before the indented code. This applies in all blocks of code in Python. Before you indent a certain line or lines of code, ensure there is a colon at the end of the previous code. Indentation also brings uniformity and readability to your code. The number of spaces you use as a programmer depends entirely on you, but just make sure you are consistent in the number of spaces you decide.
Python variables are locations that store data values in memory. Python has no specific command for naming a variable. It is created as soon as you assign a value to it. For example:
x = 8
y = "Sharonah"
print(x)
print(y)
As shown above, we use the assignment operator (=) to assign a value to the variable. The variables declared do not necessarily have to be of any type. You are even able to change the type even after assigning values to the variable. You also have the option of assigning more than one value to a number of variables, as shown below:
a, b, c = "Hello", 1, 99

print (a)
print (b)
print (c)

We also have constants in Python. These are variables also but their values can’t be changed. Variables are given values in the format shown below:
Pi = 3.14

These are the basics of Python and are very important things that one should familiarize themselves in as they plan to start the Python journey.

Top comments (0)