Every python developer must know these syntaxes to freely navigate in python environment and write an efficient code that adheres to PEP-8 guidelines.
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
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.
It is possible to assign multiple variable to multiple values respectively in python.
x, y, z = 1, 2.3, "hello" print(x, y, z)
Multiple assignment makes swapping variable values very easy in python.
x, y = 1, 2 x, y = y, x print(x, y)
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 """"""
'a' == "a" == '''a''' == """a"""
'\' 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'
Python is implicitly defined, i.e, we need not specify the variable type during declaration.
x = 123.5
type() function can be used to determine the type of any given veriable or object.
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
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.