Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Today at 3:55 PM
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Thursday at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Thursday at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Wednesday at 5:34 PM
Pure VPN - Up to 27 Months
vgp
Updated:
Jun 5, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Python The Worlds Most flexible and easist Language..
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="gayan kalhara" data-source="post: 3339715" data-attributes="member: 60168"><p><strong>Decisions 5.1 If statement</strong></p><p></p><p style="text-align: center"><strong>5.1 If statement</strong></p> <p style="text-align: center"></p><p style="text-align: left">so here is a short program to compute</p> <p style="text-align: left">the absolute value of a number:</p> <p style="text-align: left"></p> <p style="text-align: left">n = input("Number? ")</p> <p style="text-align: left">if n < 0:</p> <p style="text-align: left">print "The absolute value of",n,"is",-n</p> <p style="text-align: left">else:</p> <p style="text-align: left">print "The absolute value of",n,"is",n</p> <p style="text-align: left"></p> <p style="text-align: left">Here is the output from the two times that I ran this program:</p> <p style="text-align: left"></p> <p style="text-align: left">Number? -34</p> <p style="text-align: left">The absolute value of -34 is 34</p> <p style="text-align: left">Number? 1</p> <p style="text-align: left">The absolute value of 1 is 1</p> <p style="text-align: left"></p> <p style="text-align: left">So what does the computer do when when it sees this piece of code? First it prompts the user for a number with the</p> <p style="text-align: left">statement n = input("Number? "). Next it reads the line if n < 0: If n is less than zero Python runs</p> <p style="text-align: left">the line print "The absolute value of",n,"is",-n. Otherwise python runs the line print "The</p> <p style="text-align: left">absolute value of",n,"is",n.</p> <p style="text-align: left">More formally Python looks at whether the expression n < 0 is true or false. A if statement is followed by a block</p> <p style="text-align: left">of statements that are run when the expression is true. Optionally after the if statement is a else statement. The</p> <p style="text-align: left">else statement is run if the expression is false.</p> <p style="text-align: left">There are several different tests that a expression can have. Here is a table of all of them:</p> <p style="text-align: left">operator function</p> <p style="text-align: left"></p> <p style="text-align: left">< less than</p> <p style="text-align: left"><= less than or equal to</p> <p style="text-align: left">> greater than</p> <p style="text-align: left">>= greater than or equal to</p> <p style="text-align: left">== equal</p> <p style="text-align: left">!= not equal</p> <p style="text-align: left"><> another way to say not equal</p> <p style="text-align: left"></p> <p style="text-align: left">Another feature of the if command is the elif statement. It stands for else if and means if the original if statement</p> <p style="text-align: left">17</p> <p style="text-align: left">is false and then the elif part is true do that part. Here’s a example:</p> <p style="text-align: left">a = 0</p> <p style="text-align: left">while a < 10:</p> <p style="text-align: left">a = a + 1</p> <p style="text-align: left">if a > 5:</p> <p style="text-align: left">print a," > ",5</p> <p style="text-align: left">elif a <= 7:</p> <p style="text-align: left">print a," <= ",7</p> <p style="text-align: left">else:</p> <p style="text-align: left">print "Neither test was true"</p> <p style="text-align: left">and the output:</p> <p style="text-align: left">1 <= 7</p> <p style="text-align: left">2 <= 7</p> <p style="text-align: left">3 <= 7</p> <p style="text-align: left">4 <= 7</p> <p style="text-align: left">5 <= 7</p> <p style="text-align: left">6 > 5</p> <p style="text-align: left">7 > 5</p> <p style="text-align: left">8 > 5</p> <p style="text-align: left">9 > 5</p> <p style="text-align: left">10 > 5</p> <p style="text-align: left">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.</p> <p style="text-align: left"></p> </p></blockquote><p></p>
[QUOTE="gayan kalhara, post: 3339715, member: 60168"] [b]Decisions 5.1 If statement[/b] [CENTER][B]5.1 If statement[/B] [LEFT]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. [/LEFT] [/CENTER] [/QUOTE]
Insert quotes…
Verification
Hath warak paha keeyada? (hatha wadikireema paha)
Post reply
Top
Bottom