Writing a loop first time in python



In the last post (Beginning with a python program: The tail) we started from scratch how to begin with python. In this post we will advance towards writing a for loop. Before we can start that, let us understand the structure of the program. We need to know how python would differentiate between string (text), integer (whole number), float (real number) etc. Python is beautiful because it has built-in types. Let us try to understand these types.

Ipython Notebook

We say that you can run python directly from terminal or command-line environment or by saving a text file as file.py and run it as python file.py in terminal. But there is another way to run and edit scripts at the same time. This is called Ipython notebook. In linux just install ipython look at how to install ipython on linux?. If you have installed anaconda which I would suggest for windows users very strongly then just look at how to run ipython notebook on windows? Once you know it just type in anaconda or terminal ipython notebook. It will open a browser and start a new notebook.

Types in Python

For having a good idea how it works see here,  how to know types in python?

Numeric Type

You need to understand here integers, float and complex types. 
Integers: Integers are easiest to use. You just start python (first post) and need to do nothing. All mathematical operations go with integers as if you would be doing on your paper by hand.
Example: Start ipython notebook:

x=5 #here we set x as an integer
y=10 #here y is set to be integer 
a=x+y #we add like you would do in calculator 
b=x-y 
c=y/x
d=x/y
e=x*x
f=y**x #x to the power yprint a,b,c,d,e,f
15 -5 2 0 25 100000

We see that values of a and b are correct but for d it printed 0. Why 5/10 is zero? This is where we need float. Python did not understand what the value should be. The simple reason is that it does not know what 0.5 means because it is not an integer.


Float:  You can call any integer with float  and it will give a real number e.g. float(5)=5.0 . The problem that 5/10 is zero can be solved by either doing 5/float(10) or with 5/10.0 in both cases python understands that is is not a whole number.
Example 1: Now edit your ipython notebook, you can give two tries:
x=5 #here we pu x as an integer
y=10 #here y is set to be integer 
a=x+y #we add like you would do in calculator 
b=x-y 
c=y/x
d=x/float(y)
e=x*x
f=y**x #x to the power yprint a,b,c,d,e,f
15 -5 2 0.5 25 100000 #here d is type float 

Example 1: Now edit your ipython notebook again:
x=5.0 #herex is defined as a float by putting decimal places to it
y=10.0 #here y is set to be integer
a=x+y #we add like you would do in calculator 
b=x-y
c=y/x
d=x/y
e=x*x
f=y**x #x to the power y
print a,b,c,d,e,f
15.0 -5.0 2.0 0.5 25.0 100000.0 #now all the results are of type float 

Comparisons and conditions in Python 

Basically you need to go as you think e.g. 
OperationMeaning
<strictly less than
<=less than or equal
>strictly greater than
>=greater than or equal
==equal
!=not equal
isobject identity
is notnegated object identity
Example:  Start a new ipython notebook or save a text file to run python file.py:
x=10.0
y=20.0
z0=15
if z0<y:
    z=x+y
else:
    z=y-x
print z
30.0 #here we see that if we put z0=15 we get 30 for z0=30 we get 10.
In this case we also learnt, how to use if condition if python? Note that if you use if condition you need 4 empty spaces in the beginning on the next line. 


For loop in Python

A for loop can be used for many things e.g. adding some integers or reading some data from a file etc. Let us write a simple python notebook for a loop which just prints your name multiple times:
Example: 
n=10
name = "Nagini"
for i in range(10):
    print name
    
#Now we go to next step and make a loop for adding some integer
b=0
for i in range(10):
    b+=i #you can also write b=b+i
    print b
#if you write outside of loop you will get final value of b
print "Final value of b =", b
#we can conditionally exit a loop
c=0
for i in range(10):
    c+=i
    if c>=32: #note that if condition is 4 white space shifted 
               #and the next text is 4 white spaces shifted after the if condition
        break # if c gets bigger equal to 32 we exit. In this case at 36 
               #we break and last value we save is 28
    print c    

So we learnt quite few things in this one. How to understand types in python? How to apply if condition in python? How to use for loop in python? In next post I will put many programs which will be using concepts we tried to understand here.


Comments

Popular posts from this blog

How to use Edward library for probabilistic modeling with Tensorflow and GPy to study asymptotic connections between Multi-Layer Perceptrons (neural nets) and Gaussian processes?

Numpy: How to do data analysis using python?

How to define a function in python?