Datatypes
Datatypes are the form of value which we can use in programming so that it can be used for various purposes depending on which type of data is required. For example, if you are making a program to input list of students you will use string and for storing thing there marks you will use integer. It is very important and good practice to put same type in a group, even though python allows you to put different datatypes in a group.
There are 4 main datatypes used in python:
1. int
2. float
3. str
4. bool
'int' is used to store Integer values such as 1, 2, -34, 452 etc. , 'float' datatype is used to store Decimal values such as 1.2, 4.2356, -0.2365, -4.0 etc., 'str' datatype is used to store String values such as "Python", "Hello World", "4" (here 4 is used as a string and not as an integer) etc., you can put you string part either in single quotes or double quotes. 'bool' is used to store Boolean values 'True' or 'False'.
type() Method
'type()' method is used to identify, which type of data is used. For example, type("Hello") will display 'str' type. Let me illustrate this with the help of IDE.
print(type("Hello World!"))
print(type(True))
print(type(4))
print(type(17.2))
This what we are going to check and let's see what the output comes.
Here notice that I have kept the type() method within print(), this is because, type() method only returns the value but to display it we use print() method in Python.
Variables
A variable is something which is used to store some kind of value, it can be single value, or a list of values.
Syntax for assigning values to a variable:
<variable name> = <value>
a = 4
b = "Hello"
c = 4.5
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
In this you can see that I have assigned some random values to few variables. But, one thing you must notice here that we are not specifying any datatype to any variable. Here, python is so intelligent that that it identifies which datatype is it. And also remember one that that we do not give semicolon(;) at the end of line in python. Python identifies the end of line if we press the ENTER key. So, to verify the datatype I have used the type() method. It should display, int for a, str for b, float for c and bool for d. Let's see what is the result, whether python is able to identify the correct datatype or not.
So here you can see that python identifies the datatype even though if we don't specify it.
Taking User Input
User input is the most important part of any program. What so ever program we make, we keep in mind that the user should be able to operate that program according to their input, here comes the use of taking user input.
input() Method
We use input() method to take data from user. Let me just explain with the help of an example. Suppose we have to make a program that adds two number and displays the result. So the idea would be that, we will take 2 numbers from user and perform addition to it. But, it's not the only thing, we need to take care of one more thing. Let me first illustrate this and explain the other part after that.
n1=input("Enter first number:")
n2=input("Enter second number:")
add= n1 + n2
print("The sum of n1 and n2 is",add)
Here you can notice that, I have written some text within the input method. This is the text which will be displayed while taking input for user, and this a good practice as the user should know, for what they are giving the data. Suppose we give the value of n1 as 5 and n2 as 4, then the result should be 9.
So, you can see here the result came out as 54. What happed to it? Is the logic of my program wrong? No, this is because, by default input() method takes string as input and hence the out put was 54, adding 5 and 4 as string. So what should we now do to handle this issue.
To handle this issue, we need to typecast this into integer. Typecasting is changing the datatype of any type by specifying it. So, now after typecasting n1 and n2 to integer, let's see whether we get the correct result or not.
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
add= n1 + n2
print("The sum of n1 and n2 is",add)
So now let's see whether the result is as expected or not.
1 Comments
Nice bhooot achha h
ReplyDelete