Syntax, Indentation, and Comments
Table of Contents
It is fair to say that Python's syntax is unlike most other programming languages. Some of its keywords are rarely seen elsewhere, and indentation is critically important in Python whereas for almost all other languages, this does not matter.
Indentation
Indentation is how you denote a new code block in Python. here's a basic example:
PYTHONif True:
print("True")
else:
print("False")
BASHTrue
No curly braces needed! This approach has its pros and cons but that is how Python works. It figures out what code belongs in what code block judging by it's indentation level.
Here are examples of broken syntax in Python:
PYTHONif True:
print("True")
else:
print("False")
PYTHONif True:
print("True")
print("True")
print("True")
For all the same reasons, code in Python needs to be perfectly indented or else it will not run.
Comments
At some point down the road, you will want to leave comments in your code, either for yourself in the future, or for another developer working on the same project as you. Here is how comments in Python look like:
PYTHON# First comment
print("Hello world!") # second comment
BASHHello world!
Fairly straightforward! Comments in Python start with the #
character and the following is the comment itself. Unlike most programming languages, Python does not support multi-line comments. However, nothing technically stops you from doing this:
PYTHON# This
# is
# a
# multi-line
# comment
print("Hello world!")
BASHHello world!
This works but keep in mind that comments in general should be short anyway, so using multi-line comments should be done sparingly, if at all.
- Getting Started with Solid
- Create an RSS Reader in Node
- How to Set Up Cron Jobs in Linux
- How to deploy a PHP app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React
- Getting Started with Vuex: Managing State in Vue
- Setting Up a Local Web Server using Node.js
- Using Axios to Pull Data from a REST API