Monday, 14 August 2017

Monday fun with Python : Collatz sequence

Hello everyone. Today is Monday..well not a big deal and I know everyone knows today is a Monday. Like you I'm not a big fan of Monday, although nowadays I'm waiting eagerly for this day of the week as I get to watch a Game of Thrones episode on this day and I'm sure most of you can reciprocate the same feeling. The only thing I hate the most is the long wait till Monday night till I return to home from office and get to watch that episode for which I wait for one whole week and here I am in office waiting for the day to get over soon.

The plan

To kill the time I do one of the thing that I love the most. Yes, you guessed right? I code. So, I decided to fool around some python programming code to kill the boredom. Without much ado let's code :) I decided to device a python program that would help me to devise a "collatz sequence" In case you are thinking what a "collatz sequence" is then dive into this link here.



The code

The below function is collatz() has one parameter named number. If number is even, then collatz() should print number // 2 . If number is odd, then collatz() should print and return 3 * number + 1.

The below program let's user provide an integer and the program then keeps calling the function collatz() till the function returns 1. It's a simple program but trust me it's very interesting, the reason why it's interesting can be found in the link . Go ahead and have some fun and thank me later.










Subscribe to my youtube channel to watch the coding in this tutorial in action.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.


Friday, 4 August 2017

Where are the new Python Blogs?

Hello friends I welcome you back after a long long time. It's been nearly 3 weeks since I have posted any blog post here or any new tutorial videos on my youtube channel. Recently I have been involved in many of my pet projects. One of them being studying machine learning, I am a geek at heart and I love to learn new tech things and machine learning is one that ha got me hooked and this is the reason I am spending most of my time studying ML. Meanwhile I am also trying to learn and android development and trying to build an app on my own which is again a totally new field for me and it's taking up a very large chunk of my time not to mention my full time job.

Then there's my youtube channel for which I am trying to develop the best content for you guys and doing this involves a long planning, building the content, editing the videos etc. and this take a lot of time as I am able to devote 30 mins daily at the best to my youtube content creation and that explains the very long gap at which I post blogs or videos.

Oh, there is another thing . I have kind of outgrown blogger in terms of creative freedom so I am also trying to put together a website for you all (like minded geeks like me). This again takes lots of learning as I am not from a coding background and whatever time I am left with after devoting to the above mentioned projects/full time job etc. I use to learn the skills of the trade and put together my website. So in short I want to say that do stay along with me for a while as I haven't disappeared completely, I will be back soon enough.

Wednesday, 5 July 2017

How to become a Python Jedi - Beginners Python Programming Tutorial - Part 6 - While Loop



Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn about the while statement.

The While Loop statement?
In Python Programming, There are 3 types of loops →

  • While loop
  • For loop
  • Nested loops

Now to get into some action→

Let’s consider that two groups of aliens have landed on earth  out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys.










Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed by the print method.

We can also use negative conditions for entering the while loop. Copy and paste the below code in your editor and run it→












As you can see the condition aliens is not "Autobots" resolves to False when the user inputs a wrong condition & the control enters the loop and print the statement when a false condition is returned.

Now let’s make our system a bit more robust by adding some extra checks into it. We shall insert an if statement inside our while loop. In the version below if user gives the first input as Decepticons then the control enters the loop & prompts user for second input. Again if the input is Megatron then the control enters the if condition to print the last statement.




So, folks this was all for this week but we shall return with another chapter of this tutorial with some new concepts to learn.

Subscribe to my youtube channel to watch the coding in this tutorial in action.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Sunday, 28 May 2017

Beginners Python Programming Tutorial - How to become a Python Jedi - Part 5 : If-Else statement


Welcome back friends to another post on TheCodingProject and we are back with the fourth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn about decision making in python using the If-Else statement.

The If-Else statement?
In Python Programming, decision making is done by using a conditional statement like the If-Else statement. An If statement consists of 3 parts →
  • If keyword
  • a conditional operator
  • the values which needs comparison

