
| This forum is proudly powered by Scientific Linux 6 | SL website Download SL Help Search Members |
| Welcome Guest ( Log In | Register ) | Resend Validation Email |
![]() ![]() ![]() |
| Jessica_Lily |
Posted: Jun 25 2012, 01:32 AM
|
|||||||||||||||||||||||||||||||||||||||||||
![]() SLF IRC Team ![]() ![]() ![]() Group: Members Posts: 95 Member No.: 180 Joined: 15-May 11 |
Hazah! I do still exist
Yes this is very much delayed. I will be carrying on though with the lessons.Previous Lessons Lesson 1 - Basic Maths & Core Data Types Lesson 2 - Binary / Binary Storage & Variables Lesson 3 - Input & Output (IO) Lesson 3 Answers 1) A string is stored inside the variable question1 with the value "This is question 1" 2) It will round down the floating point number to 7, the 7 will be then displayed. 3) It will wait until you type something in, once you have and pressed enter it will display what you typed. 4)
5)
6) <type 'str'> 7) Yep (i did it in my example for 5)
Into So in this lesson we're going to be going over boolean expressions and if statements! These are a very important part of programing. When you want to check something and then do something based upon that check you usually use an if. For example. If you wanted to check the user inputted a number less than or greater than a certain amount you can or if you wanted to check the user typed a certain word or something. Conditions A condition is just an expression which will evaluate to True or False. Often you can use True and False if you wish to make sure something is always going to be true for false (note the case is important in python). So firsly we should look back at the bool function we introduced in lesson 1. You should notice that also data types can be evaluated to be true or false. The rules are below: string If the string is empty "" it is False
Otherwise it's True:
Anything but an empty string will give you true. Int & Float If the int or float has the value of 0 (or 0.0) is False
Otherwise it's True:
Now onto comparisions. Since we have used = we can't use it to check if something equals something else so in most programming languages including python we use == for example:
As you can see it shows you what they evaluate to, you can also do not equal, to do this you use != (the ! often means not in programming languages)
You can also check if numbers are less than others or greater than using < > symbols, symbols i'm sure you're familiar with from maths. If not, no worries. We'll get you aquanted in no time! Meet <, he's the less than operator/symbol.
Meet >, he's the greater than operator/symbol.
Now, what do you think 3 < 3 would give you? False? If so you're correct. What if you want a to check if it's less than or equal, in maths class you used the < with an extra liny thing under it. Have a scout around your keyboard... Let me know if you find it ![]() Kidding, since we don't have this symbol on our keyboards we use <= (makes sense right? ) so:
So you say that's all good and well but what if i want to see if both it's less than a hundered but above 0? Well we can use the words 'and', 'or' and 'not': and - This requires both sides of the operator to be True. or - Ether or both can be True for the statement has to be True but if nether are True, it's False. not - This inverts it. So our problem (assume x is our number we want to check):
A good use of not is if you want to check a string has something in, say you have got user inputting their name you might want to check they did actually write something there:
Just for good messure you might want to check if someone is elligable for an offer maybe 10% off a product if you're a student or you're older than 70:
If statements Well i'm sure by now you're thinking, lovely I can see true or false but how does that help me actually do anything. Well the reason i split these up is conditions are used in more than just if statements but other things like loops which they're also used in I will come to in a later tutorial for now you can use them in if's. If is simply checking if something is true and running some code or also running some other code if it's false. For example
(note the .... is to show the code block you should indent to make the block you're writing be executed only when the codition is true (intenting is done by the tab key.) Since i'm 19 it says, is age greater than 20 (21 or above). Because it's not nothing was displayed. watch if i changed the age.
You might want to tell the person they cannot drink if the if statement if false, you do this using the else construct for example:
Maybe you want to check multiple conditions, you can do this using elif (short for else if):
To finish i will use one of these fancy words, not, I choose you! Like i was saying before about entering a name you could use a if statement to ask again if it is blank:
Exercises (only use python to check, try and work them out before hand). True or False: 1) 72 <= 72 2) 3 < 1 3) 9.2 < 9.1 4) 9.2 <= 9.3 5) 3 > 4 6) 9.2 > 9 7) 9.2 > 9.3 8)
9) Create a short program to ask for a number and check if it's less than 100 and greater than 50 (don't forget to cast - look in past lesson's if you've forgotten)10) Extend program 9 to check if a number is less than 0 and display they've entered a negative number, check if the number is between 0 and 100 and tell them they've entered a small number else tell them they've entered a huge number. I will be posting the future lessons very soon, lesson 5 will be within the week. |
|||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() ![]() ![]() |