popolo - code simple..

popolo - code simple..

Python Basics - Getting Started with Environment

Python Basics - Getting Started with Environment

Theodoros Kafantaris's photo
Theodoros Kafantaris
·Apr 20, 2022·

3 min read

Table of contents

  • Setting up our programming environment
  • Python Syntax
  • Python Comments

In this article we will prepare our environment to be ready to run our Python code. We will install python in our laptop and will use a text editor for our code. Text editors recognise Python code and highlight sections as we write, making it easy to understand our code’s structure. Also we will see basic python syntax(especially indentation) and last we will see how to put comments in our code to explain our code better.

Setting up our programming environment

For MacOS, python is coming already installed in our system.

For Windows, we can follow the following guide.

Download Python Executable Installer

  1. Open your web browser and navigate to the Downloads for Windows section of the official Python website.

  2. Search for your desired version of Python.

  3. Select a link to download either the Windows x86-64 executable installer or Windows x86 executable installer.

image.png

Run Executable Installer

image.png

image.png

Verify Python Was Installed On Windows

image.png

In order to check which version is installed in MacOS, we can run:

(venv) (base) thkafantaris@PN-MAC-tkaf blog % python -V
Python 3.8.8

Now, we can start Python's interpreter running the following:

venv) (base) thkafantaris@PN-MAC-tkaf blog % python
Python 3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

and send our first line of Python code:

>>> print("Hello Python. Nice to meet you!")
Hello Python. Nice to meet you!

We can exit from python interpreter running the follow command:

>>> exit()
(venv) (base) thkafantaris@PN-MAC-tkaf blog %

In this series, we will use VS Code as our default text editor. We can find in the below link how to install VS Code in Windows, MacOS or Linux.

Install VS Code

If everything is fine with installation, we can see this application.

image.png

Python is an interpreted programming language, this means that as a developer we write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.

The way to run a python file is like this on the command line:

(venv) (base) thkafantaris@PN-MAC-tkaf Projects % python myprogram.py 
Hello Python. Nice to meet you!

Where myprogram.py is the name of our python file.

Python Syntax

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.

Example

It works!

>>> if 5 > 2:
...   print("Five is greater than two!")
... 
Five is greater than two!

Python will give us an error if we skip the indentation:

It throws an error!

>>> if 5 > 2:
... print("Five is greater than two!")
  File "<stdin>", line 2
    print("Five is greater than two!")
    ^
IndentationError: expected an indented block

The number of spaces is up to us as programmers, the most common use is four, but it has to be at least one.

It works!

>>> if 5 > 2:
...   print("Five is greater than two!")
... 
Five is greater than two!

It throws an error!

>>> if 5 > 2:
...  print("Five is greater than two!")
...         print("Five is greater than two!")
  File "<stdin>", line 3
    print("Five is greater than two!")
    ^
IndentationError: unexpected indent

Python Comments

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

>>> #This is a comment
>>> print("Hello, Python!")
Hello, Python!
 
Share this