Categories
100DaysOfCode EMBEDDED PROGRAMMING PROGRAMMING PYTHON RASPBERRY PI TUTORIALS

Python Program To Add Two Numbers

In this article, we will look at how to write a Python program to add two numbers. This is a very simple Python program that is suitable for even a beginner in Python programming to work on it for getting hands on practice with Python.

In order to add two numbers in Python program, we need to first break down the problem into the following steps:

Breakdown of the problem of Adding two numbers

  1. Receive the first input number from the user and store it in a Python variable.
  2. Receive the second input number from the user and store it in another Python variable.
  3. Add the two numbers by adding the two Python variables using a Python statement (Learn about Python statement here)
  4. Store the final result in another Python variable called “result”
  5. Print the value of the “result” variable.

In the above breakdown of the problem, you will notice that the Python program we will be written in such a way that the two numbers that need to be added will not be hard coded directly into the program itself but instead is written in a generic way such that we prompt the user to enter these two input values every time the Python program to add two numbers is run. This type of programming approach is often called general programming as the program is generic enough to receive any two different values each time it is run.

Pseudo-code To Add Two Numbers Using Python Programming Language

num1 = Receive First Input Number From The User
num1 = Receive Second Input Number From The User
result = (num1) Added to (num2)
Print the (result) on the screen

We can see from the above psuedo code that this is a simple program that receives two numbers from the user, adds them and print their results back on the screen. The above pseudo code gives us a nice little framework on how to write our program. The same pseudo-code can now be used to write a program to add two numbers using any programming language and not just Python!

Now that we have our problem broken down and pseudo code written, it is time for us to replace the pseudo code with actual Python programming code instructions.

Python Program To Add Two Numbers And Print Its Result

Fire up your Python IDLE interpreter and start typing in the code along with me for you to be able to understand this program better:

Python 3.5.2 (default, Oct  8 2019, 13:06:37) 
 [GCC 5.4.0 20160609] on linux
 Type "copyright", "credits" or "license()" for more information.
>>>

Once in our python interpreter, let us start typing in our Python program commands. The first thing we need to do according to the pseudo code written above is the receive the first input number from our user. In Python program, the instruction code to be used to receive a value from its user is by calling the input() function. Input function will accept a string as its parameter that will be displayed to the program user when the program is run. So, with this knowledge, our first line of the Python program to add two numbers will be:

>>> num1 = input('Enter the first number\n')
Enter the first number
10
>>> 

As you can see from the above code block, we have used the input() function to prompt our program user with the string “Enter the first number“. We are also saving the value entered by the user to a Python variable called num1.

The input function will then prompt with the above string and wait until the user enters a number. In the above code snippet, I had entered a value of 10 which is now stored in the variable num1.

In a similar way, we will write the next line of code which will prompt the user to enter a second input number that is to be added to the first number. This is achieved with the following piece of code:

>>> num2 = input('Enter the second number\n')
Enter the second number
20
>>> 

Again over here, I have entered my second number as 20 when prompted.

Now that we have the two numbers in our Python variables num1 and num2, it is time add them to get the final result that we are going to store in our third Python variable called result. This is achieved using the following Python statement:

>>> result = int(num1) + int(num2)
>>>

So from the above python code, we have now added the numbers num1 and num2 using the Python arithmetic operator “+” and the typecast operator called “int()“. We had to use the typecast operator int because by default all inputs from the user into a python program will be interpreted and stored as a string. We can check this by issuing the following command in the interpreter:

>>> print(num1)
10
>>> type(num1)
<class 'str'>
>>> 

So, by calling the typecast operator int, we are converting this string value to an integer value.

>>> type(int(num1))
<class 'int'>
>>> 

finally stored the end result in a new variable called result. Since we have not issued a Python command to print the value of result, nothing gets printed on the IDLE prompt yet. So, the final step is exactly that. To print the value of the result variable onto the screen. This is achieved using another Python function called the print function.

>>> print (result)
30
>>>

Here is the full program that we can store in a file called add2num.py and run it using the command python3 add2num.py everytime we want to add any two numbers!

num1 = input('Enter the first number\n')
num2 = input('Enter the second number\n')
result = int(num1) + int(num2)
print (result)

This concludes our Python program to add two numbers.

Additional Side Notes On Python Programming Language:

Python is an interpreted programming language that gets interpreted and executed on the fly and hence the program written in Python do not need to be compiled like in the case of a C or Java programming language.

Categories
100DaysOfCode PROGRAMMING TUTORIALS

How To Write Code Like A Professional?

Everyday, we programmers write hundreds of lines of programming code. But, have you ever stopped to notice if your program is good enough? How do you make sure your programming skill set is up to the mark? How to make sure you are Programming Like A Pro? In this article, we consider these questions and try to answer them each in greater details.

How To Code Like A Pro?

There are many characteristics of a program written by an experienced professional that sets his code apart from a code newbie. A professional programmer would make use of his past learning experience and implement his code that follows a certain set of guidelines and best practices he has learnt over the time since he began programming.

