Skip to content

Python Statements and Syntax – Symbols & Special Characters

  • by

Every programming language has different Statements and Syntax. But most programming languages have almost the same. Python also has Statements and Syntax, and that is common in a programming language but you must have known about it.

Python Statements and Syntax

Statement are some rules and certain symbols are used in program(code).

Let’s see the list of symbol and certain rule of it:-

  • Hash # Hashmark is used for comment.
  • New Line (\n) – A Standard line separator (one statement per line).
  • Backslash ( \ ) – For continues a line.
  • A semicolon ( ; ) – Used for joins two statements on a line.
  • Colon ( : ) – separates a header line from its suite.
  • Statements or code blocks.
  • code block delimited via indentation.
  • Python files organized as modules

Let’s see one by every thing about:-

Hash for Comments ( # )

Hash # mark is used for comments in python programming. Python’s comment statement starts with the pound sign or hash symbol (#). You can use any line for comment in the program. All characters following the # to the end of the line are ignored by the interpreter.

Python Comments Block Syntax | Multiline Comment with Examples

Read more about the comment: – Python Comments Syntax & Multiline Comment

See below example of comment statement and syntax:-

# Print “Hello, World!” to console
print("Hello, World!")

Backslash symbol For Continuation ( \ )

Python statements are delimited by one statement per line. if you have little knowledge about Python Programming, then you knew it (Python doesn’t need a semicolon at the end of statements, new present as a new statement).

Backslash symbol can use in python code to broken up a single line into multiple lines. You have to use a backslash symbol ( \ ) before a NEWLINE to continue the current statement onto the next line.

See below a example, how to use backslash in python statement.

minimum_speed_Bike = 0
minimum_speed_Bike = 100

if(minimum_speed_Bike > 0) and \
        (minimum_speed_Bike <= 100):
    print("Bike is running....")

Output: Bike is running….

But there is an exception, wherein python statements can be continued without the backslash. Here is some condition where one statement can continue without using a backslash, If enclosing operators are used:-

  • Parentheses ()
  • Square brackets []
  • Braces "[]", "()", "{}" or "<>
  • Triple quotes string '''Hello'''

Let's see the some examples:-

Hello = '''String 
 in next line without backslashes'''
mark = 70;

if (mark > 50
        and mark < 80):
    print('A Grade')

You should try it your self and let use know in comment section, with code.

Colon as Header of Statement Groups ( : )

A colon separates a header line from its suite (the group of code), such as ifwhile, def, and class, are those that require a header line.

See below image, showing an error. A PyCharm IDE itself shows error.

Colon as Header of Statement Groups

Check right code:-

mark = 70;

if (mark > 50 and mark < 80):
    print('A Grade')

Suites Delimited via Indentation

Python code at inner levels is indented via spaces (Same number of spaces). Each line of code statement is considered a different part, so indented lines starting at different positions or column numbers are not allowed. Otherwise, IDE will show syntax errors.

See below example, an example is the same as above only print line tab space has removed and the result is a syntax error in python.

Suites Delimited via Indentation Statements and Syntax

Multiple Statements on a Single Line ( ; )

The most useable symbol by the programmer is a semicolon. Almost every old programming language like Java, used it, after the one-line statement.

The semicolon is useful when you want multiple statements in a single line. It's not affecting other codes. But multiple statements in a single line means lesser readability of code. See below code:-

Multiple Statements on a Single Line

Modules - Block of code

Python modules can contain blocks of code to run, function declarations, etc. If you have large code and use it in different places in the application, then move some of the code out to another module.

To know more about modules and examples check this tutorial link - Python Modules | Import Custom and Builtin.

Do comment if you have any doubts and suggestions. This is all about the basics of Python Statements and Syntax. Let us know if you have more content to add to this chapter.

Leave a Reply

Your email address will not be published. Required fields are marked *