| 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.
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 ??
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.
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
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.
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
Robin your'e lucky number is 5
c=input("enter an integer")
if c < 0 :
elif c>= 0:
*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 here are some of the boolean operators which can be used in if statements:
#this program cheks for elligible age to vote
print "enter youtr'e age"
age=input()
if age < 0 :
elif age >100 :
else age >=18
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"
#program to print wether given no. is odd or even
c=input("enter a number")
if c % 2 =0 :
elif c % 2!= 0:
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. 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 : #program to print all numbers from 1 to 100
for x in range(1,101,1):
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 !! #program to print your'e name 20 times c=input("enter your'e name")
for x in range (1,21,1):
&nsbp;for x in range (1,7,1) :
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!!Creating the programming mindset
example 1 your'e very first program>
print " hello ,world "
example //bread crums all over my shirt??
example 3 anything goes >
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..
OUTPUT:
The if statement
example 5
print"absolute value of",c ," is",-c
print"your'e number was :",c
< (less than)
> (greater than)
= (assignment operator)
== (equality)
!= (not equal to)
example 6 BE RESPONSIBLE AND ELLIGIBLE
print "wrong input"
print "wrong input"
print "you are elligble to vote"
example 7 Got any ideas ??
print"even number"
print"odd number"
Iterations (Fun with LOOPs)
example 8 shheeesh this is easy!and lame!
print x,
example 9 (moving on to some thing better !)
print c
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 : example 6 (moving on to something classier,NESTED LOOPS,WOOPS)
for y in range (1,x,):
Print y ,
print
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
HOMEWORK
back to main