Python Syntax

Every python developer must know these syntaxes to freely navigate in python environment and write an efficient code that adheres to PEP-8 guidelines.

Image placeholder
Figure 1: Become a pro in python programming

List of top 10 most commonly used syntax in Python

1. Comment in python
'#' symbol is used for comment in python. The keyboard should is largely 'Ctrl + /', however idle uses 'Alt + 3' to comment.

x = 10 # this is a commment
2. Use of semicolon in python
Semicolon ';' is present in python programming with the same meaning, i.e, end of statement. But, in general \n or Return key should suffice.
x = 10
y = 20; # There is no error from ';' but it's simply not required.
3. Multiple assignment
It is possible to assign multiple variable to multiple values respectively in python.
x, y, z = 1, 2.3, "hello"
print(x, y, z)
4. Swapping
Multiple assignment makes swapping variable values very easy in python.
x, y = 1, 2
x, y = y, x
print(x, y)
5. Case sensitive
Python is case sensitive language and changes the case results into a new object.

6. Use of quotes in python
Quotes are used in python to define string objects. There are four categories of quotes that can be used:
  • A pair of single quotes ''
  • A pair of double quotes ""
  • A pair of triple single quotes ''''''
  • A pair of triple double quotes """"""
There is no difference in the string object simply due te use of quotes but a certian scenarios a specfic type of quotes might be preffered. Triple quotes are generally used for a multi-line string object. It is possible to change quotes while being consistant since python has no character type data, just string.
'a' == "a" == '''a''' == """a"""
7. Use of backslash
'\' can be prefixed before special characters to treat them as regular characters. Prefix 'r' (raw_string) can also be used in the same regard.
'I can\'t miss my programink python classes'
8. Implicit definition
Python is implicitly defined, i.e, we need not specify the variable type during declaration.
x = 123.5
9. type()
type() function can be used to determine the type of any given veriable or object.
10. Dynamic typing
Since the varible declaration in python is implicit, it can behave different in terms of data type at runtime. This feature is called dynamic typing or runtime polymorphism.
def add(a, b):
    return a+b

Share Post
Ram Kumar Poddar

Ram has worked as a Software Developer for Amazon Development Center India. He is regarded as one of the best Python trainers in India.

Share your opinion: