Python-Old-Version/C4/Conditional-statements-and-Loops /English
Welcome.
In this tutorial we shall be covering conditional statements and control loops. We have used them already in some of our previous sessions with brief introduction. We shall be covering 'if-else' statement, 'while' and 'for' statements formally.
For understanding of if-else statement we will write a python script that takes a number as input from user and prints 0 if it is zero and prints "Be positive" if it is negative, prints "Single" if the input is 1 and if the number is not 0 or 1 or negative, it prints "More".
To write the program, open Scite text editor by going to Applications-> Programming->Scite:
First we prompt user for entering a integer by using raw_input
str_x = raw_input("Enter an integer: ")
since we know raw_input gives string, we convert the input string to an integer by typing
x = int(str_x)
now we check if the number is less than zero. type
if x < 0:
Please note
- if number is negative we have to print "Be positive"
- so we give four spaces for indentation and type
print 'Be positive!'
elif x == 0:
to check if the number is equal to zero
- This is else-if condition
print 'Zero' elif x == 1: print 'Single'
then we type the else statement which gets executed when all the if and elif statements fail. so type
else: print 'More'
Save this script by going to file menu and clicking on save. save it in home folder with name 'ladder.py'
let us check the program on ipython interpreter for various inputs. open ipython terminal and type
%run ladder.py
It will prompt us to enter a integer and based on our input it prints appropriate message.
We can use binary operators like and/or/not to check for multiple conditions.
Python supports only if-elif-else conditional constructs, switch-case statements are not available/supported in Python.
Now lets look at loop constructs available. Python supports 'while' and 'for' statements.
To understand the while we shall write a script that prints all the fibonacci numbers less than 10. In fibonacci series the sum of previous two elements is equal to the next element.
In Scite go to file menu and click on new and it opens a new tab.
First we initialize two variable to first and second number of series
a, b = 0, 1 while b < 10:
This block will be executed till this condition holds True
print b,
Note ',' here for printing values in one continues line.
a, b = b, a+b
This is one powerful feature of Python, swapping and assigning new values at the same time. After this statement a will have present 'b' value and b will have value of 'a+b'(phew this can be close)
Save this file as 'fabonacci.py' and lets run it from IPython interpreter by
%run fabonacci.py
'for' in python works any kind of iterable objects. In our previous sessions we used 'for' to iterate through files and lists.
So we are going to use for loop print the squares of first five whole numbers.
To generate squares, we have to iterate on list of numbers and we are going to use the range function to get the list of numbers. Let us look at the documentation of range function by typing
range?
we see that it takes three arguments, first is the start/initial value second one is the stop/last value and third is the step size.
Out of these, 'start' and 'step' arguments are optional.
So to get list of first five natural numbers, we use
range(5)
Note here that last/stop value is not included in resulting list.
So to get square of first five number all we have to do is iterate over this list.
for i in range(5): print i, i*i
Similarly to get square of all odd numbers from 3 to 9 we can do something like
for i in range(3, 10, 2):
so the list returned from range this time will start from 3 and end at 10(excluding it) with step size of 2 so we get odd numbers only
print i, i*i
since the for statement in python works on any iterable we can also iterate through strings
to print each character in a string we do
for c in "Guido Van Rossum": print c
we see that it prints all the characters one by one
That brings us to the end of this tutorial. We have learnt conditional statements in Python. How to write loops using 'while' statement. Range function and using for loop
Thank you!