sinan islekdemir

Amazon Tortoise logo - pixabay license

Hi! My name is Sinan Islekdemir and I am a developer. (Currently living in Helsinki / Finland and working for the Space company called ICEYE ).


python-for-excel-lovers

This is a work-in-progress and this page will be updated often. I have created this page on 25th of August 2018.

This series of tutorials aim to teach Python programming language to people who are already familiar with SpreadSheets, Excel or other kinds of tabular data editors. Main purpose of this approach is to be able to use common metaphors between two things.

What is Python and why Python?

To be honest, I can admit that I am not the greatest fan of Python Language. Like any other programming language (except assembler, it is pure perfect, and maybe brainfuck as well?) Python also has pros and cons. But on the long run, lets admit that it has a plain syntax. Easy to understand. And also, Zen of Python makes it pretty straightforward.

"There should be one-- and preferably only one --obvious way to do it."

Furthermore, it is easy to get started. Not many requirements. Still, it has some downsides while teaching. For example, it is a little but frustrating to tell why there is a self definition at

    class A(object):
    def a_function(self, arg1, arg2):
        pass

class method a_function. But it is alright. I can live with it. Still, it does not have any flexible weird keywords like ruby. (if, unless, one liners, ternary operator or different ways of looping)

Let's get started. For the beginners, I recommend IDLE Editor. Which is the tiny little code editor/runner comes along with standard Python Installer. Just go ahead and install Python (3.x) from https://www.python.org. For Windows I do not have a wide knowledge but it is pretty easy.

http://www.configserverfirewall.com/windows-10/install-python-3-windows-10/

For Linux and Mac, you are blessed. You probably already know how to do that. Hats off to Linux users!

Variables. Wtf are they?

Ok, to begin with, I will confuse your mind a little bit. But not to a very deep level. There are of course a lot of things you will have to learn along the way like memory models, memory paging, pointers or so. But they are just too much to begin with.

Imagine that you have an empty room, which is the available free Ram on your computer. And you want to store things. To keep things tidy, and easy to find, you have to put all the things into separate boxes. Each box has a name, and they contain something. Those boxes are called variables. And they contain different types of data. They can be Integers, Floats, Strings, Lists, Tuples, Dictionaries, or complex objects. Doesn't matter. What matters at this level is, everything has a box. (Not really but keep it this way for now. Some boxes does not have to have a name, some just are temporary and not visible to you, so on. But they are not your concern. Not yet.)

So, you are familiar with tables, sheets, excel documents. Therefore, what we call a primitive type is a single cell on your sheet.

    A    |  B  |
----------------
0 |  100 |      
1 |      |

In the example above, we have "100" in cell A1, which we can translate to Python as:

A1 = 100

Simple isn't it? Go ahead! Open IDLE, and from the menu, File -> New File create a new Python file. You will see an empty editor now along with Python Shell. Write the above line to that file, save it, and from Run -> Run Module (or simply F5 from the keyboard), run your first program.

Yes, nothing happened. Seriously? What did you expect to happen? We just put a data inside a variable. Thats all. We haven't done anything with the data yet. If you want to see an action, do this:

A1 = 100
print(A1)

Yes, it printed 100 to screen! Now you can feel yourself like Kevin Mitnick and take the blue pill, or you can feel stupid and curious to learn more, and stay.

Good, you are still here. Just as you realized, print command prints anything you give to that function. Yes, it is a function. But what is a function? Nope, not there yet. Just know that print prints what you want to print to screen. One step at a time.

A primitive can be

Integer is, well it is integer. It doesn't have any decimal places. A1 was an integer. But if you write A1 as:

A1 = 100.0

it becomes a Float because now it has a decimal place, even-though it is 0. How Computers handles numbers is different for Integer and Float. That is why they are different. Floats have "precision". Integers do not.

Note that, for the sake of coding conventions (I will tell more on this later) I will write variable names with lower-case letters such as a1 instead of A1.

