In this article, we will learn about basic programming in Python i.e. features, concepts or styles of the Python programming language. Before diving deep into the basic coding in Python let’s first learn about how to code in the Python Programming language by looking at some of the language specifics like whitespace indentation, Python REPL, commenting style, how to get user input, Python module, etc.
The aim of this article is to get you familiarized with the general concepts & style of Python programming language i.e. basic programming in Python so that before going into actual Python coding you have some basic understanding of the language syntax and workings. So let’s learn about basic programming in Python & get a more detailed understanding of how the Python language works.
This is the third article in the Series: Python Tutorial – Learn Python Programming for Beginners
Table of Contents
Learn Basic Programming in Python
We will be learning about some basic programming in Python i.e. concepts, styles & features to understand how to code in Python and exactly how the Python programming language works.
Whitespace Indentation
Python has its own unique syntax and like many other languages, this cannot be delimited by curly brackets or Begin/end statements instead developers have to make use of whitespace, or what we can call it is indentation.
Indentation or whitespace is very important in Python as it determines how lines of code should be associated with each other. In fact, in Python, you should be very careful about the way you indent your code as misplaced indentation can change the flow of your code.
Whitespaces to the left of the code are important to decide the level of indentation. You will have to keep track of the level of indentation in your code and be consistent with it. i.e. if you decide to use standard 4 spaces indentation then you have to same 4 spaces for the entire logical block. If you miss on consistency then it can result in an error for you as shown below
In the above code, there is no consistency in the indentation of the first & second lines of code. As you can see the second line of code has an extra couple of spaces at the start so this resulted in an unexpected indent error.
Spaces are preferred for indentation over tabs so the whitespace needs to be consistent!
REPL in Python
As we had covered in our first article on Python Programming that Python is an interactive programming language i.e. its reads the input, evaluates the input, prints the result of evaluation & again loops back to the read mode. This is one of the interesting aspects of the Python programming language that it can be used in an interactive mode with the user entering the command on the command prompt & python evaluating the command as this can be helpful in debugging process.
REPL stands for Read Evaluate Print Loop i.e. Read, Evaluate, Print & again go back in a loop to Read, Evaluate, Print and this continues. Read you can say is to wait for the next command or user input and process (evaluate & print) that command to read again.
To run Python REPL you need to start Command Prompt Window and type ‘python’ into the prompt. Command Prompt Windows with REPL is shown below
As seen above we have typed several commands for python and Python in interactive mode waited to read the command, evaluated it & printed the result as per evaluation. After printing the result it again looped back to read to wait for the next command. In the above case we
- Gave command 5+8 and it printed 13
- Gave command 6+7 and it printed 13
- Set variale y as 100 and gave commands y*4 & y+2000 which were printed correctly.
- Even saw that value of variable y was retained in the session to be used in further commands. If you quit REPL mode and enter again then the variables and assignments those have made are lost.
- Finally gave command CTRL-Z to exit the Python interactive mode.
Comments
As we all know that it is good to add comments to the code so that when we refer to it after many months or years we get an idea about what this code or function does or why you wrote something.
Comments are the sections of the code that are ignored by the compiler or interpreter. To understand basic programming in Python one should know how to add comments in the Python programming language. Every programming language has its own syntax for handling comments so let’s see how to add comments in Python code.
Comment in Python code starts with # and anything that follows # till the end of the line will be considered as a comment and ignored by the Python interpreter. Below is the sample code that demonstrates how to add comments in Python code.
#This is Comment Print("Hello, World!") # Printing Hello World
User Input
User Input logic deals with the ways of getting the input from the user. Often there is a need to get input from the user like some details from the user i.e. Name, Age, DOB, etc.
In the Python programming language, a function named input() is used to take input from the user. In Python by default the data type for any input in string irrespective of what user has entered. When the input() function executes in a program then program flow will be stopped until the user has entered some input.
If the user enters an integer value still input() function converts it into a string. So if you want the input of numbers like age from a user then by default its data type will be a string which you will have to convert into an integer before processing.
Below is the sample code of how input is taken from the user.
age = input("Enter your age: ") print(age)
After executing the above python code we get the below output. The user was prompted for input that was entered as 55 and then 55 got printed
We looked at how to take input from users which can be validated and used in our programming logic to make decisions about the flow of the program.
Modules
The Python module is like a package that allows you to logically group together related functionality in code using classes & functions. It is like a library that can be reused by referencing it in other modules, codes, etc.
The module is a Python code file containing definitions & statements. It can contain both executable statements as well as function definitions.
It is a way to logically group your code by functionality that also makes it easier to use, maintain, read and understand. It is also a way to break down your code into multiple parts if your code is getting too large and you want to keep the size under control to improve readability.
A module is like a separate .py file that contains Python code like variables, classes & functions. This module can be a reference in other modules or code using import statements. Then file name of the module can be used for referencing that module in other code.
As of now, you can just look at the sample code for Module & its usage below. Don’t worry if you are not able to understand code as we have just started with Python and understanding its concepts first.
#ModFileIO.py def SaveToFile(file_data): print('File Created') print(file_data) print('File Saved')
We have imported the above module in the code below and are using the SaveToFile function from that module.
import ModFileIO print("Hi From This Code!") ModFileIO.SaveToFile("Save File Data")
So under basic programming in python we learned about what are modules, why are they required and how to use them. Now let’s look at our First program in Python Programming Language now
Different ways to run Python Programs
It is possible to run Python programs in more than one way which allows you to choose the method most suitable as per your requirements. Here are the different ways to run Python Program on Windows Machine
- Writing the code in IDE (integrated development environment) i.e. Visual Studio Code and directly running it from that IDE
- Writing the code and storing it in a file which can be executed using Python Command
- REPL Mode i.e. interactively using the Python interpreter in Read Evaluate Print Loop mode
- Writing the code and storing it in a file so that it can be ran as a script file.
Summary
In this article, we learned about basic programming in python i.e. features, concepts, or styles of the Python programming language. This gave us a good understanding of how to code in Python and how the Python programming language works.
The concept of modules looks like a good & useful way to divide our large code across multiple files to improve the readability, understanding, and maintainability of the code.
In our next article, we will learn in detail how to work with variables in the Python programming language.
Please provide your suggestions & questions in the comments section below
References – Read–eval–print loop
Nice Article about Python Programming. I get to know many things about python. I Highly recommend your article.
By the you can also checkout my article on Python language.
Thanks For this post
Thanks for your feedback!