Python The Worlds Most flexible and easist Language..

gayan kalhara

Member
Nov 22, 2007
7,060
48
0
Decisions 5.1 If statement

5.1 If statement

so here is a short program to compute
the absolute value of a number:

n = input("Number? ")
if n < 0:
print "The absolute value of",n,"is",-n
else:
print "The absolute value of",n,"is",n

Here is the output from the two times that I ran this program:

Number? -34
The absolute value of -34 is 34
Number? 1
The absolute value of 1 is 1

So what does the computer do when when it sees this piece of code? First it prompts the user for a number with the
statement n = input("Number? "). Next it reads the line if n < 0: If n is less than zero Python runs
the line print "The absolute value of",n,"is",-n. Otherwise python runs the line print "The
absolute value of",n,"is",n.
More formally Python looks at whether the expression n < 0 is true or false. A if statement is followed by a block
of statements that are run when the expression is true. Optionally after the if statement is a else statement. The
else statement is run if the expression is false.
There are several different tests that a expression can have. Here is a table of all of them:
operator function

< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal
!= not equal
<> another way to say not equal

Another feature of the if command is the elif statement. It stands for else if and means if the original if statement
17
is false and then the elif part is true do that part. Here’s a example:
a = 0
while a < 10:
a = a + 1
if a > 5:
print a," > ",5
elif a <= 7:
print a," <= ",7
else:
print "Neither test was true"
and the output:
1 <= 7
2 <= 7
3 <= 7
4 <= 7
5 <= 7
6 > 5
7 > 5
8 > 5
9 > 5
10 > 5
Notice how the elif a <= 7 is only tested when the if statement fail to be true. elif allows multiple tests to be done in a single if statement.
 

gayan kalhara

Member
Nov 22, 2007
7,060
48
0
I'm Glad to see you are Talking here....

And also

IF I'm Wrong at any point.... In this thread... Please Show me with the Corrections...
 

x-pert

Member
Jun 13, 2006
20,952
77
0
gayan kalhara said:
Yes Because I have to find and give you ONLY THE CORRECT...

always I try to provide the Correct thing...

But NOT wiki books...

By the Python tutorial from www.python.org..

it's the exact same thing mate.

no matter where you take them. :)
 

sri_lion

Member
Sep 14, 2006
12,908
102
0
Kuala Lumpur
gayan kalhara said:
PLS WAIT I'll Show you My Resources At the END OF THIS THREAD

I'll tell you what.. no need go through anymore harassment :lol:

Why dont you put all your references at the front page first? Everybody's happy and you can continue your work peacefully! ;)