Here is an interesting example for you:

a = 100
b = 3
c = a / b
print(c)

Now, what do you expect to see here? 33.3333333 or 33? The answer is, 33. Go on and try for yourself.

Now let's change the code a little bit:

a = 100.0
b = 3.0
c = a / b
print(c)

And this time, you will see "33.3333333333".

And there comes the String type. It is actually series of letters. A Text to be more precise.

a = "Sinan"
print(a)

is an example of String. It will print Sinan this time. Here are some interesting facts about String type. You can join two Strings just by using + operator. And also did you notice one thing?

RULE: Strings are wrapped inside " or ' characters where Integers or Floats did not have that.

a = "Sinan"
b = 33
c = 100.0

Main reason is to make values and variables different. There should be a way to differ a variable from a value. Like this:

Sinan = 3
a = Sinan
print(a)

would print 3 . Because Sinan is the name of the cell in your excel. Print, prints the value inside the cell. To get rid of this confusion,

Sinan = 3
a = "Sinan"
print(a)

will print "Sinan" because there are Quotes wrapping the variable value. RULE: Variable names CAN NOT start with Numerics. For the same reason. 5 = 2 nope. Thats not possible.

a = "Sinan"
b = "Islekdemir"
c = a + b
print(c)

Note: it printed "SinanIslekdemir" without a space in the middle, right? The reason is, space is also another letter which is missing from our case. You can simply fix this problem with to simple ways.

a = "Sinan"
b = " Islekdemir"
c = a + b
print(c)
a = "Sinan"
b = "Islekdemir"
space = " "
c = a + space + b
print(c)

Which ever you like. For me, none of them. There are obviously more elegant ways to join strings. But I should also mention here that, first method above is the best way in terms of simplicity and performance.

What about Boolean? It is actually a little more complex. We will get back to that later while learning Condition operators. For now, know that it can be only two values. True or False. (Unless you have a Quantum Computer, I wish I had a Quantum Computer)

Now, about the operators, Integers are "Numerics" and String is "Alphanumeric", which means you can not do this:

a = "sinan"
b = 99
c = a + b

It will return an error as:

TypeError: cannot concatenate 'str' and 'int' objects.

They are apples and oranges. Float and Integer are like green and red apple. They look different but still apple. But String is obviously orange. Except one thing, if you are sure what you are doing, you can convert one thing to another. Lets say we have a variable called "year"

year = "2018"

and it is defined as String. But we are sure that it is an Integer as well. So, we can safely say this:

year = "2018"
real_year = int(year)
age = real_year - 1986
print(age)

will print "32". Here is a list of mathematical operators you can use:

+ : Addition
- : Subtraction
/ : Division
% : Modulus
** : Exponent (2**2 = 4)
// : Floor division. (Probably will not use within these tutorials, maybe partly in Data Science)

Enough for the Primitives. Let's do something more complex. If we return back to Excel at this point;

    A    |  B  |
----------------
0 |  100 |      
1 |  101 |
2 |  102 |
3 |  103 |

Now we have a column with more than one values. We call this a "list" in Python. Variables does not have to be only single values. They can be more than one, a list of values.

a = [100, 101, 102, 103]
print(a)

will print all of them.

RULE: In computer science, unlike Excel, row numbers start from index 0. Not 1. Try this:

a = [100, 101, 102, 103]
print(a[0])

will print 100. List operations look a little bit like Excel.

a = [100, 101, 102, 103]
print(a[0:2])

will print [100, 101] (index from 0 to 2 excluding 2.)

a = [100, 101, 102, 103]
print(a[2:])

will print [102, 103] (data staring from 2 but not to a given end.)

a = [100, 101, 102, 103]
print(a[8])

will give an error. "IndexError: list index out of range" which means that the index you tried to reach does not exists.

Ok ok, I got your question, don't ask: And we can also define something like this in excel:

    A        |  B
---------------------------
0 |  Name    | Sinan     
1 |  Surname | Islekdemir
2 |  Age     | 32

