Introduction
Python has gained a lot of spotlights because of its salient features, easy syntax, good readability, availability of various libraries and APIs.
Now every computer geek wants to learn this trending language, but there may be a question “how to start?”. Well, the answer to that question would be having a look at python documentation and knowing the syntax of python.
After learning all the rules and syntax of python our mind may urge us to implement a trivial or hello world program in python. Now let’s satisfy the thirst of our enthusiastic mind by having a look at a trivial code, which is adding two numbers in python.
Top Data Science Skills to Learn to upskill
SL. No | Top Data Science Skills to Learn | |
1 | Data Analysis Online Courses | Inferential Statistics Online Courses |
2 | Hypothesis Testing Online Courses | Logistic Regression Online Courses |
3 | Linear Regression Courses | Linear Algebra for Analysis Online Courses |
Code Walkthrough
Let’s have a look at different possible implementations of this code.
1. Addition of Two Numbers
Let’s demonstrate the code of adding two user-defined numbers.
i = 5 j = 6 sum = i+j print(“sum of”,i,“and”,j,“is”,sum) #line1 print(“sum of”,i,“and”,j,“is”,i+j) #line2 |
One of the unique features in python is we don’t need to specify the datatype of the variable and we can directly allocate the variable with a value, and python does the hard work for you.
In the above code variables i and j stores the values which are to be added and the sum variable stores the value of i+j. And then we can print that value. Also here’s a point to be noted, if we don’t want a variable to store the answer we can directly calculate the answer in the print statement itself (as shown in line2).
But what if we want to add numbers that are given as input from the user?
2. Addition of User Input Numbers
This code will be similar to the previous code but the only difference would be assigning the variables with user input. Let’s have a look at the code.
i = input(” enter the first number “) j = input(” enter the second number “) sum = i+j print(“sum of”,i,”and”,j,”is”,sum) print(“sum of”,i,”and”,j,”is”,i+j) |
In the above code, the first line would prompt “enter the first number” in the output screen and the terminal or the console expects an input that will be assigned to the variable i. similarly, the same process will be observed for line 2. Now both the variables i, j are assigned with the user input, and the sum is stored in the sum variable. Feels like a cakewalk right?
Explore our Popular Data Science Online Courses
3. Addition of Two Float Numbers
Many times we may face a situation to add numbers with decimal precision, lets have a look at that implementation!
i = 1.5223 j = 1.8365 sum = i+j print(“sum of”,i,”and”,j,”is”,sum) #line1 print(“sum of”,i,”and”,j,”is”,i+j) #line2 |
As already discussed we don’t need to mention the data type of the variable we are going to use. Variables i, j are treated as float values and the final result obtained will have decimal precision.
We can also typecast an integer to a float in python, let’s demonstrate the difference between integer addition and float addition.
i = 3 j = 6 sum = float(i)+float(j) #line1 print(“sum of”,i,”and”,j,”is”,sum) #line2 print(“sum of”,i,”and”,j,”is”,i+j) #line3print(float(i+j)) #line4 |
In the above snippet, we have two variables i, j which are assigned with two integers. Now if we add these two variables the answer would be of integer type. We can have the answer in float datatype by typecasting the variables into a float (as shown in line 1) or typecasting the answer into the float (as shown in line 4).
The main difference between line 2 and line 3 would be the lack of decimal precision in line 3. The output of line 2 will be 8.0 whereas the output of line 3 will be 8, so if we want to maintain the decimal precision of a variable then we need to use a float datatype.
Also Read: Python Project Ideas & Topics For Beginners
Read our popular Data Science Articles
upGrad’s Exclusive Data Science Webinar for you –
How upGrad helps for your Data Science Career?
Conclusion
We have walked through various code snippets where we’ve seen the addition of two user-defined numbers, the addition of user input numbers, the addition of float numbers, observed decimal precision of result with float datatype, and typecasting the numbers in python.
Now that you are aware of how to add two numbers in python, try writing the code on your own and try modifying the code with various data types. And try performing a few other trivial tasks in python and explore the fun in python programming 🙂
Learn data science courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.