Skip to main content

Learn python from Zero

credit to: https://www.programiz.com/python-programming/first-program

Day 1. About Python

Python is a cross-platform programming language, which means that it can run on multiple platforms like Windows, macOS, Linux, and has even been ported to the Java and .NET virtual machines. It is free and open-source.

How to get started with Python.

In this tutorial, you will learn how to install and run python on your computer. Once we do that we will also write our first python program.

The Easiest Way to Run Python

The easiest way to run Python is by using Thonny IDE.

The Thonny IDE comes with the latest version of Python bundled in it. So you don't have to install Python separately.

Follow the following steps to run Python on your computer.

  1. Download Thonny IDE.
  2. Run the installer to install Thonny on your computer.
  3. Go to: File > New. Then save the file with .py extension. For example, hello.pyexample.py, etc.
    You can give any name to the file. However, the file name should end with .py
  4. Write Python code in the file and save it.
    Run Python on your computer
    Running Python using Thonny IDE
  5. Then Go to Run > Run current script or simply click F5 to run it.

Install Python Separately

If you don't want to use Thonny, here's how you can install and run Python on your computer.

  1. Download the latest version of Python.
  2. Run the installer file and follow the steps to install Python
    During the install process, check Add Python to environment variables. This will add Python to environment variables, and you can run Python from any part of the computer.

    Also, you can choose the path where Python is installed.
    Install Python on your computer
    Installing Python on the computer

Once you finish the installation process, you can run Python.


1. Run Python in Immediate mode

Once Python is installed, typing python in the command line will invoke the interpreter in immediate mode. We can directly type in Python code, and press Enter to get the output.

Try typing in 1 + 1 and press enter. We get 2 as the output. This prompt can be used as a calculator. To exit this mode, type quit() and press enter.

Run Python in Immediate mode
Running Python on the Command Line

2. Run Python in the Integrated Development Environment (IDE)

We can use any text editing software to write a Python script file.

We just need to save it with the .py extension. But using an IDE can make our life a lot easier. IDE is a piece of software that provides useful features like code hinting, syntax highlighting and checking, file explorers, etc. to the programmer for application development.

By the way, when you install Python, an IDE named IDLE is also installed. You can use it to run Python on your computer. It's a decent IDE for beginners.

When you open IDLE, an interactive Python Shell is opened.

Python IDLE
Python IDLE

Now you can create a new file and save it with .py extension. For example, hello.py.

Write Python code in the file and save it. To run the file, go to Run > Run Module or simply click F5..

Run Python programs in IDLE

Running a Python program in IDLE   



Now that we have Python up and running, we can write our first Python program.

Congratulations! You just wrote your first program in Python




Day 2. Python Keywords and Identifiers

In this tutorial, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.).

Python Keywords

Keywords are the reserved words in Python.

We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.

In Python, keywords are case sensitive.

There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.

All the keywords except TrueFalse and None are in lowercase and they must be written as they are. The list of all the keywords is given below.

Falseawaitelseimportpass
Nonebreakexceptinraise
Trueclassfinallyisreturn
andcontinueforlambdatry
asdeffromnonlocalwhile
assertdelglobalnotwith
asyncelififoryield

Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.

If you want to have an overview, here is the complete list of all the keywords with examples.


Python Identifiers

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for writing identifiers

  1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. Names like myClassvar_1 and print_this_to_screen, all are valid example.
  2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
  3. Keywords cannot be used as identifiers.
    global = 1
    Output
      File "<interactive input>", line 1
        global = 1
               ^
    SyntaxError: invalid syntax
  4. We cannot use special symbols like !@#$% etc. in our identifier.
    a@ = 0

    Output
      File "<interactive input>", line 1
        a@ = 0
         ^
    SyntaxError: invalid syntax
  5. An identifier can be of any length.

Things to Remember

Python is a case-sensitive language. This means, Variable and variable are not the same.

Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count = 10 would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap.

Multiple words can be separated using an underscore, like this_is_a_long_variable.