So how can we define this in Python? This is called Dictionary.

a = {}

Yes, not normal brackets [], this time we use curly brackets {}. This is a RULE. You define Dictionaries with {}. Lists with []

my = {}
my["name"] = "Sinan"
my["surname"] = "Islekdemir"
my["age"] = 32

Noticed something? Yes. Properties like "name" and "surname" are also wrapped inside quotes. "". Why? Why not? They are cell values in Excel remember? And if they are String, we wrap cell values inside Quotes. Thats why.

ATTENTION!(Thanks to a brilliant eye) You can not call Dictionary values like List values.

my = {}
my["name"] = "Sinan"
print(my[0])

Would not work. Dictionary is not something stored row based. It is stored on keys. ("name" as a key in this case.)

my = {}
my["name"] = "Sinan"
my["surname"] = "Islekdemir"
my["age"] = 32

your = {}
your["name"] = "John"
your["surname"] = "Doe"
your["age"] = 30

average_age = (my["age"] + your["age"]) / 2
print(average_age)

Noticed something? Yes, I put my["age"] + your["age"] inside parenthesis! like: (my["age"] + your["age"]) I did this to define the order of mathematical operations. Otherwise, it would first divide your["age"] with 2 then add it to my["age"] , which would result with 47. And we can get things more complicated now. How can we define a sheet full of person records? Do we have to define a different variable for each person?

Yes.

No, just kidding. That is not possible. Instead, you can combine things. Use your imagination!

people = [] #  yes a list
people.append(
    {
        "name": "sinan",
        "surname": "islekdemir",
        "age": 32
    }
)

people.append(
    {
        "name": "john",
        "surname": "doe",
        "age": 30
    }
)
print(people)

Will print [{'age': 32, 'surname': 'islekdemir', 'name': 'sinan'}, {'age': 30, 'surname': 'doe', 'name': 'john'}]

Having fun? Now, Quiz of this tutorial:

Define a sample data structure for a library. Yes, a library.
Hint: This is possible in Python:

a = {
    "i_am_a_list_of_dictionaries": [
        {
            "name": "sinan",
            "i_am_another_dictionary": {
                "info": "..."
            }
        }
    ]
}

Use your imagination. A library has sections, in each section, there are authors and every author can have more than one books and each book has some information like ISBN# or title or number of pages. Be creative! Dive deep!

What about the SUM or that kind of operations?

Ok, as I have said, this is a tutorial for those who are familiar with Excel. So, probably the first question comes to mind is how to calculate the sum of a series or other operations on lists.

Assume that we have a sheet defined as:

       A     |     B
---------------------------
0 |  1000    |
1 |  1002    |
2 |  2003    |
3 |  6423    |
4 |  9438    |
5 |  1234    |

Which can be written in Python as:

a = [1000, 1002, 2003, 6423, 9438, 1234]

And you want to do some basic arithmetic operations on this list;

       A     |     B
---------------------------
0 |  1000    | =SUM(A0:A5)
1 |  1002    |
2 |  2003    |
3 |  6423    |
4 |  9438    |
5 |  1234    |

Would probably be your formula on a stylesheet. In Python, there is a long way and a short way to do this calculation.

Here is the long way:

a = [1000, 1002, 2003, 6423, 9438, 1234]
total = 0

for number in a:
    total = total + number
print(total)

Yes! We have a keyword for here. As you can understand, it walks (we call it iterates) over your list and in each iteration, it puts the number in the cell in number variable. That means; total = total + number line will be executed 6 times for each number (item) in the list. We can shorten this as:

a = [1000, 1002, 2003, 6423, 9438, 1234]
total = 0
for number in a:
    total += number
print(total)

Which += is a short form of total = total +. You can use this short add operator if you are adding a number to its own. The same applies for -= or /= as well. Anyway, luckily, as I mentioned, there is also a short-cut for this operation.

a = [1000, 1002, 2003, 6423, 9438, 1234]
total = sum(a)
print(total)

