A Quick Guide of Python Syntax

Hello Folks, Welcome, in this module, we are going to talk about Python Syntax, we have seen a lot about python and also, we are done with Python installation, if you don’t know about how this does refer to our previous modules.

So, let’s start reading this module.

Python Syntax

The Python language has some similarities with older languages like C, Java, and Perl, but there are some definite differences between the languages.

Python Syntax

The program written in Python is read by a parser. It is readable language. The syntax of Python Programming is a set of rules which defines how to write a program in Python.

First Program in Python

Let us see the very first program in Python and that is to print hello world in the output screen.

print(“Hello World!”)
#The output thrown by the above code is Hello World!

What is a Python Statement?

Python statements are simple instructions that are executed by a Python interpreter. For example, if you assign a value to a variable like a num = 1, this is a Python statement. Apart from this, python has if statements, for statements, while statements, etc.

Multi-line statement

The end of a particular statement in Python is marked by a newline character. But if we want to extend any statement over multiple lines, then for that we have line continuation character (\).

num = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9 + 10
print(num)
#The output of the code is: 55

This is called an explicit line continuation, we can do the same by giving the sentences or statements inside parentheses ( ), brackets [ ], and braces { }.

Let us implement the above example using parentheses.

num = (1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9 + 10)
print(num)
#The output of the code is: 55

This line continuation is called the implicit line continuation, i.e., using parentheses ( ). Same we can perform by using the square brackets and parentheses.

fruits = ['mango',
'orange',
'grapes']
print(fruits)
#The output of the code is: ['mango', 'orange', 'grapes']

We can also put multiple statements, like assigning value to a variable in a single line using semicolons.

b = 201; a = 101; c = 301
print(a)
print(b)
print(c)

#The output thrown by the above code is:
#101
#201
#301

What is Python Indentation?

In many programming languages like C, C++, and Java, we use curly braces { } to define a particular block of code, but we are not allowed to do the same in Python.

In Python, we use indentation instead of curly braces for defining a block of code. A code block such as the body of an if statement, loop or function body, etc. starts with the indentation and ends with the first unindented line, i.e., where the indentation breaks. The amount of indentation depends on the requirement, but it must be consistent throughout the particular block.

The indentation makes the code look good, neat, and clean. This is the reason that the Python program looks consistent and similar. You can ignore the indentation in line continuation, but it’s a good idea to use the indentation. It also helps in making it more readable.

Let’s see some examples of indentation.

num = 1
if num > 0 :
    print("Welcome to Use my notes")
    print("Welcome to the world of Programming")

#The output thrown by the above code is:
#Welcome to Use my notes
#Welcome to the world of Programming

The main point to be noted is that incorrect indentation may lead to Indentation Error. So, be careful while doing this.

What are Python Identifiers?

A Python Identifier is a name that is usually used to identify a variable, function, module, class, or other objects. An identifier starts with a to z or A to Z or an underscore ( _ ) followed by letters or zero, underscores, and digits (0 to 9).

You cannot use punctuation or special characters such as $, @, and % within identifiers. Python is a case-sensitive programming language which means uppercase letters and lowercase letters are considered different. For example, Computer and computer are two different identifiers In Python.

Let’s see some of the naming conventions for Python Identifiers.

  • If you start an identifier by giving a single underscore at the starting, then the identifier is considered private.
  • If you start an identifier by giving a double underscore at the starting, then the identifier is considered a strongly private identifier.
  • Always start the class names with an uppercase letter. All others can start with a lowercase.

What are Python Reserved Words?

Python-reserved words are unique words with special meanings that cannot be used as variable names, constants, or any other identifier names. They all are in lowercase letters only. Here is the list of the keywords that are present in Python.

print exec break
assert from or
not for pass
class finally and
continue global raise
try yield return
del import def
else in while
elif lambda with
except is if

What are Python Comments?

Comments are basically for describing the code to the reader, it is not executed by the Python interpreter. They are very important while writing our program. These are given basically for users and readers to understand.
In Python, “#” is used to give comments. It is a single-line comment.

print(“Welcome to use my notes!”) #This line is commented
#The output thrown by the above code is: Welcome to use my notes!

Multi-line comments

Suppose, you want to give comments in multiple lines, then how you can do this? One way to achieve this is to give # in all the lines.

#This is a first-line comment
#This is Second line comment
#This is Third line comment

Another way to do this is by using triple quotes, i.e., either “”” or ‘ ‘ ‘. These are used to give multi-line comments.

"""
This is a first-line comment
This is Second line comment
This is Third line comment
"""

In the above code, we have given 3 lines of comment which are categorized under multi-line comments.

I hope you all are very much excited to start your journey towards learning Python after reading this module. Stay connected with us, until the next module comes up. Wish you all the very best. Keep reading, keep enjoying.