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

One value to assign multiple variables in one line 
X=Y=Z="MANGO"
print(X)
print(Y)
print(Z)

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.

Names=["Harshit","Harry","Hindu"]
x, y, z=Names
print(x)
print(y)
print(z)

Output Variables

In python print statements we can treat them as output variables.
To connect both two texts and a variable we use (+) sign in python.
X=Wonderful
print("Python is"+X)

Global Variables

We all know very well, the variables created outside the function are called global variables.
We can use global variables inside the function as well as outside
Example-
Create outside and use inside 
X="awesome"
def myfunc():
    print("python is "+X)
myfunc()


* The Global Keyword

If we create a variable inside the function we can't use it outside the function.
But to create a local variable as a global variable inside a function, we can use a global keyword.

def myfunc()
    global X
    X=" fantastic"
myfunc()
print("Python is "+X)

Previous                                             Next


Comments

Popular posts from this blog

How To Hack Wifi | Hack Wifi Password | TECH WORTH MIND

Remove Tools From Termux | How can I reopen the installed tool in termux? | TECH WORTHY MIND