Python Tutorial 2 : Data Type, Arithmetic Operation, Type Conversion

Posted on Posted in Software

In the previous tutorial, we discussed what is Python and how can you install python on your computer. In this 2nd tutorial, we will talk about the basic concepts of data type, some simple arithmetic operations, and type conversion. Let’s get started.

Please do have a look at our video. 😀

[embedyt] http://www.youtube.com/watch?v=DBuhpg-tcP4[/embedyt]

Once you open python idle, you can write and execute a single instruction and display the necessary output. This is known as REPL – read-eval-print-loop.  Let’s write the famous program, hello world. On pressing enter, you will get the output.

However, it is not a good practice when it comes to the bigger problem. So you need to write codes in a script and for that press ctrl+N and start writing.

There are some inbuilt functions and if you want to check them, write dir(__builtins__), hit enter and there you go. then if you want to know what is the function of a particular function, write for example help(pow) where help is also an inbuilt function and then you will get an explanation of it.

If you want to check data type, write type(10.0) and then it will return float data type, or x=12 where x is a variable and then type(x) will return int.

Now let’s perform some basic operations.

2+3 gives 5, 5-9 gives -4, 2*(3+5) gives 16. Since parentheses will have higher priority, so the addition is taking place first which is inside the parentheses. Now if we perform a division, then 10/2 will give us int value 5 in Python 2.7 version and 5.0 in version 3.x. Of course, if you try to do 87/0, then you will get ZeroDivisionError. You will also get a float value if you multiply/add/sub an integer with the float value.

In-place operator allows you to write code like x=x+3 more concisely as x+=3.  A point to be noted, many other languages have special operators such as ++ as a shortcut for x+=1. Python does not have these.

Next is the exponentiation where 3**2 gives 9, 16**(0.5) gives 4. You can also get this thing using inbuilt function pow by writing pow(3,2) that is 3 square which gives 9.

Although pow is available by default, not all arithmetic functions are available. So in order to use it, you need to import the math library as from math import *. The advantage of writing this instead import math is that you don’t need to use dot operator. For example, using import math, you need to write math.log whereas in from math import *, you just need to write log. There are lots of libraries available and we will cover those as we go through the course. So for using functions like log(), sin(), sqrt(), etc, you need to import. Lastly, if you want to get the quotient, write 20//6 and for the remainder, write like 1.65%0.5.

So for using functions like log(), sin(), sqrt(), etc, you need to import. Lastly, if you want to get the quotient, write 20//6 and for the remainder, write like 1.65%0.5.

Before ending, I must tell you that there is a concept of type conversion. For example, ‘2’+’3’ will give ‘23’ and not numeric 5 because they are in a string. To get 5, perform type conversion by writing int(‘2‘)+int(‘3’) and the answer is 5. Similarly, you can convert int to float.

So that’s it for tutorial 2 and I hope you enjoyed it. Don’t forget to subscribe to our channel if you are new and hit the bell icon so that you don’t miss a single video in the future. Follow us on social media and I will see you in the next one. Peace 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *