Python – Basic Syntax

Basic Syntax

Python syntax can be executed directly in the Command Line in windows and terminal shell in linux systems. Let’s run python first program Hello World using Windows CMD.

python syntax

First program in Python using IDLE

IDLE is very simple editor for Python, that comes bundled with Python.

How to create Hello, World program in IDLE

Open IDLE in your system.

  • In recent versions of Windows, search for IDLE or find it in the apps that are present in your system.
  • On Unix-based (including MacOS) systems you can open it from the shell by typing $ idle python_file.py.

It will open a shell with options. In this shell, there is a prompt of three right angle brackets:

>>>

Now write the following code in the prompt.

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

Hit Enter.

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

Python Indentation

Indentation are the spaces at the beginning of a code line. 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.

>>> if 2>1:
... print("Example of indentation error")
File "<stdin>", line 2
print("All good")
^
IndentationError: expected an indented block

Here in above code, you need to add spaces after If statement to avoid indentation error.

Python Variables

In Python, to assign values you need to create variables. Python has no command for declaring a variable.

>>> x = 5
>>> y = "Hello, World!"
>>> print(x)
>>> print(y)

Multi-Line Statements

Statements in Python typically end with a new line. Python also allow the use of the line continuation character (\) so that line should continue in next line. For instance:

>>> total = item_one + \
         item_two + \
         item_three

Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For instance:

>>> days = ['Monday', 'Tuesday', 'Wednesday',
         'Thursday', 'Friday']

Quotation in Python

Python accepts single (‘), double (“) and triple (”’ or “””) quotes to denote string literals, as long as the same type of quote starts and ends the string.

The triple quotes can be used to span the string across multiple lines. For instance, all the following are legal:

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
                        made up of multiple lines and sentences."""

Previous : How to Create Virtual Environment in Python

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Python – Basic Syntax”

  1. Pingback: Python Comments

Leave a Comment