It’s time to start some actual programming. I will be using Windows and Linux interchangeably as time goes on. Don’t worry about it, just type the same command into your console irrespective of your Operating System.
We talked about installing Python in the intro. However, if for some reason you can’t install it on the machine you’re using, I recommend using an online Python editor and command line such as this one.
Start Python in Command line
Open your command line and type python. You should see something like this:
This is interactive in the sense that, when you type python code into it, you get the response instantly. Later, we will use an editor as we talked about in the Intro .
The infamous hello world!
It’s time to type your first code. Type the code below into your console:
print 'hello world!'
hello world! to you. Oops! You’re not impressed? C’mon this is python, it makes life easy. You don’t need to think too much to make it output hello world. Don’t worry, a whole lot awaits you, you’ll be more than impressed.
You just instructed python to output the string “hello world” to the console.
Integers and Calculations
You do realize that we encompassed the hello world in quotes. That’s a string, but let’s talk about integers. Integers are not enclosed in quotes. They are written as they are. Examples include: 1,2,0,-5,5000,2345, -1000.
print 1000 into your python console. It outputs 1000. There’s no need for quotes.
Now let’s do some arithmetic. Take a look at the table below.
|Operation||Math Operator||Programming Operator|
It is the same for almost every programming language with the exception of the exponent operator. Most of the other languages do it differently.
Now that you know the operators, let’s apply some of them and see how it works practically.
1 + 1 into the console and watch python do your math for you.
Now that was cool right? Try something complex. Maybe
2**4*(4*4-5) (Don’t try division yet).
Are you having fun yet?
Division can be tricky with integers
1/2 . It seems python is dumber than a 3 year old huh? Well, that’s wrong, Python is just too “logical”. You are asking for an integer answer to the division of two integers. That is, 0.5 isn’t an integer so Python gives you 0 as the integer answer.
Yes, 0.5 is a float. In order to get divisions right, you need to make one or both numbers a float. Now try
1.0/2. Right huh? Great!
Be creative and play around
Now, try any calculation and see it’s output. Explore, programming is more of practice than being taught. Post your issues as comments and I’ll respond.
Integers, Float/Double and String
These are what we call Datatypes. Integers are all numbers without a floating point. Floats or doubles are numbers with floating point, such as 1.0, 5.432, 3.14. Strings are alphabets and symbols (and even numbers) enclosed in quotes (single or double). Example: “Hello world!”, ‘+233’, “12345”.
Remember you cannot do correct arithmetic with numbers that are strings. For example ‘1’ + ‘1’ will not give you 2. You will get an out put of ’11’ which is also a string. That’s string concatenation (We’ll talk about this in a later post).
Something to think about
Type the code
name = "Sam" hit enter and type this:
"My name is" + name + "I am a " + "programmer!" . Take a close look, We are adding strings and something like a placeholder called name! More on that in our next tutorials.
Practice, practice, ask questions, practice some more and don’t give up. You are now a programmer!