Python Tutorial: Basic Syntax - How To Work With Python

Python Tutorial: Basic Syntax - How To Work With Python

Learn how to work with line indentation, multi-line statements, comments in Python, and multiple statements on a single line.

In my previous article, I wrote extensively on an Introduction to Python programming. The article covered a lot on what you need to know to get started with Python.

In today's article, you will learn how codes are formatted in Python. This article will cover concepts such as white space, line indentation, multi-line statements, comments in Python, and multiple statements on a single line. This article provides a brief overview of how Python code is formatted and is not intended to be a comprehensive guide to Python's syntax. Additional syntax concepts will be explained in future articles as they are used.

Python's Syntax

A language has a unique form in which it is written. This distinguishes it from other languages. This is what is referred to as syntax. Python has a set of rules that govern the way statements are written. While there may be some similarities between its syntax and that of other languages, there are also differences that makes it unique.

Let's take a look at this example

greeting = 'Hello, world'
print(greeting)

In the above example, I created a variable named greeting and assigned the string Hello, world to it.

You may or may not be familiar with what variables and strings are. If you have experience with other programming languages, you likely know what variables and strings are.

If you are completely new to programming, variables are used as containers to hold values, while a string is a sequence of characters enclosed within double or single quotes. More on these topics will be covered in future articles.

The second line outputs the contents of the variable.

If you have experience with other languages like C, you might expect this line of code to end with a semi-colon. That's not the case in Python.

Python does not require semi-colons at the end of statements. It operates on only whitespaces which makes it a very clean language to work with.

Comments

Let's take a look at the example below.

I've added a new line above the print statement which explains what the print statement will do.

greeting = 'Hello, world'

# Prints Hello, world

print(greeting)

What I did is what is referred to as commenting. Comments are mostly used to explain a line of code, functions, classes and so on. The way you add a comment to a line is by preceding the line with a pound sign (#). This tells python you're writing a comment. This line of code will be skipped when python is running your program.

The pound sign can be used on multiple lines to make a multi-line comment.

For example

def add(x, y):
    #Adds x and y
    #And prints the output
    z = x + y
    print(z)

Triple quotes can also be used to write comments, especially when defining functions, conditional statements, and classes

For example

def add(x, y):
"""Add x and y"""
    print(x + y)

Line indentation

To explain how indentation works, let's take a look at this piece of code

x = 3
y = 5
if x > 2:
    print(x)

You don't have to worry about how if statements works for now, the goal is to understand how indentation affects codes in Python.

Indentation in python is something you will always come across. For beginners and even experienced programmers, indentation is something you will most likely find frustrating when you're just getting started with Python. It's advisable to take note of this.

Notice in the code above that, on line 4, I added a little space before the print(x) statement.

Most of the time, you will hear people say indent, this is what they mean. I indented on the line 4, and what this does is that it tells python that the code is still a part of line 3. That is, this is a block of code.

Python does not support the use of braces to indicate blocks of code. So the way you write a block of code is by line indentation.

The way you indent code varies, you either hit the space bar four times or use the Tab key to make an indent.

Looking at our example, the greater than operator, >, checks if x is greater than 2. x will be printed only if x is greater than 2.

Now if you're wondering how this program works,

line 1, 3 is assigned to x,

Line 2, 5 is assigned to y

Line 3 checks if x is greater than 2, which is True, so x will be printed out.

Let's assume we had something like this

x = 3
y = 5
if x > 4:
    print(x)
print(y)

x will not be printed out, because it is not greater than 4. But the last line will be executed since it's not inside the block of code. 5 will be printed out.

Multi-line statements

All statements in python ends with a new line but there's a way to tell python that more than one line of code should be treated as one.

Let's say we have

x = 1
y = 2
z = 3
result = x + y z

The last line can be written as

result = x + \
         y + \
         z

The backslash is used to perform this task.

After running this program, 6 should be printed out.

Since we're writing a very small program, this is not so useful because everything looks okay on a single line.

Having this knowledge will become really useful by the time you start writing longer programs where you will need to split a line of code into multiple lines for better readability.

Multiple statements on a single line

The semicolon can be used to write multiple statements on a single line. There's an exception. The statements should not start a new block of code.

Let's say we have

x = 2
y = 5
print(x + y)

It can be also written as

x = 2; y = 5; print(x + y)

After running this program, 7 should be printed out.

However, it is not recommended to do this often as it can make the code less readable and harder to understand. It is better to write each statement on a separate line for better clarity and easier maintenance.

Final Thoughts

Python has a Syntax which allows for faster readability and clarity, and in this article, I have covered some of the essential concepts. I talked about white spaces, how indentation works, how to use comments, how to write multi-line statements and multiple statements on a single line. While this is just the tip of the iceberg, understanding these concepts is a crucial step in becoming a proficient Python programmer.

That's it for this article. I hope you find it helpful as much as I do.

If you have any question or you think there's something you want me to add to this article or upcoming ones, please drop them in the comment section below.

And If you like this article, please follow for more and don't forget to share with others who might find it helpful.

Finally, I'd like to connect with you on LinkedIn | Twitter | GitHub | YouTube.