Yes, there is a magic function for this list operation, which is sum. There are also different keywords you can use. Here is a list of them:

Length of a list:

a = [1000, 1002, 2003, 6423, 9438, 1234]

# Number of items in a list
count = 0
for number in a:
    count += 1 # we have 1 more item
print(count)

# OR
count = len(a)
print(count)

Maximum number in a list

a = [1000, 1002, 2003, 6423, 9438, 1234]

# Number of items in a list
maximum = 0
for number in a:
    if number > maximum: # Yes, we have "if" keyword, I will explain later
        maximum = number
print(maximum)

# OR
count = max(a)
print(maximum)

Minimum number in a list

a = [1000, 1002, 2003, 6423, 9438, 1234]

# Number of items in a list
minimum = a[0]
for number in a:
    if number < minimum: # Yes, we have "if" keyword, I will explain later
        minimum = number
print(minimum)

# OR
count = min(a)
print(minimum)

So, there is a VERY IMPORTANT lesson to be learned here.!

INDENTATIONS! (CODE BLOCKS)

In modern programming, codes are divided into blocks Or Scopes you might say. Every logic has a block. This can be a for-loop or an if statement (which is described below) or a function or class or whatever. Everything in programming has a scope. In Python programming language, scope is defined as the codes which starts at the same column visually. (Same Indentation). Even global is a scope. What is a scope? Well, it is best to try and learn instead of long texts on it.

Let's head back to the previous example.

# Code 1: Normal code,
a = [1000, 1002, 2003, 6423, 9438, 1234]
total = 0
for number in a:
    total += number
print(total)

# Code 2: Broken Scope
a = [1000, 1002, 2003, 6423, 9438, 1234]
total = 0
for number in a:
total += number # ---> !ATTENTION, the line is not indented.
print(total)

Example code Code 2 will give an error message as: IndentationError: expected an indented block because for operation is expecting to have a block of code to run below it. But below the for operation, there are no blocks. Due to the indentation, total += number is in the same block with for operation, it is NOT a block OF the for operation.

Also, we can extend this error like this:

# Code 1: Normal code,
a = [1000, 1002, 2003, 6423, 9438, 1234]
total = 0
for number in a:
    total += number
    print(total)

Now the code will not give an error. But it will give an output as:

1000
2002
4005
10428
19866
21100

Because, now print command is ALSO within the for block. So, in each step of for walking, it will print the existing state of total variable. Interesting right? For more complex operations, you need to write some more codes but again, some of them are already defined in libraries like numpy. But it is still too early to mention them.

IF Statement

Now, I do imagine that you are curious about what if keyword does up there? Not curious? Probably you have already understood what it is. If, as it is pretty clear that is a control flow statement. If the given condition is satisfied, code within the block gets executed. Nothing more. Here is a basic example from (https://docs.python.org/3/tutorial/controlflow.html)

x = int(raw_input("Please enter an integer: "))

if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')

Note: There is another function here. Which is input. It basically stops the execution at that line, waits for you to enter a text from the keyboard and assigns your keyboard entry to variable.

name = raw_input("What is your name? ")
print("Welcome " + name)

is the simplest example. So, as here, we know that the user will enter an integer so we can cast this string to integer by int()

Now, go ahead and play with the code of if statement.

Having fun? Now, Quiz of this tutorial:

Write a simple joke application. Expected output should be like:

Hello, what is your name?
Welcome, Hakan!
How old are you?
You are legally adult then! (+18)
Do you know that I can count? Give me a number between 1-100: 13
1 2 3 4 5 6 7 8 9 10 11 12 13
And here are the ODD numbers above:
2 4 6 8 10 12

Hint:

sum_upto_ten = 0
for number in range(10):
    sum_upto_ten += number
print("Sum of numbers up to 10 (except 10) is %d" % sum_upto_ten)

Here is a footer to please visitors' user experiences. - Sinan Islekdemir