While each of these set of guidelines and best practices may differ from one Professional Programmer to another Pro, there are many common guidelines which more or less all Professional Programmers follow that can make their code set apart from the rest. Here is a list of some of such common guidelines used by almost all Professional Programmers. If you can practice and incorporate these practices and techniques into your daily programming routine, even your program code will start to look more professional.

Best Practices & Guidelines To Write Code Like A Professional

  • Write code that makes use of the processor efficiently – In other words, write code that runs fast on the processor using lower number of clock cycles. How to achieve this? While there are many things involved in learning to write a highly CPU efficient programs which will be explained in the future articles, some of the most common practices is to use loops sparingly, use efficient looping systems, write programs using instructions that require less clock cycles to accomplish the task at hand etc. You need to have good understanding of the underlying computer system architecture to get a better hold or control on these aspects, so learn more about the computer architecture.
  • Write code that makes use of memory efficiently – Use the memory of a computer system efficiently and sparingly. It usually takes certain amount of computer clock cycles to read and write data to or from the memory. So if you want your code to speed up, it is better to write code using instructions that uses lesser amount of memory. If your code allocates too many variables, it can start gobbling up a lot of RAM space in your user’s computer, there by slowing down the computer all together, thus giving a had user experience.
  • Write code that is more readable – This is one of the most important aspect of a professional code. You can write code in a convoluted way to do something extremely smart on its data, but if in the future you move on in your job role and your code is handed over to your junior developers, they will find it extremely hard to read and understand your code. So always write code that is easily readable by anyone familiar with programming. This can also include adding more comments in your source code, naming the variables appropriately etc. Sometimes, writing readable code could result in writing a slightly inefficient piece of code thus contradicting the above 2 points discussed earlier. But however, in such a situation, it is still advisable to give priority towards readable code than saving a few CPU clock cycles.
  • Write code that is highly modular – What does this mean? It means you need to break down your programming into individual modules such as functions and classes, that does one thing and only one thing but extremely well. Your code need to be split up into several functions, files and libraries that can be made use of in the future for other projects as well.
  • Write code that is well tested and documented – Each of your code modules need to be very well tested to ensure it fulfills and works under all boundary conditions. It should not break down for some unexpected values but gracefully reject the input under such situations. This is achieved by writing robust test cases that can test for all frequently possible conditions even if it occur only one in a million time (boundary conditions). Make sure that the code is well documented using comments and appropriate variable or function names as discussed earlier.
  • Write code that is easy to enhance – Before you sit down to write your code, you need to design to program to be highly modular and easy to enhance of its functionalities in the future. This would mean writing code that is modular, with will defined APIs and interfaces such that a function serving that API can be swapped for another function with a different implementation for the same set of input and output values.

There are still many more aspects of a professional’s code that usually depends on his style and taste of coding, but the above list should give you a good fundamental beginning on how to think and write code like a professional programmer.

In the future articles, we will continue to explore some of the fundamental aspects of computer science such as a computer’s architecture, its basic functionality etc that will help us getting a solid understanding the basics of computer science. Do let me know if you have any comments on this or any other topic you would want me to explore and I will be happy to accommodate your requests.

So until then, happy coding! 🙂

Categories
100DaysOfCode BROWSER EXTENSION TUTORIALS WEB BROWSER

Use Your Web Browser As A Notepad

Did you know that you can turn your web browser into a notepad like text editor for quickly jotting down some notes while browsing the web?

Yes, it is possible to turn a new browser tab of your favorite browser such as Google Chrome, Firefox or Opera into a text editor in an instant. You do not need to have any programming experience to make this work. You will just need to remember the below little piece of string that you need to type into your favorite web browser to turn it quickly into a notepad.

Below are the steps you need to follow to turn your favorite web browser into a notepad like quick text editor:

  1. Open up a new tab in your web browser
  2. Type in the following piece of code into the URL bar (or the address bar) of your browser tab:
data:text/html, <html contenteditable>

Hit enter and voila! Your browser tab has turned into a notepad! You can now start typing anything you want in the browser tab window and verify that the browser has indeed turned into a note pad!

How does a web browser turn into a text editor?

By typing the above mentioned code into the URL of your web browser we were able to turn the browser tab/window into a text editor. But how did this work?

It works because the above mentioned code instructed the web browser that the current document’s content is of raw text data type – done through the use of keywords data:text/html

On the other hand, by typing in the markup code:

<html contenteditable>

We are instructing the web browser such as Google Chrome or Firefox or Opera to turn this web document into an editable HTML document. This results in browser allowing us to type in any kind of text we want into this browser window from now on!

Thus, we found that by simply typing a simple piece of code:

data:text/html, <html contenteditable>

into the address bar (URL bar) of our web browser, we were successfully able to turn the browser tab into a text editor.

However, it is a little bit difficult to remember this piece of code as it looks a bit cryptic. So to avoid having to remembering this, we will look at how we can create a new Google Chrome extension that can be used to open a browser tab as a notepad every time it is clicked. But that is a topic for my next article.

This article explained how and why typing in some magical code into the address bar of your web browser would convert it into a text editor. Hope it was informational and you learnt something new from it. There are many such simple tricks we can use to convert our web browser into a high performance tool by customizing it to our needs. We will decode each of these tricks as we go further, one step at a time.

Until next time, happy coding! 🙂