Now it should be kept in mind that the only values that an If condition returns are a True or a False , Using this True or a False we can decide if the condition that we are trying check is right or wrong.
When Coding knowledge came to my rescue→
Let me tell you a story about how coding knowledge came to rescue . I have this neighbour named Alex who doesn't quite seem to be a regular guy. Wait ! actually he never seemed to me as a regular human 😟. This one day I was visiting him at his home for some work when I noticed something unusual. His living room is decorated with his family photographs, he has the photographs of his entire family tree over there starting from his great grandfather down to him. The weird thing that I noticed was that all the men in these photographs looked similar to him as if he himself was present throughout his family history. Is Alex a Vampire ? 😓 With this frightening thought in mind I ran back to my house and jumped straight to the computer and started to code. Using my superhuman brain I made a python program which would hack into his personal computer and collect all his personal information and then the program would calculate his age & use that knowledge to decide if Alex was really a Vampire. Now, that program is highly confidential so I won’t show you my entire code but as you guys are so much interested I will show you the snippet that is really pivotal in deciding Alex’s truth →





Copy & paste the above code in your editor and run this code. In the above code we have an If condition & an else condition. Let’s assume we have the age as 100. Now when the above code runs the code touches the If condition & checks if age is greater than or equal to 0 or age is less than or equal to 99. Notice that we have used an or operator so the condition becomes True even if only one of the condition matches & as age is 100 ( greater than 0) so the If condition returns a True & the control enters the If statement to print the statement inside the print method.

Let’s modify our If-Else statement to make it more robust. Copy and paste the below code in your editor and give it a spin→







As you can see this time we have an and operator instead of an or. So this time our If condition will return True  only when age is greater than or equal to 0 and less than or equal to 99. Now as we have the value of age out of the bounds of 0 to 99 so the control skips the If condition & jumps to the line of code immediately after that which is the Elif  statement. This kind of expression is known as a  Nested if-else statement, wherein we have another If condition immediately after the first If condition to put a second conditional check and in python programming language it’s written as Elif. In our case the control checks that age is not satisfying even the condition inside the nested If statement so it skips that too and jumps to the next condition i.e the Else  statement and as there are no further condition defined for this else statement, so it returns a True & the statement inside the print method is printed.


Conclusion→
You can define n number of rules using the nested if-else conditional statements. Go ahead & try to extend on the above example code by implementing your own rules with nested if-else statements. The code examples can be found in the Python Decision Making.py file in my Github repo.

So, folks this was all for this week but we shall return with another chapter of this tutorial with some new concepts to learn.

Subscribe to my youtube channel to watch the coding in this tutorial in action.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Sunday, 21 May 2017

Beginner Python Programming Tutorial - How to become a Python Jedi - Part 4 - Comparison Operators



Welcome back friends to another post on TheCodingProject and we are back with the fourth chapter of the Python programming tutorial - How to become a python Jedi. This time we are going to learn about comparison operators.

What are Comparison Operators ?
Comparison operators are used to compare any two values, be it two numbers , two strings or any other objects. In short these operators help us to make a decision by giving back the results of a comparison. Comparison operators always return either a True or a False.

Different Comparison opearators →
== Equal to
!=  Not equal to
<   Less than
>   greater than
<= Less than or equal to
>= Greater than or equal to

Examples of Comparison operators →
Let’s get into some examples to better understand the usage of comparison operators. Copy the below code and paste it in pycharm or any other editor that you have. Additionally you can also download the comparison operators.py code file of this tutorial from github. →










For the first print statement the Equals to operator denotes that we want to compare that whether the integers 1 & 2 are equal to each other or not. When the control finds that both the integer values are not equal then it returns a False. Similarly for the second print statement when control finds that the two strings are not equal to each other so it returns a False once again. But in the third print statement we are comparing to see if 45 is greater than 34 and since it’s true, so a value True is returned. Similarly in the last print statement since 56 is equal to 56, so the condition less than or equal to evaluates to True.  
So, folks this was all for this week but we shall return with another chapter of this tutorial with some new concepts to learn.

Subscribe to my youtube channel to watch the coding in this tutorial in action.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.


Monday, 1 May 2017

How to become a Python Jedi - A Python programming tutorial - part 3(Boolean operators)




Welcome back friends to another episode of TheCodingProject and we are back with the third chapter of the Python programming tutorial - How to become a python Jedi. This time we are going to get a taste of boolean operators.



