Python Tutorial — Chapter 1 — Print() Function & Indentation

In chapter 1 of Python Tutorial, we will learn about Print function and Indentation

In the last tutorial, we learnt about the basic topics of Python language.

As mentioned earlier in the previous tutorial, Python is a simple language with a relatively straightforward syntax.

Anyone can easily understand the Python code, it is like reading a paragraph. There are two major Python versions: Python 2 and Python 3. Both versions are very different. This lesson use Python 3 version, which is more semantically valid and includes newer capabilities.

To give an example about the differences between Python 2 and Python 3, let’s take take a look at the print statement which is our first topic in this chapter 1 of Python tutorial.

In Python 2, the “print” statement is not a function, hence it is called without brackets which means when trying to run/execute code. However, in Python 3, it is a function that must be called using brackets when trying to run/execute code.

Let’s learn more about print statement in this chapter 1 of Python tutorial.

To print a string** in Python, we write the code as below:

print("Printing this line")

#output
Printing this line

The output of this code will be “Printing this line“.

You can try your own string** within the print function and run the code. Please scroll down for Python IDE to practice print function.

** We will learn more about what is a string in next tutorial.

Print function displays text or variables on the console or standard output. You can use the print function to display messages, variables, and even calculated results.

It is a built-in function in Python 3; therefore, it may be used in any Python program without the need for further installation.

Indentation:

Indentation refers to the spaces that appear at the beginning of a code line. In most of the programming languages, code indentation is just for readability, in Python, it is critical.

Indentation is used to represent a block of code. Tabs and spaces are both supported, however standard indentation requires four spaces in Python code but it is not mandatory. In the below example, it is indented for two spaces only.

a=1
if a == 1:  
  #indented
  print("a is equal to 1")


#output
a is equal to 1

Please make a note of the indentation in the above code. As per Python code standard, four spaces are indented but not mandatory, it can be two spaces only.

That brings to the end of chapter 1 of Python tutorial, see you in the next tutorial!!!

Please make sure your practice your learnings in the Python IDE below. Practice makes everyone Perfect!:

DataCamp Light | Standalone example

In the left hand side screen, you can write your code/program and once, it is completed, click on the Run button to run/execute the code.

On the right hand side screen, you will see the output of your code if the code is properly written. If there is some issue with the code, it will thrown up an error.

By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS.

How to Install Python 3 and Pycharm IDE on Windows

How to configure Pycharm IDE and “Run” your first Python 3 program

For your reference, to download Pycharm IDE, visit below link:

https://www.jetbrains.com/pycharm/download/?section=windows

However, if you don’t want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.

If you have any questions or comments about chapter 1 of Python tutorial, please let us know in the comments section below.

Leave a Reply