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
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
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
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?
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.
What are the pros and cons of programming in Python?
Python is an amazing language that has deep roots in development and is now gaining support in the programming domain. But along with its pros, there are some cons as well that must be considered before opting it out. The pros and cons are as follows:
1. The Python syntax is extremely user-friendly and easy to write. It is nearly equivalent to writing some sentences in the English language with indentation.
2. Problems that are solved in too many lines in other languages can be solved in less than half-line in Python with the help of various inbuilt functions.
3. The code is much cleaner and shorter as the programmer does not have to write much and most of the work is done by the compiler itself.
1. One of the biggest drawbacks of Python is its speed. It is very slow, slower than C++ and even Java.
2. Many programmers recommend avoiding Python because it spoils the logic building as most of the work is done by the inbuilt Python functions.
What do you understand about the numeric data type in Python?
In Python, the numerical data or values are generally represented by the numeric data types. There can be three types of numeric values in Python which are as follows:
The integers like 3, 0, -100, -2, and 25 are represented by the int class. Unlike other programming languages like C++, there is no limit to how long an integer value can be. You can enter a value equivalent to the long long int of C++ in the int class in Python.
The fractional or decimal numerical values are known as float and are represented by the float class. The scientific notation of a float value can be determined by appending “e” or “E” followed by the “+” or “-”.
The values that are in the form of “a+ib” where “a” is the real part and “b” is the imaginary part are considered as complex numbers and are represented by the complex class of Python.
Name some best Python IDEs?
There are a lot of Python IDEs out there that are pretty decent. Some of these are PyCharm by JetBrains, Visual Studio Code by Microsoft, Spyder, Eclipse, Atom, PyDev, and Wing IDE.