An example of logic programming,algorithm with Python
Well, because of the interest of many I decided to do this post. Its a short talk with you , and if you like, leave a comment and I will bring more stuff. So lets go
First exercise your mind and do logic sequences such as the detailed steps of opening a door , example:
You walk to the door
You grab the knob
You turn the knob
You pull the knob
Your door is open
You can try something like the snowing exercise too:
Done? Ok, lets move on. Imagine an example to use on our study: Calculate the final grade of students in the third grade. The students will perfom two tests: T1(0-10) and T2(0-10). The results will be numbers 0-10. Note all the numbers(T1,T2 and result) must be integers.
Ok, how do we resolve it?
We have to get the results of the two tests and get and the average by dividing the sum by 2, like that:
Lets talk about Algorithm, its a self-contained sequence of actions to be performed, so, to find the answer we need to find the sum and divide by two
Now lets find the algorithm of this question. Here we have 3 things: input data, process and output data. Lets think about what steps we need to do:
First of all we have to receive the result of the first test and the result of the second test,this will be our input. Then we have to sum all test results and divide by 2, this will be our process. For the last step we have to show ,it will be our output. Imagine this in a computer window, it will be like this:
Receive the test grade 1(input)
Receive the test grade 2(input)
Sum both test results and divide the result by 2(process)
Show the result of division(output)
Good, congratulations, we found the algorithm.
Programming is about logic and algorithm, you will do the same thing but in a computer language.
In a programming language we have variables. What you need to know is that variables are used to store information to be referenced and manipulated. Example:
We have the grade ,right? And we have to keep this grade somewhere, so we can store it in a variable.Lets call the variable "t1" for the first grade ,"t2" for the second and we call the result of the division "result":
Receive t1
Receive t2
Sum both test results and divide them by 2, store it in result variable
Show the result
Get it? Ok, every variable have to be declared first, and we do this by creating and naming them before we store something . In some languages we have to define what type is this variable(Example: int,float,string), we are working with integer numbers, so the type will be integer,(int) now we have to put this on programming language, and all programming languages have their way:
PYTHON
In python we declare an Integer(and any variable) by giving them a name and passing the value to this variable,you don’t need to specify if it is an integer, or string or anything .
Well, in this case, we have to ask the user what grade the student took in T1 and t2. It has to pass a message. To get the input and pass the message you will do this :
input("Test 1: ").
Input("Test 2:").
Everything we write between " " will be showed to the user and the type is string. This way, the user will know what to do.
Every string is a sequence of characters, and will always be between " ",example: a = "Hi", b ="how are", c = "you".
Ok, we already have the input, but it’s a string and what we need is an Integer, so an easy way to do it is writing the type you want before the input you want to convert,like this:
t1 = int(input("Test 1 :"))
t1 is our variable and int is the type , so we put all we want to convert inside: int( what we want to convert)
Alright, we have the first part of the code, but how we make an equation?Explaining in an easy and simple way, to do a sum you will use + , to do a subtraction - , to do a division / and a multiplication *, so:
2 + 2 = 4
2 - 2 = 0
2 / 2 = 1
2 * 3 = 6
Now we know how to make the code ,result = (t1+t2) / 2. When we put in an equation something inside ( ) we are giving them priority, so it will be resolved first(t1+t2) then / 2.
To the last step, we have to show to the user the result, and its really easy, we use print(). In this case we can write print("The result is" ,result).
What he do here is show in the window a phrase and pass the result, it will show for example: The result is 5. We can pass the result alone too, its your choice: print(result). Note that whenever we want to show a phrase we have to put it inside " " like I already said because it’s a string and separate the phrase and the result with the comma(,) because its two different things, the string is what you want to pass to make clearer to the user, the result is the variable that is storing your result number. If you want to only pass a string you can do this: print("Showing only this");
The code will be like this:
t1 = int(input("Test 1 :"))
t2 = int(input("Test 2 :"))
result = (t1+t2) / 2
print("The result is" ,result)
Java
Analyze the same exercise in java and try to understand
I did not go into too many details here, but if you want me to start teaching a language, let me know which one. The purpose of this post was simply to show how easy it can be to learn
That’s all for today, I hope you enjoy, I really put my heart on this
See ya
I like it. Have you experiences with NLP and especially nltk?
I saw something about it, but I did not get it to learn well. I will start to take a look to learn well because I know it is very useful
That was an awesome little break down, I have a best friend that's a programmer, and this stuff is always over my head!
i love python :) i think everyone should start with it.
Nice GIF man. And great info keep posting #minnowsupport
Nice little programming example in Python. Love it :)
Create a great day,
@kozan
upped , thank you
nice post.. keep it up