Python Variables and Types
January 8th 2020 289
Variables and Types
What are Variables?
- Variables are used for storing data values.
- A variable is created when we assign a value to it.
- Python is completely object oriented, and not "statically typed".
- You do not need to declare variables before using them, or declare their type.
- The equal sign (=) is used to assign values to variables.
- Every variable in Python is an object.
What is Dynamic Typing?
Dynamic Typing is a technique in some languages where depending on how a value is used, the data type of the variable is dynamically and automatically assigned. Consider the below code in Python,
n=65 #integer type char="A" #string type
What is Static Typing?
Static Typing is used in some languages where the data type has to be declared before it is used. Consider the below code in C,
int num=65; //integer data type char str = "TeachMeBro"; //string data type
Rules For Declaring Variables
Your variables should be descriptive( age, num, etc) or you can also give short names like (x, y, m, n, etc). Giving descriptive names to your variable makes your code more readable to you and to others who are reading your code.
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Basic types of variables are:
- bool (Boolean)
- int (Integer/Long)
Python supports three types of numbers - integers, floating-point and complex numbers.
# Examples """ Number types Variables """ a = 4 #integer b = 1.5 #float c = 1-1j #complex
Getting the Data Type
To check datatype of any variable use python in-built function type() . type() is very useful when we don't known what is the data type of the variable.
a = 4 #integer b = 1.5 #float c = 1-1j #complex print("Type of a",type(a)) print("Type of b",type(b)) print("Type of c",type(c))
#output Type of a <class 'int'> Type of b <class 'float'> Type of c <class 'complex'>
Boolean variable stores either True or False value.
d = True print("Type of d",type(d))
#Output Type of d <class 'bool'>
Strings are defined either with a single quote or a double quotes.
#string example #declaring string with single quotes mystring1 = 'hello' #declaring string with double quotes mystring2 = "hello" #check the type print("Type of mystring1",type(mystring1)) print("Type of mystring2",type(mystring2))
#Output Type of mystring1 <class 'str'> Type of mystring2 <class 'str'>
#Multiple Assignment a = b = c = 1 print(a) a, b, c, d = 5, 2.5, True, 1+2j print(a) print(d) #You can also delete the reference to a number object by using the del statement. The syntax of the del statement is − var = 1 var_a = 2.5 var_b = True # del var1[,var2[,var3[....,varN]]] # You can delete a single object or multiple objects by using the del statement. del var del var_a, var_b # if you print var then you will get error # print(var)
#Output 1 5 (1+2j)
Delete the reference of the object
#You can also delete the reference to a number object by using the del statement. The syntax of the del statement is − var = 1 var_a = 2.5 var_b = True # del var1[,var2[,var3[....,varN]]] # You can delete a single object or multiple objects by using the del statement. del var del var_a, var_b # if you print var then you will get error # print(var)
How to Concatenate integer and string?
If you will try to concatenate an integer with a string you will get an error.
# ERROR: variables of different types cannot be combined t = 5 + "string" # solution # first comment line 3 #this is called type casting => int to str t= str(5)+"string" print(t)
#Output Traceback (most recent call last): File "/tmp/sessions/9bde3bbd9dfd1963/main.py", line 3, in <module> t = 5 + "string" TypeError: unsupported operand type(s) for +: 'int' and 'str'