0% found this document useful (0 votes)
105 views

Python Syntax

Python syntax defines the rules and structures used to write programs in Python. It includes line structures, multiline statements using backslashes or triple quotes, comments denoted by #, docstrings delimited by triple double quotes, mandatory indentation to delimit blocks of code, multiple statements per line separated by semicolons, quotation rules, treatment of blank lines, keywords, and identifier naming conventions.

Uploaded by

Ashu Negi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Python Syntax

Python syntax defines the rules and structures used to write programs in Python. It includes line structures, multiline statements using backslashes or triple quotes, comments denoted by #, docstrings delimited by triple double quotes, mandatory indentation to delimit blocks of code, multiple statements per line separated by semicolons, quotation rules, treatment of blank lines, keywords, and identifier naming conventions.

Uploaded by

Ashu Negi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Python Syntax

What is Python Syntax?


 Term syntax is referred to a set of rules and principles that describes the structure of
a language
 The Python syntax defines all the set of rules that are used to create sentences in
Python programming
Types of Syntax Structures in Python
Python Line Structure
A Python program comprises logical lines. A NEWLINE token follows each of
those. The interpreter ignores blank lines.
The following line causes an error.

Output:
SyntaxError: EOL while scanning string literal
Python Multiline Statements
We saw that Python does not mandate semicolons.
A new line means a new statement.
want to split a statement over two or more lines.
It may be to aid readability.
can do so in the following ways

 Use a backward slash

 Put the String in Triple Quotes


Use a backward slash
Put the String in Triple Quotes
Python Comments
Python Syntax ‘Comments’ let you store tags at the right places in the code
use them to explain complex sections of code
interpreter ignores comments
Declare a comment using an octothorpe (#)
>>> #This is a comment
Python does not support general multiline comments like Java or C++
Python Docstrings
 A docstring is a documentation string
unlike comments, they are more specific
they are retained at runtime
Delimit a docstring using three double-quotes ( “ “ “ )
Python Indentation
 Since Python doesn’t use curly braces to delimit blocks of code, this Python Syntax is
mandatory.
 You can indent code under a function, loop, or class.

 You can indent using a number of tabs or spaces, or a combination of those. But remember,
indent statements under one block of code with the same amount of tabs and spaces.
Python Multiple Statements in One Line
 You can also fit in more than one statement on one line.
 Do this by separating them with a semicolon.
Python Quotations
Python supports the single quote and the double quote for string literals
 But if you begin a string with a single quote, you must end it with a single quote.
The same goes for double-quotes.
Python Blank Lines
If you leave a line with just whitespace, the interpreter will ignore it.
Python Keywords
These are reserved words and you cannot use them as constant or variable or any
other identifier names. All the Python keywords contain lowercase letters only.
Python Identifiers
An identifier is a name of a program element, and it is user-defined. This Python
Syntax uniquely identifies the element. There are some rules to follow while
choosing an identifier:

1. An identifier may only begin with A-Z, a-z, or an underscore(_).


2. This may be followed by letters, digits, and underscores- zero or more.
3. Python is case-sensitive. Name and name are two different identifiers.
4. A reserved keyword may not be used as an identifier.
Python Identifiers
Apart from these rules, there are a few naming conventions that you should follow
while using this Python syntax:

1. Use uppercase initials for class names, lowercase for all others.
2. Name a private identifier with a leading underscore ( _username)
3. Name a strongly private identifier with two leading underscores ( __password)
4. Special identifiers by Python end with two leading underscores.

You might also like