Python Syntax and Data types
Today we are going to see about Python Syntax and Data types which consist of the following
- Python Syntax
- Indentation in Python
- Best practices while writing Python program
- Data types (In build data types in Python)
Code Repo : https://git.io/JtUqQ
Slides : https://git.io/JtUZM
In case if you have missed our previous topics
- Python introduction in Tamil Part 1
- Python introduction in Tamil Part 2
- Writing your first python program and how to execute it?
Watch in youtube: https://youtu.be/ftaMRl8gdZ0
Syntax, Indentation and Best practice
Indentation is it really needed for Python?
Well to answer for it, Yes Its is very important for us to know about the indentation. because Python does not uses the curley braces ‘{‘ or ‘}’ for enclosing the statements. where as it used the indentations for it.
Key points to remember about Indentation
- Giving space or tab space at beginning of a line, is called indentation.
- In Python Indentation plays important role.
- It uses the space to determine the block of statements.
- The first line of Python code should not have indentation.(it will throw IndentationError)
Python coding best practices
Lets us see some best practices to be followed while writing Python, these best practices are followed so that your code does not end up having errors, easy to read and understand the code or the logic.
Python Indentation Rules
- We can’t split indentation into multiple lines using backslash.
- The first line of Python code can’t have indentation, it will throw IndentationError.
- You should avoid mixing tabs and white spaces to create indentation. It’s because text editors in Non-Unix systems behave differently and mixing them can cause wrong indentation.
- It is preferred to use white-spaces for indentation than the tab character.
- The best practice is to use 4 white-spaces for first indentation and then keep adding additional 4 white-spaces to increase the indentation.
We will have another exclusive video coming up on the topic “Python coding best practices”.
Save the above file in any folder with the name helloworld.py
.py is the file extension used to identify the file is Python script
Python Data types
In Python we have the following in-built data types.
Category | Data types | Notes/Description |
Text type | str | String type, which is enclosed with double quotes Example "Hello", "Python", "Programming" |
Numeric type | int, float, complex | numbers, decimal and real numbers |
Sequence/Collection type | list, tuple, range | Collections, group of items. |
Mapping type | dict | dictionary type which has key and value pair combination. Example: {"virus_id":20190101, "virus_name":"COVID19", "isSpreadable":True} |
Set type | set, frozenset | Similar to the list, just that it excludes any duplicates and it sorts the items using natural sorting order i.e., ascending order. |
Boolean type | bool | True or False its that simple. |
Binary type | bytes, bytearray, memoryview | File reading and writing we can use the bytes or bytearrays. memory view is to view the byte location in the memory. |
