Site hosted by Angelfire.com: Build your free website today!
Welcome | About Me Downloads | | Links |programming in python | Contact Me |home| |

 

Programming requires alot of skill and virtue ,most people aren't born with it while others ,well you know what i mean. But this skill can be learnt,with a little bit of determination and a little bit of enthusiasm , anyone can learn to program. Although most people start out by learning languages such as QBASIC,C,C++,PASCAL etc .I'll not recommend anyone of them to a complete beginner. Qbasic is a good language for novices and beginners but due to the fact that it's almost dead now, we won't be covering any part of it C,C++ are good languages but are more than what a beginner can grasp.C's cryptic syntax and it's down to earth nature makes it my last choice,same goes for C++. In this tutorial I'll be explaining to you programming with python,an interpreted programming language.This tutorial is meant for the complete beginner so,i'm not going to be covering topics such as objest oriented programmin (oop).But i'll try my best to add new things and concepts every once in a while.

Creating the programming mindset

What does programming mean ??How do i learn to program??,what kind of software do i need ??,these are the questions that are most frequently asked by beginners .Well ,I'd define programming as analyzing any problem ,then trying to solve the problems thgrough trial and error method or right on the spot (nailed 'em ,heehee ) . It's as simple as that.Progamming can be learnt but cannot be mugged up ,you need to practice all the examples i'll give,try them out,analyze and ask youself questions .(in programming curiosity never kills the cat.) Python interpreter comes with the RED HAT LINUX package ,but for all of you who aren't fortunate enough can also get Python for WINDOWS (9x,2000,whatever..). Well here's what you've been so impatiently waiting for , your'e very first program in PYTON ,exciting isn't it ??

example 1 your'e very first program>

print " hello ,world "

 You can either do you're programming in the immediate mode or you van save your'e program and then run it.

WEll the first example is quite self-explanatory,the Print statement prints whatever you want it to.if you need to print numbers you don't need to give the quotes,while on the other hand if you have to print strings (i.e non numerical characters given under quote ),you will need to give the quotes.here's another program using the Print statement.

example //bread crums all over my shirt??

  print

  print "5+5 = " ,5+5

  print "5-3 = " ,5-3

  print "5^2 = " ,5**2

  print "5 mod 2=",5%2

  print "5 /2 =" ,5/2

  print "5*5 =" ,5*5

example 3 anything goes >

  print "hello , there"

  print "hi"*5

 In the first example the Print (only) statement tells the interpreter to go to the next line.In pyton + ,- ,* , / are used for addition ,subtraction ,multiplication ,and division respectively ,while these are used for simple operations ,% is used for modulas or to get the remainder and ** is used as exponentiation.

Input statements (getting data from user)

Python is pretty sensitive when it comes to getting data from the user ( programmer ).It uses the input("commnet") statement for entering numerals (int,long ,short ,float, etc) , whereas raw_input("comment ")to input strings.here's an example :

example 4 dum dada dum..i love rock n' roll nanana..

 c=input("enter your'e lucky number number: ")

 a=raw_input("enter your'e name: ")

 print a ," you're lucky number is : ",c

what did that program do??,well it basically asked you the user for a number ,then stored it in a variable(a variable is an entitiy or an object which contains a certain value,either numbers or strings,in this case the variable was C ,which stored a number),then it asked for your'e name which happens to be a string and stored it in variable a. Then the print statement showed output as :

 enter your'e lucky number: 5

 enter your'e name : robin

OUTPUT:

  Robin your'e lucky number is 5

The if statement

example 5

 c=input("enter an integer")

 if c < 0 :

   print"absolute value of",c ," is",-c 

 elif c>= 0:

   print"your'e number was :",c 

*Note that in conditional statements in PYthon have to be indented correctly ,if not the PYthon interpreter may not interpret your'e code.Also the if line must end with a column (:).

here are some of the boolean operators which can be used in if statements:

               <  (less than) 
               >  (greater than) 
               =  (assignment operator) 
               == (equality) 
               != (not equal to) 

