Latest posts

10/recent/ticker-posts

Variable Declaration in Python

Variable Declaration in Python

What are the variables?

Variables is a named location in memory that stores a value.
They used to store data values in a program.
Variables can be created by assigning a value to a new or existing variable name using the assignment operator (=).
The value assigned in a variable can be changed during program execution.

Structure to create variable in Python

 variable_name = value

For example:

 x = 5
 y = "Hello World"
From the upside example we can see there are two variables x and y which contain specific type of data.
In the simple language variable is containers which stores values.

There are few rules to follow when naming variables in Python:

  1. Variable names can only contain letters, numbers and underscores (A-z, 0-9, and _).
  2. A variable name must start with a letter or the underscore character.
  3. Variable name cannot start with number.
  4. Variables names should not be the same as keywords in Python.
  5. Variables names should be descriptive and meaningful.

Examples of valid and invalid variables:

Valid variables:

 var = 57
 _name = "Hello World"



Invalid variables:

 1var = 5
 $name = "Hello World"



Use of Variables in Programs :

Mostly variables can be created to store the values in memory locations.
For example, suppose we have to create a Simple program that can be Shows the Addition of two numbers.
In that we have to create variables for Storing the numbers and a different variable which can stores a String.
 num1 = 10
 num2 = 5
 result = a + b


Program :
 text = "Program For Addition of Two Numbers"
 print("")
 print(text)
 print("")
 num1 = input("Enter First Number : ")
 num2 = input("Enter Secont Number : ")

 """
 Here We are taking two Numbers from user, but the 
 function 'input()' can takes only the String Values from user
 So to do the addtion operation we need to Convert the String Valus
 to Numbers First
 """

 num1 = int(num1)   # Here we have converted our String Values to 
 Number
 num2 = int(num2)

 # Additon of Numbers 
 result = num1 + num2    # Here we are created another variable 'result',
                        #which can stores the Addition of num1 and num2

 # Now just Show the Result of Additon 
 print("")
 print(f"Addition is : {result}")
 print("")



To know about 'input( )' Function, check out out tutorial on Function in Puthon.

Hope this post will help you to learn JavaScript More...

Thank You...!!

Variable declaration in Python. Python variable initialization. Python variable types. Python variable scope. Variable naming conventions in Python. How to declare variables in Python. Python global and local variables. Python variable assignment. Understanding variables in Python. Python variable scope rules. Python variable declaration syntax. Declaring variables in Python 3. Python variable declaration best practices. Python variable assignment examples. Python variable declaration and initialization. Python variable naming conventions best practices. How to initialize variables in Python. Python variable declaration and assignment. What is variable declaration in Python. Python variable naming rules. Understanding variable scope in Python. Python variable initialization and assignment. Python variable declaration and naming conventions. How to declare and initialize variables in Python. Python variable scope and naming conventions.



Also Visit


Post a Comment

0 Comments