Python Syntax

python-syntax

Python Syntax – Python Coding with Examples

Python was explicitly intended to be a profoundly readable language The Python Syntax is made for convenience and have made it one of the most utilized language among freshers just as experts. Python punctuation is English like which makes it a lot easier to write, read and understand a python script as compared to a similar script written in other dialects, for example, C or Java.

A punctuation of any language refers to the structure of that language which incorporates a certain arrangement of rules, emotional to that particular language. The Python punctuation additionally comprises a lot of rules and components that altogether make up the Python Syntax.

Following is the rundown of themes that we will cover in this module, in the event that you need to hop to a particular one.

  • Python line structure
  • Python Multiline Statements
  • Whitespaces and Indentations
  • Identifiers
  • Reserved words
  • Python Quotation

Python Line Structure

Python coding style comprises physical lines just as sensible lines or proclamations. A physical line in a Python program is a succession of characters, and the stopping point terminates the line arrangement instead of some other dialects, for example, C and C++ where a semi-colon is utilized to mark the finish of the announcement. An intelligent line, then again, is made out of one or more physical lines. The utilization of a semi-colon isn’t prohibited in Python, in spite of the fact that it’s not mandatory. The NEWLINE token signifies the finish of the consistent line.

Python Code Example:

Variable = 6

NOTE:  A logical line that only contains spaces, comments, or tabs are called blank lines and they are ignored by the interpreter.

Python Multiline Statements

As we saw that in Python, another line basically implies that another announcement has started. In spite of the fact that, Python provides an approach to split an announcement into a multiline explanation or to join various proclamations into one legitimate line. This can be useful to increase the readability of the announcement. Following are the two different ways to split a line into two or more lines:

Explicit Line Joining

In explicit line joining, we use a backward slash to split a statement into a multiline statement.
Example:

print ("this\
is a python coding example")

We can also use explicit line joining to join two or more physical lines into a logical line, using back slash.

Implicit Line Joining

Statements that reside inside [], {}, or () parentheses can be broken down into two or more physical lines without using a back slash.

Example:

months = [' January', 'February',
'March', 'April']

Multiple Statements on a Single Line

In Python, it is possible to club multiple statements in the same line using a semi-colon; however, most programmers do not consider this to be a good practice as it reduces the readability of the code.

Example:

a=10; b=30;
print(a); print(b);

Whitespaces and Indentation

Unlike most of the programming languages, Python uses indentation to mark a block of code. According to Python coding style guideline or PEP8, we should keep an indent size of four.

Most of the programming languages provide indentation for better code formatting and don’t enforce to have it. But in Python it is mandatory. This is why indentation is so crucial in Python.

For example, if we run the following code, we will get an error of ‘expected indentation’:

a=1

if a>0 :

print(“There is no indentation in this statement”)

Identifiers

Identifiers in Python are nothing but user-defined names to represent programmable entities like (Python Variables, Python Functions, Python Class, Python Modules), or any other objects. But, there are a few rules that we need to follow while defining an identifier. They are:

  • We can use a sequence of letters [lowercase (a to z) or uppercase (A to Z)], and we can also mix up digits (0 to 9) or an underscore (_) while defining an identifier.
  • We can’t use digits to begin an identifier’s name.
  • We should not use Reserved Keywords to define an identifier.
  • Other than underscore (_), we are not allowed to use any other special characters.
  • Even though python doc says that we can name an identifier with unlimited length, it is not entirely true. Using a large name (more than 79 chars) would lead to the violation of a rule set by the PEP8 standard.

Reserved Words

Reserved words are nothing but a set of special words, which are reserved by Python and have specific meanings. Remember that we are not allowed to use keywords as Variables in Python.

Reserved words in Python are case sensitive. Following table shows all these reserved words used in Python.

Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnon localwhile
anddelglobalnotwith
aseliforyield
assertelseimportpass
breakexceptinraise
Python Quotations

In Python, single quotes and double quotes, both are supported for strings. If we have started a string with a single quote, it is mandatory to end it with a single quote only. The same goes with double quotes.

Example:

print (' singles quoted string')
print ("double quoted string")

This brings us to the end of this module to Learn Python. We learned about the Python Syntax along with the Python basics Syntax and Python Coding. Next module highlights Python variables. See you there!