Python Variables | Local And Global Variables | TECH WORTHY MIND
Variables
Variables are like containers that are used to store data values.
Creating Variables
There is no command in Python for declaring a variable.
Simply variable is created by assigning a value to it.
Example- X=5
Y= "John"
Print(X)
Print(Y)
There is no need to declare a particular type of variable, we can change the type after it is set.
Casting
By using casting we can specify the data type of variable.
Example-
x=str(3) #will be '3'
y=int(3) #will be 3
z=float(3) #will be 3.0
Get the Type
The data type of variable can be determined by using the type() function.
Example- x=5
print(type(x))
Output <class 'int'>
Variable Names
A variable can have an appropriate and short name ( like x, age, name, etc...)
• Variations always begin with a letter or the underscore character.
• Starting with a number is strictly prohibited.
• Variable name can only contain underscore and alphanumeric character (A-z, 0-9 and _)
• Variable name is case-sensitive ie. Name, name treated as two variable names.
Legal Variable names:
myvar="Harshit"
my_var="Harshit"
_my_var="Harshit"
myvar2="Harshit"
Illegal Variable names:
2myvar="Harry"
my-var="Harry"
my var="Harry"
• Multi words Variable names:
Variables more than one word is difficult to read but by using some methods we can make it simple
Camel case
myVariableName="Bob"
Pascal case
MyVariableName="John"
Snake case
my_variable_name="Honey"
One Value to Multiple Variable
Unpack a collection
If we have a pack of values in a list, tuple, etc. Python provides us to extract the values into variables. This is called unpacking.

Comments
Post a Comment
DON'T COMMENT LINK.