Variable and datatypes.
In this blog, we will learn variable and datatypes in python and how to use them. So, lets get started.
As we know
( ) ] on to the screen.
So we are able to do the same thing using a variable.
Using of variable:
In above code the we have used print() twice. Firstly, The value to be output is given directly in between the brackets Secondly, The value is assign to message and then given to print. One thing to observe is the both the methods give same output.
So the concept of variable in python is we create a small storage memory and store there the value and address of storage is name as variable i.e, 'message in above code is variable' .
To get clear idea, Lets compare variable with a book. The variable is like an index ( page no ) of the content and the value ( data ) of the variable is the actually content. Or you can say variable is empty box with its name where we can store value in it. Or Variable is alias name of the the data. Here we have given the data
'Hello world, My name is masi' a alias name as
message = 'Hello world'
'=' is use to assign value to the variable.
So to create variable you need to give name of variable followed by '=' sign and the value to assign to variable.
. variable_name = data .
Assign data from right side of '=' to left side.
Benefit of using variable is it can be used repeatedly.
message = 'Hello world' print(message) print(message) print(message)
C:\Users\Masi\AppData\Local\Programs\Python\Python38-32\python.exe "C:/Users/Masi/Desktop/python tutorials/heloworld.py" hello world hello world hello world Process finished with exit code 0
As you can observe, we have store the value in message and reused it.
variable data can be updated. Example:
message = "First value of variable" print(message) message = "Second value of a variable" #changing the value of variable. print(message) second_message = message #assign value to variable using variable. print(second_message)
First value of variable Second value of a variable Second value of a variable
The value of variable can be changable and is updated in above example. Also we can assign data to variable using another variable.
Try to play around with code. It will increase your understanding about the concepts. Changes you do can be seen on the console output.
Comments are very important in your programs. They are used to tell you what something does in English, and they also are used to disable parts of your program if you need to remove them temporarily. Here’s how you use comments in Python:
1 # A comment, this is so you can read your program later. 2 # Anything after the # is ignored by python. 3 4 print "I could have code like this." # and the comment after is ignored 5 6 # You can also use a comment to "disable" or comment out a piece of code: 7 # print "This won't run." 8 9 print "This will run."
I could have code like this. This will run.
As you can see any code after
# in line is not excuted.see the print code is not excuted which is having #.
We mainly use comments to give an explaination of code. Its is consider as a better coding standard.
message = "First value of variable" print(mesage)
C:\Users\Masi\AppData\Local\Programs\Python\Python38-32\python.exe "C:/Users/Masi/Desktop/python tutorials/heloworld.py" Traceback (most recent call last): File "C:/Users/Masi/Desktop/python tutorials/heloworld.py", line 2, in <module> print(mesage) NameError: name 'mesage' is not defined Process finished with exit code 1
See in above code the variable name is message but in print the value given is mesage which is not defined or used. So its throwing error. To fix it just change the mesage in print to message.
Also if you were using the ide ( pycharm ) it would have notified you. And also when ever you get this kind of error. Just read the message carefully to understand the reason of this kind of behaviour.
In programming, value of the variable is known as 'data'.
Naming a variable rules:
- Variable names can contain only letters, numbers, and underscores.They can start with a letter or an underscore, but not with a number.For instance, you can call a variable
- Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example,
first messagewill cause errors.
- Avoid using Python keywords and function names as variable names;that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word
- Variable names should be short but descriptive. For example,
nameis better than
student_nameis better than
name_lengthis better than
- Be careful when using the lowercase and the uppercase letter because they are consider as different in python. MESSAGE is diffent than message.
Variable are storage for the data and data can be of different types. We mainly describe type of
data store in the variable as datatype and they are mainly
Dont get panic. We will see all of them step by step in an organised manner. Also the syntax for creating different datatypes is same.
Data like integers, floats ,complex numbers, decimal numbers , fractions etc., comes under the Number datatype.
See the examples to understand
# assigning integer value to varaible value1 = 1006026 print(value1) # the integer value can also be assign to varaible # using int() built-in funtion. value2 = int(86658) print(value2)
For Float or decimal values:
# assign float value to variable value3 = 10.2568 print(value3) # the float value can also be assign to varaible # using float() built-in funtion. value4 = float(5.698) print(value4)
For Complex numbers
# assign complex number values to variable value6 = (5+7j) print(value6) # assign complex number value to variable using complex() built-in function. value5 = complex(4,3) print(value5)
In above examples
int(), float(), complex() are built in python functions. You can understand the functionality of them by examples.
Basic operations between variables:
1. Addition i.e, x + y
x = 5 y = 6 # adding the variables print(x+y)
2. subtracting i.e, x - y
x =12 y = 7 # subtracting the variables print(x-y)
3. multiply i.e, x * y
x =12 y = 7 # multiply the variables print(x * y)
4. divide i.e, x / y
x =12 y = 6 # divide the variables print(x / y)
5.reminder x % y
x = 121 y = 10 # reminder of division the variables print(x % y)
given operations performed for
int type can be performed for
Operations between the different types of variables is allowed
x = 1554 # int y = 25.689 # float # Dividing the interger value with float is allowed print(x / y)
you can add, subtract, Multiply and divide between float, int and complex numbers.
z = (4+3j) t = 25.689 # adding the complex value with float is print(z + t)
You can find the type of the number using the built in function
z = (4+3j) t = 25.689 x = 5896 # getting the type of value using type() print(type(z)) print(type(t)) print(type(x))
<class 'complex'> <class 'float'> <class 'int'>
Also you can change the data from integer to float or vice-versa or from integer to complex,etc., Using the
int(), float(), complex() built in functions.
x = 4586 print(x) print(type(x)) y = float(x) print(y) print(type(y)) z = 783216.84562486 print(z) print(type(z)) t = int(z) print(t) print(type(t))
4586 <class 'int'> 4586.0 <class 'float'> 783216.84562486 <class 'float'> 783216 <class 'int'>
So that it in this tutorial. In next tutorial we will learn about strings and how to use and manipulate them.