Every python developer must know these syntaxes to freely navigate in the 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
2. Use of semicolon in python
Semicolon ';' is present in python programming with the same meaning, i.e, end of the statement. But, in general \n or Return key should suffice.
x = 10
y = 20
3. Multiple assignments
It is possible to assign multiple variables to multiple values respectively in python.
x, y, z = 1, 2.3, "hello"
print(x, y, z)
Multiple assignments makes swapping variable values very easy in python.
x, y = 1, 2
x, y = y, x
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 to te the use of quotes but in certain scenarios, a specific type of quotes might be preferred. Triple quotes are generally used for a multi-line string object. It is possible to change quotes while being consistent 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
type() function can be used to determine the type of any given variable or object.
10. Dynamic typing
Since the variable declaration in python is implicit, it can behave differently in terms of data type at runtime. This feature is called dynamic typing or runtime polymorphism.
def add(a, b):