Introduction to Python variables.

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 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, For 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)
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)

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:-

Assign a value for multiple variables means In python you can assign values to multiple variable in one line. like below,

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

Output variable:-

The output variable is used to print a statement. It’s also used to combine two or more text and variables values in a single variable. You can display all variable values in a single variable output by using only the “+” symbol.

a = "smgplaza"
b = ", simple solution for a complex problem"
z =  x + y
print(z)
smgplaza, simple solution for a complex problem.

What is the global variable:-

What is the global 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 variable outside the function.

x = "superb"

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

func()
python is superb

How to create variable inside the function.

x = "superb"

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

function()

print("Python is " + x)
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)
smgplaza is fabulous

Use global keyword inside the function

x = "awesome"

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

fun()

print("Python is " + x)
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

Thank you so much for your support, you all are very valuable for us. You have to follow us using your mail id and also follow us on social media for more updates. We are one by one covering all topics of python and android both. If you have any query then comment me on the below comment box.