Python basics
  • Tutorial
  • Exercices
  • About
  1. Fundamentals
  2. 0-Basic Syntax
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • First program
    • Interactive mode (local machine)
    • Script mode (local machine)
    • Google Colaboratory
  • Syntax
    • Indentation
    • Comments
      • String line comments
      • Multilines comments
  1. Fundamentals
  2. 0-Basic Syntax

0-Basic Syntax

Notebook

Link to the Google Colaboratory notebook: Basic Syntax

First program

We will try to execute the well-known Hello World program.

Interactive mode (local machine)

You can into interactive mode by using a terminal and run the command python.

Example of output
Python 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> denotes a Python Command Prompt where you can enter Python commands.

Let’s write print("Hello World !") and press Enter

Output
Hello World !

Script mode (local machine)

Instead of writing a code line by line, we can write everything in a file and then run it line by line using command line.

Let’s create a file name test.py with the following content:

print("Hello World !") 

You can now execute the code using python test.py in a terminal.

Google Colaboratory

We will execute this code in a Google Colaboratory notebook.

Code link

The Google Colaboratory is accessible in the following link: Basic Syntax

Syntax

Indentation

Python programs don’t contain braces to indicate the end of a line.

Block are denotated by line indentation, and the lines that are indentated form a block of code.

if True:
    print("Hello")
    print("World !")
else:
    print("Goodbye")
Hello
World !

Comments

Comments can be used to explain the code.

These are lines than won’t be executed.

String line comments

You can comment a line using # at the beginning of a line:

# Print Hello world
print("Hello World !")

It can also be place at the end of a line:

print("Hello World !") # print a message
Note

Comments can be line of codes that you just don’t want to execute for a moment.

# print("Test")
print("Hello World !")

Multilines comments

Sometimes, it can be usefull to have multiple lines with comments.

You can use # that will be place at the beginning of each line.

# This command will print a message.
# It is accessible to every user running 
# the code
print("Hello World !")

You can also use multiline comments.

You can add three quotes """ at the beginning of your comments and end it with """ again.

"""
This command will print a message.
It is accessible to every user running
the code
"""
print("Hello World !")
Python
1-Variables
 

Copyright 2023, Clément Bernard