Non-Developers Guide to the Python Programming Language

I am a software developer and a sociology student. I am writing this article as a sociology student and my intention for this article to be understood by everyone, not only developers.

Python is an interpreted programming language written in 1991 by the Dutch computer scientist Guido van Rossum. The most important feature distinguishes Python from other programming languages is that the whitespaces are also taken into the account in the grammatical structure of language in order to increase the code readability.

It means that you can also manage the flow of the program with spaces. In this way, it will be more suitable for scanning the written code by eye, and if you follow the proposed design principles, it will be easier to scale your project in comparison with other programming languages. I prefer Python because of its enormous range of libraries, rather than the technical implications of the language. Python has the widest range of the diversification of the libraries. It’s commonly used in data science, astronomy, or even in biology.

Programming language network
Programming language network
Python language and it’s influences in programming language network. Click this link for the colorful version.

You can watch the programming language network on youtube too.

Image for post
Image for post
Amazingness

About me

I was born in 1992. Since 2012, I have been giving presentations and workshops for Python language in various communities. I voluntarily taught basic web programming to many students and public officials.

I started to be interested in this language when I was in high school. I found a book in the library and started reading it.

Just by chance, I found a python compiler for the mobile phone that I use at these years. The phone had a Symbian Operating System. It was a Nokia phone. I was trying to learn the language with that phone in my classes. And I programmed a little game with it, which is a snake game. This game was the first application that I wrote and distributed to my friends.

Another widely used module of mine is NginxParser — a low-level closed language parser. A grammar that I wrote to parse the Nginx server’s configuration files. It is used in the project developed by EFF to automatically enable HTTPS on web sites.

While it may seem like a technical thing, what I was coding is actually a linguistic tool. I think programming languages have become our natural languages. We can program any entity in our life by just using the programming languages like we use our natural languages.

Image for post
Image for post
Noam Chomsky says, “a language is not only just words” or something like that

How to install Python

It comes with Python installed on GNU / Linux and OSX computers. On your Windows machines, you need to install it separately. For me this is the biggest difference between Windows and others. All the tools that make up an operating system can already be built with Python — and are made. What makes Microsoft — microsoft is that it does not come with Python installed in the operating system. I hope it will change one day.

You can install Python by downloading the appropriate installation package according to your operating system from the Python official website. Please don’t download the python compiler somewhere else for your own safety.

Python.org is the official web site of python language.

You can also use Fil if you want to exercise python in your browser. Fil is an application runs on your browser and has an ability to interpret Python and some other programming languages.

Image for post
Image for post
https://fatiherikli.github.io/fil

Data structures and variables

For learning a language, it is always best to look from the top to the down by making deductions. There are things we use in our natural languages to indicate something more explicitly, such as numbers, or dates. They are not part of the language, but they can even change the message in the opposite direction. Variables are the same.

I ate 3 breads today. Which means three breads were eaten.
I ate 0 bread today. Which mans no bread was eaten.
I ate -1 bread today. One didn’t eat any bread. Maybe produced it —

Maybe the one is a baker.

We must not forget that the program we will write is a message. So it has a receiver and a sender. You are the sender, and the receiver is the computer where the program will run. Since our computer does not understand the Python language, we need an interpreter. Python takes on the interpretive role here — which are categorically under the interpreted programming languages for exactly this reason.

Image for post
Image for post
We have three different types of variables in the example.

String (name on the example). Some sort of set of characters. I’ll leave a definition from the wikipedia here:

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation)

Integer (age on the example). To operate with numerical values, we must define variables of integer type so that we can perform mathematical operations on these values. If you define it as a string, you will have to waive these operations.

Question: Which data type should we choose for a phone number? Is it a string, or is it an integer?

Boolean (is_male on the example). We define logical expressions with boolean type. It means, the boolean type can store only two different values. For example, the one or the zero numbers, or a male or female, or “on” or “off” expressions. The answers for the questions such as whether if he is a man or woman, whether they have a children or not, whether are they alive or not should have an exact answer like True or False.

If you are dealing with artificial intelligence, you can define fuzzy answers, but the data you keep will not be Boolean, it will be a float number, (like 0.60), which means it does not have an exact answer, but with a possibility of %60, the conclusion is True. This is called Fuzzy logic, but let’s not think about it now. We will be dealing with Boolean logic in this article.

Logic is the most important part of programming languages. If you skip this part, you won’t be able to do anything. Go back to this subject again, go as far as you can, as back as to Aristotle’s logic.

Let’s make an experiment:

As you will notice in the example, you can apply logical operators on Boolean types. Both conditions are expected to be met on “AND” logical gate.

I live in Krakow and I am 28 years old. One of two conditions is expected to be met in the “OR” logical gate. I live in Krakow or I am 28 years old. In Java-style languages, the ampersand is denoted by two ampersand signs (&&). Python seems to be a more human language in this aspect.

Data structures: Lists

Although it corresponds to Arrays in other programming languages, Arrays are implemented a little differently on Python.

They are actually collections that you you can navigate in. You can store any type of data in these collections. It is even possible to keep collections in collections. It will be better understood by visualization on the fil application.

Image for post
Image for post
Click here to run the example

We have two collections on the example above. The names and natural languages are shown with a border with turquoise color as Fil recognizes them a collection. In the last one, we retrieve the first item of the natural languages collection and the result appears to be a string. It is a chosen element from the collection.

An important point is that, counting in programming languages starts at zero. Therefore, the first element the natural languages collection is Polish language.

