Sunday, March 8, 2020

How to become a python coder, first part


What is python 

Python is a high level computer programming language easy to learn and put in use by anyone who is passionate about coding. The first time I started reading some python code it felt like i was reading myself, I mean my thoughts converted to code.
Heavily used by Google and Youtube, python is becoming very popular among computer programmers all over the world espcially in the startup world as it features rapid development, which makes it one of the number one technologies when it comes to prototyping.  

Why learn python

Many of you may ask why learn python and not java or another programming language. This is the right question to ask for a starter in the coding world and I will list some arguments below why you should start learning python.

It will take you probably two weeks to learn all the python syntax needed to build real world applications. Or just a few days if you are a passionate reader about these kind of topics.

The fact python is easy to read makes it easy for you as a developer to maintain the code of your project. As the project grows in terms of code you don't want to come back after your holidays and when looking at the code you have written not getting shit on what it does.

I have had the chance to talk to a python developer who did perl programming for a living back in the days and he told me it is real hard to maintain the code of his old projects. Not hard, but struggle!

Python is fully Open Source which means that you can look at its implementation code and understand stuff from a lower level perspective.

You can code python on almost any platform. Telling from my experience, I have been able to run my python scrips in both Windows and Linux operating systems except for Mac, because it is such an expensive toy for me, but for sure you can code python on Mac too.

Python is not just a programming language. It has a big community behind who is very welcoming and it makes you feel like Home. I am very happy to have met so many python developers in Firenze Italy back in 2013. I felt real love in there.

Some technical knowledge on python

You can notice a python file by the .py extension. Every file that ends with this extension is classified as a python module. For example the following is a python module.
pythongeek.py
The module serves as a container for the python code of our app and we can use the code of a module in other modules by importing it with the import statement like shown below, but do not try to understand this thing right now as this is just an introduction. I am sure some things may not make sense to you right now and that's alright.
import pythongeek # imports the pythongeek module
Since python is an intepreted language you will need the official python intepreter to run python code on your machine. In most linux distributions such as Ubuntu it comes installed by default, but in Windows you have to install it yourself.

Install python on windows

Installing python on windows is just a matter of clicks, but configuring it to run from the command prompt needs some configuration.

By running python from the command prompt I mean the following.


The first thing we need to do is to find out our system type so we know what kind of python installer to download.

To find out your system type go to Start, right click on Computer and go to Properties; the following will come up.



As you can see from the above screenshot I have a 64 bit machine. I need to download the python installer for 64 bit system type.

Now go to the python official website and go to Downloads. Under Downloads click on the Windows category.

Depending on the system type you have, you need to download the right version for it. In the following screenshot everything is self explanatory so take a look at it and click on the python installer for your type.

Note: The version of python we need is 2.7.11



Download the right version of python for you and move it on Desktop like shown below.


Then do a right click on the python installer you just moved to Desktop and click on Install. The installation of python will start.

The following box will show up.




If you don't want to make python available to other users then just select the second option and click Next.

I prefer to leave this as default.

The next box will ask you about the path where you want python to be installed. Please leave this as default as you are new to this stuff.




The C:\Python27\ is the path where our python installation is going to be placed. We will need this path for some configuration so make sure to save it in a notepad file.

C:\Python27\ 
Then the following will show up.



Click Next.


The installation will start.





When you are close to finish, just click finish.



To be able to run python from the command prompt, it is needed that we configure the PATH environment variable.

1. Go to Start, Computer, Properties.

2. Click on Advanced system settings.


3. The following will come up.


4. Click on Environment Variables.


5. Under  System variables, look for PATH and double click on it.


6. The following will come up.


7. At the end of the Variable value copy and paste the following and click OK.
;C:\Python27
8. You are done.

9. Open the command prompt by going to Start and on the search bar type cmd. Run it and check if it is possible to execute python directly from it.



I also uploaded the following video on youtube that teaches you step by step how to install python on a windows 7 machine.


Some python coding basics

Now that you have python available on your machine, it is time to build some basic coding skills to build the foundations for further development.

To open the python shell on windows just go to Start and in the search box type python. Then click on IDLE (Python GUI). The following python graphical shell will come up.




This is the shell where we are going to practice python.

The first thing you need to learn in python is how to declare a variable. You can declare a variable in python like shown below.

a = 5

Where a is the name of the variable and 5 is the value this variable is linked to.




To get the value of the variable you type the name of it and hit Return (Enter) on your keyboard.

a

You can also do some maths.

a + 1 # hit Enter


Let's get more pythonic. There are special builtin datatypes in python such as the following:

  • Lists
  • Tuples
  • Dictionaries

There are many others, but for the purpose of this tutorial we will take a look at the ones listed above.

Let's take a look at lists. For example the following is a list assigned to variable l.
l = ['python', 'geek', 'google', 'youtube']




Lists elements can be accessed and used in a program. For example to access the first element of the list you use the following syntax.
l[0] # where l is the list and 0 is the index of the first element




So to access the second element you do the following.
l[1]


A list can be updated. For example you can change the first element in our list by doing the following.

l[0] = 'ruby'

As you can see, when printing the new list the first element is updated.

l



We will cover lists in more depth in specific tutorials. Now it is time to learn about tuples.

You can define a tuple like shown below.
t = ('python', 'geek', 'internet', 'skype')




The same as in lists you can do indexing in tuples too.

For example to get the first element of the above tuple you do the following.
t[0]




So tuples are like lists? Not really!

Try to update the first element of the tuple we have and see what happens.
t[0] = 'ruby'


Enough about tuples.

Let's move to dictionaries. For example the following is a python dictionary.
d = {'name': 'pythongeek', 'occupation': 'maths', 'age':13}


Each item of the dictionary is composed of two elements: a key and a value. For example the first item of our dictionary is composed of the key 'name' and the value 'pythongeek'.

Try the following.

d['name']



As you can see you access the value of an item by its key, python is very cool. Now if you think we are done, I am sorry but you are completely wrong!

We have to learn about functions now. As in math, a python function takes an input and returns a value.

For example the following is a math function.
y = f(x)
The value of y depends on the value of x and what kind of function f is. A python function is defined with the def statement followed by the name of the function and a colon like shown below.

def py_function():
    print('Python Coder')

I will explain the above function in details, but for now open a new python graphical shell and type the followings one by one.
def py_function():


Once you have typed the above, hit Enter on your keyboard and type the rest of the function like shown in the following screenshot.


A thing to notice in here is space from left to right that was created when you pressed the Enter button on your keyboard. This space is real important when coding python and it is called indentation. It's purpose is to make the code more readable.

When you are finished coding the function hit Enter again. Now you are wondering how to use this function.

To use a function in python just type the name of the function with () and the input it takes inside them.

Because our function does not take any input we just run it like shown below.

py_function() # calling our function

Conclusion

There is enough information you have been through in this first part. In the second part we will take look at more advanced stuff, but I am sure you will be ready for it if you feel you understand what we covered in here. For anything which you don't understand, feel free to leave a comment in the comments section of this post and I will be more than happy to help you.

0 comments:

Post a Comment