Introduction to Python Variables.

Posted on

In all previous 3 articles, we are discussing basics of python, what is an indentation, how to do comment in code, so at this point, you all know very well about python basic, if you don’t reach then there is a link for all python articles https://smgplaza.com/category/python/ just follow 1 to 3 articles, from this link. We know about python variable, How to declare a variable, How to assign a value for a variable, How to assign value for multiple variables but What is output variable and what is a global and global variable, let see…

What is the Python variable:-

First of all, Variable is some byte of memory where any value or character is store in memory, the variable for storing data value, python has no data type, Python has no command for variable declaration, To create a variable just give a name of a variable and assign value like below,

a = 10
b = 19.8
c = "SMGPLAZA"
print(a)
print(b)
print(c)

Output Of Above code:-

10 19.8 SMGPLAZA

The variable doesn’t need to declare with any type, also if you want to change the datatype then you can.

a = 10      #a is a int type
b = 19.8    #b is a float type
c = "SMGPLAZA"  #c is a character type
print(a)
print(b)
print(c)

Output Of Above code:-

10 19.8 SMGPLAZA

Variable Names:-

A variable naming has particular no rules, but in programming, we are declared as a small name like x, y, a, b, etc… Else we are declaring a variable with meaning full name.

Rules for python variable

  • The name started with the letter or underscore character.
  • Name can’t start with numbers.
  • Name can only start with the alpha-numerical characters and underscores.
  • Names are case-sensitive.

Legal variable name:-

var = "SMGPLAZA"  #legal variable name 
mvar = "PYTHON"   # Legal variable name

Illegal variable name:-

9var = "Jay"      #Illegal variable name 
m-var = "Manav"   #Illegal variable name 
hi var = "Riya"   #Illegal variable name 

Assign a value for multiple variables:-

In python allow values to multiple variable in one line. like below,

a, b, c ="smgplaza" , "jay" , "manav"
print(a)
print(b)
print(c)

Output:-

smgplaza  jay  manav

Output variable:-

Variables that are created outside of a function are known as global variables. It can be used by, both inside and outside functions.

how to create a variable outside the function.

x = "superb"

def func():
  print("Python is " + x)

func()

Output:-

python is superb

How to create a variable inside the function.

x = "superb"

def function():
  x = "awesome"
  print("Python is " + x)

function()

print("Python is " + x)

Output:-

python is awesome. python is superb

Global keyword:-

To create a global variable in function, you can use the global keyword.

use of global keyword

def myfun():
  global a
  a = "fabulous"

myfun()

print("smgplaza is " + a)

Output:-

smgplaza is fabulous

Use global keyword inside the function

x = "awesome"

def fun():
  global x
  x = "superb"

fun()

print("Python is " + x)

Output:-

python is superb

Also visit this articles :-

For All python article, you have to follow this link, https://smgplaza.com/category/python/

If you have any doubts any suggestions or any query DM us one below Social media handles.

Our Social Media Handles:-

Instagram Handle :- https://www.instagram.com/smgplaza/

Twitter Handle :- https://twitter.com/smgplaza

Leave a Reply

Your email address will not be published. Required fields are marked *