Variables are the temporary placeholder for storing the value which has a unique name. In Python, you don’t need to declare the type of variable, this is done internally according to the value assigned.
Python supports following dataTypes –
Simply declare the variable name and assign a value to it using assignment operation
Create variable in Python IDLE –
>>> a = 10 >>> b = 12.5 >>> name = "yogesh" >>> c = True
Here, I defined variables in which
a store integer value,
b store decimal value,
name store string value and
c store boolean value.
You can print the value on the screen using the print() function.
>>> print(x) 10 >>> print(b) 12.5 >>> print(name) yogesh >>> print(c) True
Storing user input
>>> num = input("Enter number : ") Enter number : 43
You see an error on the screen when you directly try to perform arithmetic operations on the value returned by the input() function.
>>> num + 2 Traceback (most recent call last): File "<pyshell#20>", line 1, in num + 2 TypeError: Can't convert 'int' object to str implicitly
This is because in Python the input() function always returns the string object. When you perform concatenation between strings and integer object then its returns an error.
To avoid this convert the string object to an integer type using an int() function.
>>> num = int(input("Enter number : ")) 43 >>> num + 2 45
Concatenate two string variable.
>>> text1 = "Hello" >>> text2 = "World" >>> text1 + " " + text2 'HelloWorld'