Let’s Code In Python

Part 3

Hi guys, I know it’s been a while since I posted anything here. Frankly, I was busy with college and stuff, now that I have some free time let’s continue with our Python adventure.

Just a quick recap of the previous part, we learn’t about variables and keywords, the differences between them and how variables can be named. We also studied the different data types that Python supports. Now that that’s that, lets begin with today’s topic.

Input in Python. Let’s take a closer look at what input actually means. When we look at it in terms of computer science, it means to enter data into the computer. Say, for example you’re eating food, the food that goes in is the input and the energy that comes out is the output. Input can be understood in many way’s, take another example of a car, the fuel is the input and horsepower is the output. The same goes for a computer, you give it some data, it performs the required operations and spits out the result also known as the output. Let’s see how we can make use of this in Python.

Go ahead and fire up the IDLE, if you don’t know or have no idea what that is I highly recommend reading my previous article. Let’s set some objective’s, because objectives are cool and they help you make sense of what you’re going to do or achieve or solve. So let’s go ahead…

  1. Create a variable ‘a’ (or any name you like) and set it equal to 0.
  2. Prompt the ‘user’ for an ‘integer’ input and store it in variable ‘a’.
  3. Print back the value of the variable, i.e. give the ‘user’ an output.

Here’s the code:

a = 0
a = raw_input("Enter an integer: ")
print a

Then save it and run it. You should see something like this…

Go ahead and type in your favorite integer and hit the Enter key. Magic!, isn’t it? It should then print back the same value. Cool, right? Don’t worry if you didn’t understand what you just typed in, let me explain.

Lines 1 and 3 are pretty simple to understand if you’ve been through the previous parts. Let’s get to line 2. Now here we come across a ‘method’ called raw_input() and it has a ‘parameter’ of the string type which say’s “Enter an integer: ”. So what’s a ‘method’? Let’s break it down. A ‘method’ is a small chunk of code that carries out specific tasks or functions(that’s what they’re actually called), like a recipe. You’ll understand it better when we compare them both. You decide out of the blue you want to bake a cake, having no knowledge of baking. So what do you do? You rush to the nearest book store(or website) and find a recipe that fit’s your needs. Now the comparison starts. Let’s say the ingredients are the ‘parameters’ and the method which you follow to bake, or procedure(yep they’re known as that too.) is just what it is a method. So basically what raw_input() is doing is that it waits for the user to input something and hit the Enter key. It then processes it and ‘returns’ the result of the operation it performed in this case taking the input from the ‘user’. It’s just like baking cake, except the result of baking a cake, well, is a cake. That’s enough about method’s for now we’ll go in depth later on in this series.

That was about inputs in Python. Let’s talk about conditional’s and conditional structure. If you’d remember way back in high school, you learn’t Boolean algebra. That topic forms the basis of entire computer science. If you don’t remember, here’s what the basics are…

  1. TRUE AND TRUE make TRUE
  2. TRUE AND FALSE make FALSE
  3. TRUE OR TRUE make TRUE
  4. TRUE OR FALSE make TRUE
  5. NOT TRUE is FALSE
  6. NOT FALSE is TRUE

In the above 6 points pay attention to the AND, OR and NOT. These are called Boolean operators.These are just the basic ones there are many more, but let’s learn these for now.

The first question you should ask yourself is how do I perform these operations in Python?. Python has a set of conditional operators built in. They are ‘==’ known as the double equal, used to check if two data items are equal or same, ‘!=’ know as the not equal, used to check if two data items are not equal or not the same. As for the Boolean operators above Python has keywords like ‘and’, ‘or’ and ‘not’. Let’s play with these in the shell, go ahead and fire it up. Type in these…

1 == 1
1 == 2
1 != 1
1 != 2
1 == 1 and 1 != 2
1 == 1 and 1 != 1
1 == 1 or 1 != 2
1 == 1 or 1 != 1

The output you get should agree with the six points above and may look something like this(you CAN change the integers).

That’s all about conditionals. But programming isn’t complete without conditional structures. That’s the whole point of programming you know. So let’s take a look at the conditional ‘if-else’ structure. It looks something like this…

if <some condition>:
statements to be executed if condition is satisfied
else:
statements to be executed if condition is not satisfied

So that’s the syntax for the conditional ‘if-else’ structure. Note the indentation, it’s very important in this language Python. You can only have one kind of indentation at a time i.e. you can’t use both spaces as well as tabs in the same script file, it’s either spaces or tabs but not both. Let’s write a script using this new stuff we learn’t. So these are going to be our objectives(cause objectives are cool)…

  1. Create an arbitrary variable and set it equal to 0.
  2. Prompt the user for an integer input.
  3. Check if the integer is bigger than 50.
  4. If it is we print ‘That is a big number’.
  5. Else we print ‘That is a small number’.

That’s about it. Go ahead and fire up IDLE and create a new script. Type the following into it…

a = 0
a = input('Enter an integer: ')
if a > 50:
print 'That is a big number'
else:
print 'That is a small number'

Notice how we use the ‘input()’ method instead of the ‘raw_input()’ method. The reason for this is because ‘raw_input()’ returns a ‘string’, so if we used that, it would not give us the right output, because hey a ‘string’ and an ‘integer’ data type are always different and hence will never be equal. The ‘input()’ method returns an ‘integer’ data type which is what we want.

That set aside, save it and run it. You should see something like this…

So what exactly is going on here? Let me explain. The first two lines are pretty straight forward. You initialize a variable and you ask the user for input and store it in that variable. The fun part is in the next few lines. It’s pretty easy to explain also. You can literally picture the computer asking itself ‘is the given number bigger than this number?’. If the computer receives a ‘True’ viz. a ‘yes’ in terms of a computer language, it will execute the statements in the ‘if block’. A ‘block’ of code in Python can be considered as having the same amount of indentation. Since all statements following the ‘if statement’ have the same indentation, we can say it belongs to the ‘if block’. The same can be said for the ‘else block’. Back to where we were…if the computer receives a ‘False’ viz. a ‘no’ in terms of computer language, it will execute the statements in the ‘else block’ and accordingly display the right output. In my case, since I input 56 viz. bigger than 50 it rightly outputted ‘That is a big number’ of the ‘else block’. So yeah, that’s pretty much all you need to know for now. Please do remember we aren’t only restricted to a ‘>’ sign, we can use any of the signs we used for conditional operators above. I’ll dive more deeper in the next part.

That’s all for this part. I like to keep it short, because you learn well in small steps. So, for the next parts I’m planning to write on the ‘If-else’ ladder and also the all important ‘looping structures’. Till then keep playing around with what you’ve learnt and I’ll see …uh… expect to be viewed next time. :)