Uttar Pradesh, India
Instagram
image

Python variables

Variables are the basic building blocks of any programming language. Python variables related to the holding of data during program execution.In python programming language variables holding any data type like String, Int, and Boolean.

In python, every data type is Object Type.

In python, variables are created or declared when you assign a value to it at the moment.

 

Important points related to the declaration of python variables:

  • Python variable doesn't start with numerical and special characters. It gives below error looks like
  • Python variables are dynamic type, it can be changed to any type at the run time. (Python is dynamically typed language)
  • Variable names are case-sensitive (test, Test, and TEST are three different variables)

 

 

Sample Code:

@123="Test"          

 

O/P:

File "main.py", line 3

    123="test"

    ^

SyntaxError: can't assign to literal

Declaring Some Python Variables Examples:

  • Declare String, Int, Float and Boolean Type Variables

           Sample Code:

           a="Hello World !"

           b=4500

           c=45.56

           d=True

           print(a)

           print(b)

           print(c)

           print(d)

  • Declare Multiple Variables in Single Line

          Sample Code:

          a1,b1,c1,d1=1,"test",3.5,False

          print(a1)

          print(b1)

          print(c1)

          print(d1)

What's the means of global keywords in the context of variables in python

The variables declared outside the function is known as a global variable.

 

Let's look at the below program:

x="Test"

 

def func():

 x="123"

 return x

 

print(x)

print(func())

 

In the above program, the variable 'x' used in two places. So x is global as well as a local variable. The variable 'x' used inside a function is known as a local variable and the variable declaration at the start is known as a global variable(Find the screenshot).

 


Comments

Share your thoughts in the comments

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