Python Syntax | The Best Tutorial to Learn Python Syntax

1. Python Syntax: Objective

Previously, we saw what is Python and how to set up a Python environment on your computer. The code in any language must follow a set of rules. Today, we will learn about the Python syntax. In doing so, you will see what is Python Syntax and how it is different to Java and C++. After this lesson, you will be able to identify and debug beginner Python syntax.

So, let’s start the Python Syntax Tutorial.

Python Syntax Semantic

Python Syntax | The Best Tutorial to Learn Python Syntax

Read: Python Built-In Functions with Syntax and Examples

2. Introduction to Python Syntax

Let us see various basic python syntax that is used while doing programming in Python.

3. 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.

How are you?")

If you face any doubt anywhere in the Python Syntax Tutorial, Please Comment.

Read: Python Function Arguments with Types, Syntax, and Examples

4. Python Multiline Statements

This one is an important Python Syntax

We saw that Python does not mandate semicolons. A new line means a new statement. But sometimes, you may want to split a statement over two or more lines. It may be to aid readability. You can do so in the following ways.

a. Use a backward slash

how are you?")

You can also use it to distribute a statement without a string across lines.


b. Put the string in triple quotes

       how are you?""")

However, you can’t use backslashes inside a docstring for statements that aren’t a string.


#This causes an error

Read: Python Functions with Syntax and Examples

5. Python Comments

Python Syntax ‘Comments’ let you store tags at the right places in the code. You can use them to explain complex sections of code. The interpreter ignores comments. Declare a comment using an octothorpe (#).

#This is a comment

Python does not support general multiline comments like Java or C++.

6. Python Docstrings

A docstring is a documentation string. Like a comment, this Python Syntax is used to explain code. But unlike comments, they are more specific. Also, they are retained at runtime. This way, the programmer can inspect them at runtime. Delimit a docstring using three double quotes. You may put it as a function’s first line to describe it.

def func():
		This function prints out a greeting

Any query yet in Python Syntax Tutorial? Please Comment.

Read: Python Namespace and Variable Scope – Local and Global Variables

7. 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.

if 2>1:
      print("2 is the bigger person");
      print("But 1 is worthy too");

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.

if 2>1:
     print("2 is the bigger person");
   print("But 1 is worthy too");

#This causes a syntax error: “unindent does not match any outer indentation level”

8. 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. But you’d only want to do so if it supplements readability.


Read: Python Decision Making Statements with Syntax and Examples

9. 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.

The following string is delimited by single quotes.

print('We need a chaperone');

This string is delimited by double quotes.

print("We need a 'chaperone'");

Notice how we used single quotes around the word chaperone in the string? If we used double quotes everywhere, the string would terminate prematurely.

print("We need a "chaperone"");

10. Python Blank Lines

If you leave a line with just whitespace, the interpreter will ignore it.

Read: Bitwise Operator in Python with Syntax and Example

Let us see next python syntax of Identifiers.

11. 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. The following is a list of keywords.

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.

Read: Python Operators with Syntax and Examples

12. Python Variables

In Python, you don’t define the type of the variable. It is assumed on the basis of the value it holds.


Here, we declared a variable x, and assigned it a value of 10. Then we printed its value. Next, we assigned it the value ‘Hello’, and printed it out. So, we see, a variable can hold any type of value at a later instant. Hence, Python is a dynamically-typed language.

Read: Python Strings with String Functions and String Operations

Python Interview Questions

13. Python String Formatters

Let us see next python syntax String formatters.

a. % Operator

You can use the % operator to format a string to contain text as well as values of identifiers. Use %s where you want a value to appear. After the string, put a % operator and mention the identifiers in parameters.

x=10;  printer="HP"
print("I just printed %s pages to the printer %s" % (x, printer))

b. format method

The format method allows you to format a string in a similar way. At the places, you want to put values, put 0,1,2,.. in curly braces. Call the format method on the string and mention the identifiers in the parameters.

print("I just printed {0} pages to the printer {1}".format(x, printer))

You can also use the method to print out identifiers that match certain values.

print("I  just printed {x} pages to the printer {printer}".format(x=7, printer='HP'))

c. f-strings

If you use an f-string, you just need to mention the identifiers in curly braces. Also, write ‘f’ right before the string, but outside the quotes used.

print(f"I just printed {x} pages to the printer {printer}")

So, this was all about the Python Syntax tutorial. Hope you like our explanation

Read: Python Comment, Indentation and Statement

14. Python Syntax – Conclusion

In this Python Syntax tutorial, we learned about the basic Python syntax. We learned about its line structure, multiline statements, comments and docstrings, indentation, and quotations. We also learned about blank lines, identifiers, variables, multiple statements in one line, and string formatters. In the next lesson, we will look at different variable types in Python.

If you have any query regarding the Python Syntax Tutorial, please drop a comment.

See also – 

Python Operators and Syntax

For reference

24 Responses

  1. Racks says:

    Very nice article for python beginners. I like your blog design and your presentation.

    • Data Flair says:

      Thank you, for such nice words for our “Python Syntax Tutorial”. Feel free to share this blog with Python beginners to understand and Learn Python Programming.

  2. Venugopal says:

    I have never seen these type of Python tutorial anywhere. I hope this Python syntax tutorial will be helpful for python beginners. The Python Syntax Examples were of great use.
    Keep rocking…

  3. Vasantharaj says:

    Great effort and well organised

    • Data Flair says:

      Thanks for being a loyal reader of Python Syntax Tutorial, your comments keep us Motivated to bring you nothing but the best.
      Keep Learning….. Keep Visiting Data-Flair.

  4. Shubham Panchal says:

    In Indentation part, both the parts of code seems same to me
    What’s the difference there ?

  5. hieudoanitus says:

    Series of material in that site is good. I’ve already read many tutorial sites but this site is very clear and comprehensive. Thanks your team.

    • Data Flair says:

      Hii Hieudoanitus,
      Thank you for giving such a fab review on Python Syntax. Hope you have checked our Python Tutorial. If not then we recommend you to check that. Also, you can check more new Python articles and if there is any topic which you want to learn with us please let us know. We will be happy to help you.
      Try out latest Python blog

  6. Pooja tomar says:

    I want to know will you people provide any online python course in which you will provide us a certificate of the course

    • Data Flair says:

      Hi Pooja
      As you ask for the online Python Course, currently we don’t have such a course. Soon, we will start the Python online course with Video Tutorials. Till then you can take knowledge from our published Python material. This will guide to learn Python easily.
      Thank you for visiting Data Flair.

  7. Rishi says:

    Thanks for such a wonderful tutorial, its very easy to learn here. But from few days now I am unable to get the content tree page to jump on to a particular section. It was working fine before, but now i am unable to proceed to the topics, Can you please check?

    • DataFlair Team says:

      Hi Rishi,
      Thanks for the comment on Python syntax tutorial. The sidebar which was on the under construction mode. Now, it is working fine. You can refer and explore more about Python programming Language.
      Data Flair

  8. Hiren says:

    Hi, great tutorial about Python…
    Will you please describe f-string with example??

    • RahulD says:

      f-string didn’t work in case .
      >>> print(f” I just printed {x} pages on printer {printer}”)
      SyntaxError: invalid syntax

      x=10; printer=”HP”

      • sahastra says:

        even I got same error.
        what’s the syntax

        • DataFlair Team says:

          Hi Sahastra,
          Thanks for the feedback, we have corrected our mistake. Now, you can learn Python Syntax easily with all the exmaple.

      • DataFlair Team says:

        Hello RahulD,
        Thanks for pointing out, it was our typo mistake in Python Syntax Tutorial. We have made the necessary changes. Now, you can run all the programs.
        Keep learning and keep exploring DataFlair

    • DataFlair Team says:

      Hey there, Hiren,
      Thanks for interacting through Python Syntax Tutorial. Sometimes, you may want to embed the values of variables in the middle of the strings. To do that, you can precede a string with the character ‘f’. Within the string then, you can surround variables names in curly braces {}. Here’s an example:

      >>> a,b=2,3
      >>> print(f””{a}+{b}={a+b}””)

      This piece of code assigns values 2 and 3 to the variables a and b, then prints their sum.
      Hope, it will help you!
      Regards DataFlair

  9. Jeelani says:

    print(“I just printed {x} pages to the printer {printer}”.format(x=7, printer=’HP’))

    • DataFlair Team says:

      Hi Jeelani,
      We are glad our readers trying to interact with us, through there valuable feedbacks. We have updated our Python Syntax tutorial, all the programmes and syntax were reviewed by our Python expert. Now onwards, you will not find any difficulties.

  10. Brian Kamanga says:

    Great introduction, thank you! I couldn’t get around running the function though, but I believe I will find it in the tutorials as I go on.

    • DataFlair Team says:

      Hi Brian,
      Thanks for referring to our Python Syntax tutorial. You did not fail in making a function; this is just inspiration to practice ahead. So, keep exploring and keep practising Python programs from DataFlair.

Leave a Reply

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