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.
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.
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.
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.
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.
In this example, we use another decision making statement, the for loop.
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.
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.
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.
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.
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?
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.
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.