In your mind, there are many questions, what are variables and how do we typecast the variables in Python? So, don’t worry you came at right. In this blog, I will explain variables and how we typecast the values in Python. So let’s get started.
Introduction to Variables in Python
- Variables in Python are nothing but reserved memory locations for the values.
- It means that when you create a variable in the program it reserved some space in the memory.
- When you are writing the program at that time it does not create a memory for the variable, it reserved some memory space when you run the script file or program.
As soon as, you assigned some value a variable is created.
# Variables
company ="Bin Fin Tech"
Variables in Python can store any type of value i.e; Datatype. In Python, it is not compulsory to declare some datatype to variables. We can change any type of variable anytime in the program.
There are some set rules to create a variable in Python which are known as Identifiers in Python. To check the type of the variable, we can use the type() method.
# Type of the variables in Python
company ="BinFin Tech"
print(type(company))
#Output:
<class 'str'>
Here <class ‘str’> means that the type of the company is String Datatype.
Rules for creating variables
- The variable’s name starts with the letter or an underscore(_).
- Variable names cannot start with numbers.
- Variable names can only contain alphanumeric characters (A-Z, a-z, or 0-9) or underscore(_).
- Variable names are case-sensitive. For example, name and Name are two different variables.
- Variable names cannot use keyword words i.e, reserved keywords.
A) Re-declare the variable in Python
# declaring the var
num = 320
print("Before declare: ", num)
# re-declare the var
num = 420.3
print("After re-declare:", num)
# Output
Before declare: 320
After re-declare: 420.3
B) Multiple variables with the same values
num1,num2,num3 = 40
print (num1)
print (num2)
print (num3)
# Output:
40
40
40
C) Assigning different values to multiple variables in a single line
num1, num2, company = 10, 20, "BinFin Tech"
print(num1)
print(num2)
print(company)
#Output:
10
20
BinFin Tech
Types of Variables based on Scope
A) Local Variables in Python
- Local variables are the variables that are defined and declared inside the function.
- We cannot use these variables outside the function.
# This function uses local variable company in function
def nameCompany():
company = "BinFin Tech"
print(company)
nameCompany()
# Output:
BinFin Tech
B) Global Variables in Python
- Global variables are the variables that are not defined and declared outside the function.
- It is used inside the function when we want to assign a new value for the global variable.
If you want to use the global variable inside the function then use the global keyword.
def num():
global a # global a =3
a = 3 # We can assign the value in these way
# for the global keyword variable
print(a)
num()
# Output :
3
Type Casting of Variables in Python
Type Casting is the technique to convert one variable datatype to another variable datatype to perform some operation by the users.
a) Implicit Type Casting
In these types of type casting, Python converts one datatype to another data type without the involvement of the users which means Python automatically converts.
Number1 = 5
Number2 = 2.5
Number3 = Number1 * Number2
print(type(Number1))
print(type(Number2))
print(type(Number3))
# Output :
<class 'int' >
<class 'float' >
<class 'float' >
Here, Number1 is an integer datatype while Number2 is a float datatype. When we assign the multiplication of Number1 and Number2 in the Number3 Variable.
Number 3 automatically gets its datatype of Float.
b) Explicit Type Casting
In this method, Python needs user involvement to convert the datatype from one to another. This conversion can be done through Python’s inbuilt function. Basically, there are three methods in Python, which are as:
- int(): It takes float or string as an argument and returns int type object.
- float(): It takes int or string as an argument and returns float type object.
- str(): It takes float or int as an argument and returns a string-type object.
FAQ
Variables in Python are nothing but reserved memory locations for the values.
When you are writing the program at that time it does not create a memory for the variable, it reserved some memory space when you run the script file or program.
There are two types of variables available in Python, i.e., local and global variables.
Local variables are the variables that are defined and declared inside the function.
Global variables are the variables that are not defined and declared outside the function.
Type casting is the technique of converting one variable datatype to another variable datatype to perform some operation by the users.
There are two types of typecasting available in Python i.e; Implicit Type Casting and Explicit Type Casting.