Let’s Code In Python

Part 1

Python is a fairly easy language to learn. I highly recommend it for a beginner with no previous programming experience. You can grab the latest version of Python form here. Go ahead and download the 2.7.x version of Python. I use this version, be warned though, if you try and run code in meant for 3.x.x in 2.x.x, you will surely run into trouble.

Lets start. Fire up the Python Command Line. It will look something like this.

Python Interpreter

This is the Python Interpreter a very power full tool. Python is an interpreted language, what this means is that it interprets all the instructions fed into it and spits out the result. Let’s play a little with the Interpreter. Go ahead and type 1+1 into the Interpreter and hit enter. You’ll see something similar like the picture below.

Simple Math

So what happens? It adds both the numbers. This makes the Python Interpreter very interactive. Similarly try multiplication (*) and division (/). Fun right? I know, I know you want to do something better than that. Let’s move on to variables.

Basic Variables

If you’ve every met a programmer, you might have come across the term variable. I like to explain it this way. Imagine you have a box, say we name this box as Box A, now we put something in the box. The box thus is a storage device. In the similar manner we say that a variable is a storage device in program. A variable stores things called as Data. There are different kinds of data. These various kinds of data are collectively known as data types.

Let’s look at some of the basic data types in python.

  1. int — Integer Data Type (e.g. 1, 99, -23, 983, etc.)
  2. float-Floating Point Data Type (e.g. 2.313, -0.23, 3.14, etc.)
  3. string-String Data Type (e.g. “Hello”, “A sentence”, “Logout”, etc.)

There are other data types also, but for sake of simplicity lets start with these. Now how do we assign values to so called variables. It’s pretty simple, you start with the variable name followed by the ‘=’ sign, followed by the value or data. Lets look at declaring some variables in Python. Enter these into the Interpreter hitting the enter key after typing each line.

  1. a = 1
  2. b = 3.142
  3. c = “Example String”

Your output should be like the picture below.

Variables in Python

Congratulations you’ve created your first variables. Let me explain. You created 3 variables with the names a,b and c. Each variable stores a different datatype. It’s not very hard to see form the picture what kind of data these variables store. Let’s take variable ‘a’. Now we’ve ‘assigned’ a value to this variable and that value is 1, what this means is that this variable stores the integer datatype. Next let’s look at variable ‘b’. It’s pretty clear that it stores the floating point datatype which is 3.14 (it’s PI). The last variable ‘c’ stores a string data type of the value “Example String”.

It’s good to know that Python is a dynamically typed language, what this means is that you don’t have to specify the data type of the value your assigning to a variable. Python automatically detects it. So you don’t have to worry about what kind of data is assigned to a variable. You can seamlessly assign another data type to the same variable and Python would assign that value to the variable without complaining. Dynamically typed languages also have the advantage of automatically assigning variable sizes. That’s right each data type can store a very specific size. Dynamically typed languages do know no bounds in variable size or in other words the User, which is you need not worry about the data type or size.

That’s what makes Python a really amazing language. That’s all for this part. I know it’s kinda childish, but hey, I’m assuming no programming experience here. Don’t forget to follow or leave a comment. Didn’t understand something? I’m here to help.