What is python syntax and example?


Execute Python Syntax

As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:

>>> print("Hello, World!")
Hello, World!

Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

C:\Users\Your Name>python myfile.py


Python Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

Python will give you an error if you skip the indentation:

The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one.

Example

if 5 > 2:
 print("Five is greater than two!") 
if 5 > 2:
        print("Five is greater than two!") 

Try it Yourself »

You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:

Example

Syntax Error:

if 5 > 2:
 print("Five is greater than two!")
        print("Five is greater than two!")

Try it Yourself »



Python Variables

In Python, variables are created when you assign a value to it:

Python has no command for declaring a variable.

You will learn more about variables in the Python Variables chapter.


Comments

Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

Example

Comments in Python:

#This is a comment.
print("Hello, World!")

Try it Yourself »


Test Yourself With Exercises

Exercise:

Insert the missing part of the code below to output "Hello World".

Start the Exercise



Summary: in this tutorial, you’ll learn about the basic Python syntax so that you can get started with the Python language quickly.

Whitespace and indentation

If you’ve been working in other programming languages such as Java, C#, or C/C++, you know that these languages use semicolons (;) to separate the statements.

Python, however, uses whitespace and indentation to construct the code structure.

The following shows a snippet of Python code:

# define main function to print out something def main(): i = 1 max = 10 while (i < max): print(i) i = i + 1 # call function main main()

Code language: Python (python)

The meaning of the code isn’t important to you now. Please pay attention to the code structure instead.

At the end of each line, you don’t see any semicolon to terminate the statement. And the code uses indentation to format the code.

By using indentation and whitespace to organize the code, Python code gains the following advantages:

  • First, you’ll never miss the beginning or ending code of a block like in other programming languages such as Java or C#.
  • Second, the coding style is essentially uniform. If you have to maintain another developer’s code, that code looks the same as yours.
  • Third, the code is more readable and clear in comparison with other programming languages.

Comments

The comments are as important as the code because they describe why a piece of code was written.

When the Python interpreter executes the code, it ignores the comments.

In Python, a single line comment begins with a hash (#) symbol followed by the comment. For example:

# This is a single line comment in Python

Code language: Python (python)

And Python also support other kinds of comments.

Continuation of statements

Python uses a newline character to separate statements. It places each statement on one line.

However, a long statement can span multiple lines by using the backslash (\) character.

The following example illustrates how to use the backslash (\) character to continue a statement in the second line:

if (a == True) and (b == False) and \ (c == True): print("Continuation of statements")

Code language: Python (python)

Identifiers

Identifiers are names that identify variables, functions, modules, classes, and other objects in Python.

The name of an identifier needs to be a letter or underscore (_). The following characters can be alphanumeric or underscore.

Python identifiers are case-sensitive. For example, the counter and Counter are different identifiers.

In addition, you cannot use Python keywords for naming identifiers.

Keywords

Some words have special meanings in Python. They are called keywords.

The following shows the list of keywords in Python:

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

Code language: Python (python)

Python is a growing and evolving language. So its keywords will keep increasing and changing.

Python provides a special module for listing its keywords called keyword

To find the current keyword list, you use the following code:

import keyword print(keyword.kwlist)

Code language: Python (python)

String literals

Python uses single quotes ('), double quotes ("), triple single quotes (''') and triple-double quotes (""") to denote a string literal.

The string literal need to be sourounding with the same type of quotes. For eample, if you use a single quote to start a string literal, you need to use the same single quote to end it.

The following shows some examples of string literals:

s = 'This is a string' print(s) s = "Another string using double quotes" print(s) s = ''' string can span multiple line ''' print(s)

Code language: Python (python)

Summary

  • A Python statement ends with a newline character.
  • Python uses spaces and identation to organize its code structure.
  • Identifiers are names that identify variables, functions, modules, classes, etc. in Python.
  • Comments describe why the code works. They are ingored by the Python interpreter.
  • Use the single quote, double-quotes, tripple-quotes, or tripple double-quotes to denote

Did you find this tutorial helpful ?

What Is syntax with example in Python?

As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line: >>> print("Hello, World!") Hello, World!

What Is syntax of Python language?

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

Is there syntax in Python?

Here we'll begin to discuss the main features of Python's syntax. Syntax refers to the structure of the language (i.e., what constitutes a correctly-formed program). For the time being, we'll not focus on the semantics – the meaning of the words and symbols within the syntax – but will return to this at a later point.