Let’s continue.

Image for post
Image for post
Click here to run the example

To access only some part of a collection, we can perform an operation called slicing. For this, we use two dots (:) when choosing from the list. The snytax of this notation is the following: Start index (that the slicing will start): End index (that the slicing will finalize).

After slicing the days of the week and weekends in this way, we are assigning it to a new collection. We obtain a two-dimensional collection as seen in the visualization of this collection Fil.

Image for post
Image for post
Collection within another collection (Two dimensional collection)

Decision-making statements

Before moving on to the next data structure, we need to learn a few more basic things. These are the loops and conditions that are necessary to control the flow of the program.

Image for post
Image for post
Click here to run the example

The most conventional phrase for controlling the program flow is “if” in programming languages. In our example, we used an “if” decision statement to print different messages according to the languages the user uses.

If you have noticed, after specifying our condition and ending the line with a colon, we continue one tab on the next line. This means that if the condition is valid, continue with the following procedures.

If the condition is not valid, we define the next condition that will be validated with elif statement. We can define as many elif conditions as we want. In cases where it does not comply with any conditions, we finalize our decision making flow with the else statement.

Loops

In this example, we use another decision making statement, the for loop.

Image for post
Image for post
Click here to play with the example

In first part, we print all the languages in the collection one by one. Then we enumerate them with the enumerate function and print the index.

In the last part, we create new number collection with the range function. Just like the list slices, we can create the number sequence we want by giving the beginning and ending index. In the context of the loop, we wanted that number to print itself, its square, and its cube line by line.

Data Structures: Dictionaries

Dictionaries are one of the most common data structures in Python programming language. If you remember, we had to give an index to access the items inside in the lists we created. This is a little bit different in dictionaries.

The purpose of the dictionaries is to enable you to access the items in the collection with a key that you have previously determined, not a numerical index. Therefore, in some programming languages, this data structure is called as Hashmap.

You can really imagine this data structure as a dictionary, like its name.

Image for post
Image for post
Click here to play with the example

As you can see, Fil visualizes the dictionary data structure in a different way. There are a few things we need to pay attention to in this data structure.

First thing, we made a reassignment for shark so that its value is hai. This is not visible in the second visualization on the output. This is because we can store only a single value with the same key.

The other thing is that while making the definitions, we define the “shark” item the first, but we see it at the very bottom of the table on the visualization. This is because of the implementation of the data structure. Dictionaries are unordered data structures, unlike lists. If the order of the items in your data is important, you should not use a dictionary.

Functions

In the examples so far, we have written programs by defining only top-down procedures line by line. Unfortunately, when developing projects in real life, it is not possible to proceed in this way. We need some structures in order to have a more compact and scalable code base. Like functions and classes.

Image for post
Image for post
Click here to play with the example

The function definition is made with def statement. Our functions take parameters just like in math. We can also specify default values for these parameters. Our first function converts a string to capital letters. The second one adds “hello” to the beginning of the received value and has a default parameter value.

You’ve probably noticed how it works already in the fil visualization. Since we did not provide parameters when calling the Hello function, it printed the default value untitled artist.

Functional Programming

We use classes or functions to reduce the complexity that emerges when our program grows. I will not cover the classes in this article, because if we get into it, we will have to go into object-oriented programming as well. The ecosystem is progressing somewhat differently, people are heading into functional programming rather than object-oriented programming.

Functional programming is a paradigm based on the tendency to create the entire program from small self-running functions. I cannot compare object orientation and functional programming paradigms with each other here, but I am the one who is in the functional programming side. The reason for that, I would like to develop my applications like puzzles. I’d like to break them tiny pieces, then generate new things based on this tiny tiny pieces.

If you create your programs from small function blocks that do not reach any external state, it will be easier to manage it.

So how do I integrate these functions into my program?

Image for post
Image for post
Click here to play with the example

I tried to visualize what I am thinking about functional programming. Our code base (will be in different files of course) consists of functions that are unaware of each other. Our program only deals with data definitions and how the data will be shaped as an output.

We used two functions named map and filter. Run the example and try to differentiate how the results are changing by manipulating the functions. The map there applies the function you give on all the elements on an array and returns a new one. Filter, on the other hand, eliminates some of the list items according to the function you have given. For example, we passed a function that determines if a given name starts with the letter a.

Image for post
Image for post
Click here to run the example

If you have noticed, we can generate another collection from one collection with map and filter. If you would like to aggregate a single value from a collection, you need to compress that collection as a single value. This is called folding in functional programming languages. Its equivalent in Python language is reduce. In our example, we defined a reducer function in order to calculate the total items on the user’s basket of an imaginary e-commerce site.

These are the most basic functional programming concepts. It is possible to develop any kind of application you want with these three tools.

Thank you for reading!

This is it for now. I am thinking to write the second part of this article. Here’s the list of topics that I will be covering:

  • Modules
  • Virtual Environment
  • Web Frameworks
  • Flask Framework
  • Database Integration

This article was an introduction to the Python framework and it’s ecosystem. I will focus on more practical things such as web development in the second part of the article.

You can email me if you want me to cover a specific area.
fatiherikli@gmail.com.

All the snippets written are published under the Creative Commons Legal Code. If you’d like to support me, you can be one of my sponsor on github.

You can follow me on twitter too @fthrkl.

Stay safe!

Image for post
Image for post
Python programming language

Senior Software Developer and Resource Manager in EPAM Systems. Writes articles about Computational Intelligence. Current twitter account @fatihxy0

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store