What are Boolean Operators ?
Computers don’t understand the common human language, they only speak and understand machine level language and this language is made up of only two numbers 0 & 1. These numbers are known as binary numbers & this language is known as binary language. Now if you are a crazy nerd like me and you wanna know more then dive in here. The numbers 0 & 1 also represent decision making as they also represent True (for 0) & False (for 1) and these two operators are known as boolean operators.


Enough talk man, I’m here to learn code!!😠
Okay Okay I just took a trip in my emotion boat. I know you want to learn code so let’s just cut the talk talk & dive to code code. Yayyy!!
Boolean operators in action
Copy the below code and paste it in pycharm and any other editor that you have code →



What the above code gives us ? We remember from above section about boolean operators that they represent True (for 0) & False (for 1) and in the above example for the first print statement when we try to compare OS1 with OS2 the python interpreter checks that IOS(OS1) is not equal to Android(OS2) so the interpreter returns a False. In the second print statement we are checking if ‘IOS’ is not equal to ‘Android’ which resolves to true and that’s why the second print statement returns True.
We shall now look into some more examples. Copy and paste the below code in your editor and run the code →




Let’s say you and me are to arrange a party tonight and we list down what we need for the party but we have a difference of opinion and to come to a conclusion we turn to the geeky method of decision making and that’s through python. We list down our choices in the following variables → my choices are listed in drinks and food, your choices are listed in for_party. Now, we check that is your choice similar to my choice? The first print statement gives us a False because the values mojito is not equal to the either drinks or food. You have noticed that we have used the boolean operator or in the first statement which tells the interpreter to check if the value of variable drinks or value of variable food is equal to the value of variable for_party. Run the second print statement as well and analyse why we get a True over there. Here’s a hint for you → Notice the is not statement.


To summarize what we have learned through the above examples just go through the below table, it is a kind of cheat sheet during the usage of boolean operators →
Operators
Results
True and True
True
True and False
False
False and True
False
True or True
True
True or False
True
False or True
False


So, folks this was all for this week but we shall return with another part of this tutorial with some new concepts to learn.

Get the code files for this tutorial in Github.


Subscribe to my YouTube channel to watch new python programming tips and tutorials.


If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.



Saturday, 15 April 2017

How to become a Python Jedi - A Python programming Tutorial Part 2 - Instruction flow control




We all know that programming is a set of instructions, but many a times we forget to think that this set of instructions should make sense to be called a computer program. You can get your dog to fetch a stick just by yelling “FETCH” but if type an instruction like “calculate” it will do nothing other than just sitting there and gaping at your face with that ugly python command prompt look.


To command a machine to do a task we need to pass a set of “precise and clear set of instructions that flows seamlessly from one step to another”. This flow of instruction is known as flow control or instruction flow control in the programming world. One handy tool to do this is a flowchart.



What the hell is a flowchart?
Well simply put a flowchart is a graphical representation of instructions/logic sets that helps you to visualize the logic that you have formulated for a task.
Let’s get into an example….


Let’s say that you want to automate the login to your facebook account and want to code the entire process in python, but before you get cosy with your keyboard you need to think and devise a series of steps as below-


  • Type the facebook url
  • Search for the user id & password text box
  • Enter your credentials
  • Press the Enter button


It’s time to visualize the steps via a flowchart like the one below -



The above flowchart is a visual representation of the steps that you want your code to follow. The boxes represent the different units/steps of the logic that was devised earlier and the arrows represents the “flow” of the program control between different steps of the logic. In actual practice your program should flow through the steps in the same manner as is represented in the above flowchart.


Endnotes
Devising steps/algorithm for a computer program is not as straightforward as is represented in the above example but I to get a taste of what a flowchart is and how to design a basic flowchart the above example will suffice.
Below are some practice exercises that will help you get going till the next tutorial which will be coming your way soon.


Quiz
  1. You want to prepare a large jug of lemonade. Write down the steps by step procedure for the same and devise a flowchart that represents the steps.


  1. Devise a flowchart to visualize the steps to login to Gmail.


  1. Create a flowchart to represent a up of coffee. Also include the necessary steps to check if the gas stove is on or not and whether water has boiled already etc.


So, folks this was all for this week but we shall return with another part of this tutorial with some new concepts to learn.


Subscribe to my youtube channel to watch new python programming tips and tutorials.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.