Let us now dive into python and start some coding and learn about various conditional statements, looping and control structure in Python. At the end of reading this post, you will learn to code and use if-statements, for-loops and while-loop in Python. We will start with the basics of branching programs.
The simplest branching statement is a conditional statement,, which has three parts:
- First part is a test part, which is a condition that evaluates to true or false.
- Second part is a true block which is executed if the test condition in first part evaluates to true.
- Third part is an optional block of code which is executed if the test condition in first part evaluates to False.
In Python, a conditional statement has following form, moreover, you also have elif
Importance of Indentation in Python
Now, seeing the above piece of code in the example, you would have felt that something is missing [scope]….nope…nothing is missing…..Everything is there but it is somewhat different. So, let’s see how is it different.
For scope, in Python, We need to use “INDENTATION” and it is much important but why only indentation for scope? Curly braces are used for scope in most of the programming languages, then why indentation in python?
- It is semantically meaningful.
- For example, if the last statement in the above code were indented it would be part of the block of code associated with the else, rather than with the block of code following the conditional statement.
I guess you are now clear about scoping Python, so now let’s get back to conditional statement [if-statement]. You can see “:” and indentation in the example which indicates that when the indentation is removed your condition ends and when you put a colon “:”, your condition starts or scope starts.
Let’s see some for-loop examples now:
We just saw the two examples of for-loop in Python, you can notice that there is no increment or decrement operator required here. In example 1, you can see that the for-loop simply runs through the array (which actually is a list, that we will learn later) and prints out all its content. It would have done same thing even there were strings instead of integers in the array.
In example 2, you would find that in order to print numbers 1 to 9, we used a function called range(). Let us dig in to range() function.
The range() type returns an immutable sequence of numbers between the given start integer to the stop integer. Following is its syntax:
We can see that, range() allows user to generate a series of numbers within a given range. Users can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next as per their requirements. range() takes mainly three arguments.
- start: it is an integer starting from which the sequence of integers is to be returned
- stop: it is an integer before which the sequence of integers is to be returned. The range of integers end at stop-1. Which means that it is right-exclusive and same is the case with every function, list, dictionary, tuple or any sequence generated in Python.
- step: it is an integer value which determines the increment between each integer in the sequence.
We already saw in example 2 where range() is used with two arguments, start and stop, now let’s take examples where only one argument and three arguments are used.
In example 3, you can see the range() function with only one mandatory argument, and the output are numbers from 1 to 9 and not up to 10 (because of right-exclusiveness). By this example 3 as well as from example 2, we can note that range() always take step size of 1 as a default.
In example 4, we have given step size as 2 and we can see that it prints every number between 1 to 9 with difference of 2 between every number. So, this is how step size works. Thus, we have seen all the examples with different number of arguments in range(), hope it is clear now that how does it work.
Hope that, scoping and conditional statements are clear to you guys, thus we move directly hop on to the example of While-loop for its understanding.
You can see the output below, which shows that while-loop controls the value “ans” that we are printing out as an output of square. This how while-loop works as a control structure.
We also have iter() function in order to have Iteration in tuple and lists. We are going to see tuple and lists later on.
So we have learn about various looping and control structure in python and hope that everyone is very much clear about if, for and while in Python. Moreover, at the end of this post, I also hope that the concept of scoping using indentation and iterating in for loop using range() will also be clear. In next post, we will go through indexing and slicing of strings and various other inputs.
Other Important Topics:
- Python and Other Object-Oriented Programming Languages
- Python 2 vs Python 3
- Introduction to Python Programming
- The Basic Elements of Python Programming
- Branching, Indentation, Looping and Control Structure in Python
- Indexing & Slicing of Strings and Capturing Inputs
- Built-in Data Types and Functions in Python
- Specifications, Global Variables, Modules and Packages in Python
- Working with Files in Python
- Strings in Python
- Lists in Python
- Tuples in Python
- Dictionaries in Python
- Mutable and Immutable Python Objects
- Functions as Objects, map(), filter() and reduce()
- Exception Handling in Python
- Classes and Object Oriented Programming in Python
- Searching Algorithms in Python
- Method Resolution Order (MRO) in Python
- Sorting Algorithms in Python