You are currently viewing Python syntax and rules

Python syntax and rules

Python syntax can be executed directly in the Command-Line or by creating a python file using the .py extension and running it in the Command-Line. In this article, we will see some important rules and syntax that we need to keep in mind while doing Python Programming.

General Guidelines

Few things to remember while writing programs in Python 

  1. Python is case-sensitive it means variable with name number and Number both are different.
  2. There is no statement terminator in python, means no semicolon; is required to terminate a statement.
    Ex: print(“hello”)
  3. If you want to write the two command in a single line then you can use a semicolon and write the two command in a single line.
    Ex: print(“hello”); print(“world”)
    output:
    hello
    world
  4. There are some reserved words in Python, you cannot use them as a variable or constant. Check out the complete list of Python reserved keywords here.

Python Identifiers

A Python identifier is a name given to variables, functions, modules, classes, or any other objects. In Python, an identifier must starts with a letter A to Z or a to z or an underscore (_) followed by more letters or numeric values.

Naming conventions for Python identifiers

  • Class names start with an uppercase letter.
  • All other identifiers start with a lowercase letter.
  • Private identifier starts with an underscore (_).
  • If an identifier starts with Two underscores,  it indicates a strongly private identifier.

Strings in Python

In Python, we can use

a.) single   to represent a string.

ex: name=’Demo’

b.) double “” to represent the string.

ex: name=”Demo”

c.) Triple “””  “”” to represent the string.

ex: name=”””Demo”””

Comment in python

# is used to comment the line in python. Where ever is found Python will treat rest of the line as a comment.

Example:

print ('hello world')

# print('Demo')

Indentation in Python

Indentation is the most important thing in python because python does not use the { }  to write the code block. Python uses indentation to indicate a block of code. The Indentation means one tab. If you skip the Indentation, Python will throw an error.

Ex: 

if(a<10):
print("hello")
else:
print("world")

 

Leave a Reply