Python 🐍
In this section we will take a look at the syntax of Python Programming language and start with writing code.
Installation
Before we start, this would be a good time to have python installed in your computer. You can follow the steps on the official python website here.
Syntax
Python syntax is designed to be readable and easy to understand. Unlike languages such as C++ or Java, Python does not use braces {} to denote code blocks. Instead, it uses indentation (whitespace) to indicate code blocks.
Here’s a simple example of indentation:
def greet(): print("Hello, world!")
The line print("Hello, world!")
is indented.
Indentation Rules:
1) Indentation can be whitespaces (usually 4 spaces) or tab.
2) It is important to make sure you choose one type of indentation, use either whitespaces or tabs, never mix them or you will get an error.
3) You cannot use indents anywhere. Indentation can only be used when defining a block.
More about indentations
PEP 8 – Style Guide for Python Code | peps.python.org
Getting started to code.
The python program can be written in any text editor. Every python file should have the extension “.py”.
The print function
In Python, the print
function is used to display text on the screen.
Try writing the following code in the editor below:
print("Today is a good day!")
The print
function is used to print anything on the screen.
The screen
When we say, “print on the screen”, we mean that the text is displayed on the console. The console is a window that displays the output of the program.
Generally the text is printed to the “stdout” (standard output) of the program. The stdout is the default output stream of the program. Read more about standard streams.
Comments
You can write your own comments in your code by using the “#” (hash or pound) symbol. Comments are ignored by the interpreter and are used to explain code.
# This is a comment print("Comments do not affect code execution")
Try running the below code and see it for yourself.