Introduction To Python Statements

 



In this article, we will be doing a quick overview of Python Statements. This lecture will emphasize differences between Python and other languages such as C++.

There are two reasons we take this approach to learn the context of Python Statements:

  • If you are coming from a different language this will rapidly accelerate your understanding of Python.
  • Learning about statements will allow you to be able to read other languages more easily in the future.

Python vs Other Languages

Let's create a simple statement that says: "If a is greater than b, assign 2 to a and 4 to b".

Take a look at these two if statements (we will learn about building out if statements soon).

Other Languages

if (a>b) { 

    a = 2; b = 4;

 }

Python

if a > b:
    a = 2;
    b = 4;

You'll notice that Python is less cluttered and much more readable than the first version. How does Python manage this? 

Let's walk through the main differences: 

Python gets rid of () and {} by incorporating two main factors: a colon and whitespace. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in the case of the statement.

Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.

Lastly, to end this brief overview of differences, let's take a closer look at indentation syntax in Python vs other languages:

Indentation

Here is some pseudo-code to indicate the use of whitespace and indentation in Python:

Other languages

if (x)

Read Also

    if(y)
        code statement;
else
    another code statement;

Python

if x:
    if y:
        code statement
else:
    another code statement

Post a Comment