scientificlinuxforum.org QR code
Scientific Linux Forum.org



  Reply to this topicStart new topicStart Poll

> Python 4
Jessica_Lily
 Posted: Jun 25 2012, 01:32 AM
Quote Post


SLF IRC Team
***

Group: Members
Posts: 95
Member No.: 180
Joined: 15-May 11









Hazah! I do still exist tongue.gif 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)
CODE

total = 7 + 2 # 7 and 2 are just an example.
print total

5)
CODE

number_inputted = int(raw_input("Enter a Number: ")) + 7
print number_inputted

6) <type 'str'>
7) Yep (i did it in my example for 5)
CODE

x = float(raw_input("Input a number: ")) #you could cast it to be an int or something else


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
CODE

>>> bool("")
False
>>>

Otherwise it's True:
CODE

>>> bool("Scientific Linux Rules!")
True
>>> bool(" ") # note, there is a space so it's not empty.

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
CODE

>>> bool(0)
False
>>> bool(0.0)
False
>>>


Otherwise it's True:

CODE

>>> bool(0.001)
True
>>> bool(-9)
True
>>> bool(72)
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:
CODE

>>> 0 == 0.0
True
>>> "bob" == "bob"
True
>>> "linux" == "gnu"
False


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)
CODE

>>> "" != ":)"
True
>>> "Linux" != "Linux"
False
>>> 7 != 2
True
>>>


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.
CODE

>>> 3 < 5
True
>>> 7 < 2
False
>>> 7.2 < 7
False
>>> 7.2 < 8
True
>>>


Meet >, he's the greater than operator/symbol.
CODE

>>> 3 > 5
False
>>> 7 > 2
True
>>> 7.2 > 7
True
>>> 7.2 > 8
False
>>>


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 smile.gif

Kidding, since we don't have this symbol on our keyboards we use <= (makes sense right? smile.gif) so:
CODE

>>> 3 <= 3
True
>>> 3 < 3
False
>>> 2 <= 5
True
>>> 6 <= 5
False
>>>


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):
CODE

>>> x = 5
>>> x < 100 and x > 0
True
>>> x = -2
>>> x < 100 and x > 0
False
>>> x = 5000
>>> x < 100 and x > 0
False
>>>


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:
CODE

>>> name = raw_input("Enter your name: ")
Enter your name: Jessica
>>> not name
False
>>> name = raw_input("Enter your name: ")
Enter your name:
>>> not name
True


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:
CODE

>>> age = 19
>>> student = True
>>> student or age > 69
True
>>> student = False
>>> student or age > 69
False
>>> age = 70
>>> student or age > 69
True
>>> age = 95
>>> student or age > 69
True
>>>


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
CODE

>>> age = 19
>>> if age > 20:
...     print "You can drink alochol in america!"
...
>>>

(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.

CODE

>>> age = 30
>>> if age > 20:
...     print "You can drink alochol in america!"
...
You can drink alochol in america!
>>>


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:
CODE

>>> age = 19
>>> if age > 20:
...     print "You can drink alcohol in america!"
... else:
...     print "Awhh, sorry. You're too young to drink in america!"
...
Awhh, sorry. You're too young to drink in america!
>>>


Maybe you want to check multiple conditions, you can do this using elif (short for else if):
CODE

>>> age = 19
>>> if age > 20:
...     print "You can drink in both the UK and America!"
... elif age > 17:
...     print "You can only drink in the UK"
... else:
...     print "Sorry, you can't drink in the UK or America!"
...
You can only drink in the UK
>>>


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:
CODE

>>> name = raw_input("Name: ")
Name:
>>> if not name:
...     print "You didn't enter anything, please enter your name"
...     name = raw_input("Name: ")
...
You didn't enter anything, please enter your name
Name: Jessica
>>>


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)
CODE

>>> age = 5
>>> age < 10 and (age > 5 or age == 0)

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 smile.gif - 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.
PMUsers Website
^
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll