thekrtek.net Thoughts on Security & Technology

18Sep/110

Beginning Python: Lesson 2

Well, it's a Thursday evening and I'm bored, so time for another iteration of Beginning Python! We learned the following last week:

  • Setup of Development Environment
  • Variable Types
  • Operands/Operators (Math and Strings)
  • User Input with raw_input()
  • Testing Operands
Today, we're going to move into more useful topics like loops, functions, and evaluative statements. To get right to it, let's start with evaluative statements and loops.
If you've programmed before then you should be somewhat aware of statements like if, else, while, for, else if and so on. If you haven't programmed before, not to fear, these concepts are quite easy to understand!
  • if statement - evaluates "if" a value on the left coincides with one on the right, such as if 1 = 1 then do
  • else statement - follows after the "if" statement, so if 1 does not equal 2, else, do this now. Basically, if the statement doesn't return true for the "if" statement, do something else.
Before we go on to loops, let's go over if and else. If (pardon the pun) I wanted to make a simple set of code to check a variable and handle it accordingly I would do:
i = 2
if i == 2:
   print 'the value of i is 2!'
else:
   print 'the value of i is not 2!'

What the above block of code does is check (with ==) whether variable i equals the value of 2, if it does then it prints 'the value of i is 2!' in a case where a user would be entering input the else statement or another if statement could be used to take user input and control the flow of the program accordingly (such as a menu). When working with if statements we can use some of the following operators to check values: == (compare two values, see if they match), <= (less than or equal to) >= (greater than or equal to) < (less than) > (greater than). Also if comparing two or more things in an if statement we have expressions like and / or .  Using the example above we can modify it to show this usage.

a = 2
b = 1
if a == 2 and b == 1:
   print 'the value of a is 2 and b is 1'

Onto some simple loops, while and for. The while looks does what it sounds like, "while a condition is met do the following". We can show this with the following:

i = 0
while i < 10:
   print i
   i += 1

So we start with creating a new variable, i, and assign it to the value of 0, then we say, while i is less than 10, print the current value of i and then add 1 to i. It will then loop back up the top and check if i is still less than 10. This loop will keep printing until i = 10 in the final loop. We will go over these in more depth, but on the surface they are quite simple.

Onto the for loop. for loops work by saying "for each item in x, do the following", we can rip an example of this from some code im currently working with:

for char in message:
    new_thing(char)

this says for each char (character) in the message (which is a string variable), call the new_thing function, and pass char to it. It is just as simple as that but when combining everything we've learned so far these items can become very complex and powerful.

More will come soon in this guides, this one isn't as in depth due to my limited time to write them. Once again, email me sbrichards [at] mit.edu if you have any questions or need help!

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.