example 6 BE RESPONSIBLE AND ELLIGIBLE

  #this program cheks for elligible age to vote

 print "enter youtr'e age"

 age=input()

 if age < 0 :

     print "wrong input" 

 elif age >100 :

     print "wrong input"

 else age >=18

     print "you are elligble to vote" 

here ,if age is greater than 100 or less than 0 it gives "wron input " but if it'e some where in the middle most likely greater than 18 ,it will give you "you are elligible to vote"

example 7 Got any ideas ??

 #program to print wether given no. is odd or even

 c=input("enter a number")

 if c % 2 =0 :

               print"even number" 

 elif c % 2!= 0:

               print"odd number" 

Note* the "#",better known as pound sign is used it PYthon as a way of commenting or giving remarks about the program.This can be very useful,especially when other programmers read your'e code .It's a good habit to do so and can save alot of time later on.The "#" pound sign is traditionally used in powerful programming languages like C and C++ as pre-processor directives.

Iterations (Fun with LOOPs)

The looping concept has been there for as long as there have been programming languages.The need to perform certain tasks repaetedly meant that the strenous task of writing the same code over and over again had to be done.For this programmers inveted and initialised the LOOPING concept.In PYthon too you can perform loops.These statements are called looping statements.while ..loop ,for ..loop etc are common examples.Let's try some of them :

example 8 shheeesh this is easy!and lame!

 #program to print all numbers from 1 to 100

 for x in range(1,101,1):

              print x, 

 Note* that (:)the column sign is used in the first line of example 8.Sorry if the indents in the loops are incorrect,you'll just have to bear with it.heheh !!

example 9 (moving on to some thing better !)

 #program to print your'e name 20 times

c=input("enter your'e name")

  for x in range (1,21,1):

               print c

example 6 (moving on to something classier,NESTED LOOPS,WOOPS)

&nsbp;for x in range (1,7,1) :

          for y in range (1,x,): 
                    Print y ,   
          print    

WAS TAHT COOL OR WHAT !!,man i could have sworn that one really made me think for a while . hey but nothing's easy ,that's how life's supposed to be right.OK,enough of the wise talk,lets get down to business,ok we assigned initial value of the outer loop as 1,whereas the sentinal value as 7 ,with 1 as step value.THE minute you hit the ENTER key,the interpreter assigns these values and enters into the inner loop,where again the interpreter assigns new values.The inner loop iterates until it's condition is met.Take for instance x=1 in outer loop is initial value then in the inner loop x=1 in sentinal value,then the interpreter reads Print y,and goes back to for y in range(1,x,): which now has met it's condition sice initial value 1 is equal to sentinal value X(which is also 1),thereby going back to For x in range(1,7,1).but again ,now the value of x is equal to 2(x=2).Then again it enters the inner loop with value of x as 2.this now prints 1 2.finally the result is :

       1
       1 2 
       1 2 3 
       1 2 3 4
       1 2 3 4 5 

HOMEWORK

  • Writ a program (WAP)to print wether a given number is prime or not.
  • wap to find the root of equation ax^2+bx+c=0{hint:use x={-b+(b^2-4*a*c)^0.5}/2*a and x={-b-(b^2-4*a*c)^0.5}/2*a}
  • wap for a grocer to calculate cost amount of vegetables at Rs5/kg,biscuits at Rs 20/pack,cookies for Rs 40/pack,tickets to Rock concert at Rs 150/each etc.ask user for no of items and then calculate amount accordingly.
  • wap to print if a five digit number is palindrome or not.

    That's it,this much for now more later.Any,comments ,suggestions etc.are welcome.

    more coming soon,i've been soooo busy,but i'll try to update the site every now and then.Thnx for visiting!!


    back to main
    Copyright © 2003 - Robin joshi
    All Rights Reserved
    Webmaster: Robin joshi - robinj@cyberspace.org