How to Declare and use a Variable
​
Python variable defined using simple ways like you can see it below given example and print it–
>>> a = 5
>>> print(a)
​
You can get result 5.
Concatenate Variables
​
We can concatenate Variables using below given logic:
>>> a = 5
>>> b = 4
>>> print(“The Result is”, str( a)+ “” +str(b))
Naming convention of Variable in python
A variable can have a short name (like x and y) or a more descriptive name (name, age, gender, carname, total_average).
Rules for Python variables:
-
A variable name must start with a letter or the underscore character
-
A variable name cannot start with a number
-
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
-
Variable names are case-sensitive (age, Age and AGE are three different variables)
The common naming styles in Python code and when you should use them:
-
Variable: Use a lowercase single letter, word, or words. Separate words with underscores to improve readability. Example: x, var, my_variable
-
Constant: Use an uppercase single letter, word, or words. Separate words with underscores to improve readability. Example: PI, MAX_VALUE, MIN_VALUE
-
Function: Use a lowercase word or words. Separate words by underscores to improve readability. Example: display, show, my_function
-
Class: Start each word with a capital letter. Do not separate words with underscores. This style is called camel case. Example: Employee, Student, Person, Model, MyClass
-
Module: Use a short, lowercase word or words. Separate words with underscores to improve readability. Example: module.py, my_module.py
-
Package: Use a short, lowercase word or words. Do not separate words with underscores.Example: package, mypackage
Assign Value to Multiple Variables once in one line
-
Assign different values to multiple variables in one line:
x, y, z = 5 ,10 ,15
print(x)
print(y)
print(z)
-
Assign same value to multiple variables in one line:
x, y, z = 10
print(x)
print(y)
print(z)
Global Variables
Global variable is used to access command value through out the program. Global variable can be accessed from inside or outside from function, class or methods.
​
Global variable in python can be defined by two ways:
​
-
Without global keyword.
-
Using global keyword.
-
Without global keyword
college_id = 10
def myfunc():
print("Python is " + college_id)
myfunc()
Note: Always try to define global variable top line of program so that rest of the code after that can use it.
2. Using global keyword
def myfunc():
global college_id
college_id = 10
myfunc()
print("Python is " + college_id)
What will happen If you create a variable with the same name inside a function
college_id = 10
def myfunc():
college_id = 20
print("Id inside function " + college_id)
myfunc()
​
print("Id outside function " + college_id)
Output:
​
Id inside function 20
Id outside function 10
​
local value will be used if we create variable with the same name as global
Thanks for reading this, In the next tutorial, we will learn about “python data types”