" Variables "  in Python.

" Variables " in Python.

Hey techies, I hope and wish u have a good day today, welcome to this week's article. Today, I am going to make you gain knowledge on "Variables in python". Hope you are loving my articles and I appreciate it if you show a response by commenting, liking or even sharing on your socials. I am focusing more on Python so I also suggest you to read my previous articles on Python.

1.) What are Variables?

The Variable is a name that we are going to give or assign some value to store its data or address in it.

  • Python is a language where we dont need to assign a variable about its type.

  • but in the case of other languages c, c++, java...etc we need to assign what type of variable is it. Ex: (int a = 9 ) here we need to declare that a is an integer which is storing 9(integer) value in it.

  • int a = 67 , char B = "smd" ,float k = 8.74.

  • but in the case of python no need to assign like other languages its very simple.

  • Ex: a = 54 , b = " smd " , c = 7.89 here python will automatically take them based on the value which we give as integer, character, float).

2.) Important Rules to be followed while assigning variable?

  • The identifier name must not contain any white space, or special character (!, @, #, %, ^, &, *).

  • Identifier name must not be similar to any keyword

  • Identifier names are case sensitive ; for example, "im smd" , and "ImSmd" both are not same.

  • All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore,

  • Examples of valid identifiers: a123, n, n9, etc.

  • Examples of invalid identifiers: 1a, n%4, n 9, etc.

Consider the following valid variables name.

name = "s"  
Name = "m"  
naMe = "d"  
NAME = "s"  
n_a_m_e = "o"  
_name = "a"  
name_ = "i"  
_name_ = "l"  
na56me = "36"  

print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, na56me)
  • Camel Case - In the camel case, each word or abbreviation in the middle of begins with a capital letter. There is no intervention of whitespace. For example - name of student, valueOfVaraible, etc.

    Example - imSohail,etc

  • Pascal Case - It is the same as the Camel Case, but here the first word is also capital. Example - NameOfStudent, etc.

  • Snake Case - In the snake case, Words are separated by the underscore. For Example - name_of_student, etc.

3.) Object References

Python is a highly object-oriented programming language; that's why every data item belongs to a specific type of class.

Consider the following example.

a = "smd"
print(type(a))  #it will print that what type of variable is "a"here.

Output:
<class 'str'>

4.) Assigning single value to multiple variables.

we can assign multiple values to single value or we can assign multiples to single variables.

#ASSIGING SINGLE VALUE TO MULTIPLE VARAIABLES
a = b = c = 5
print(a)
print(b)
print(c)

Output:
5
5
5
#ASSIGNING MULTIPLE VALUES TO MULTIPLE VARIABLES
a=10 b=20 c= 56
print(a,b,c)

Output:
10,20,56

""" lets go bit advance but simple to understand """

Python Variable Types:

  1. LOCAL VARIABLES

  2. GLOBAL VARIABLES

LOCAL VARIABLES:

Local variables are the variables that are declared inside the function and have importance to it within the function. Let's understand the following example.

# Declaring a function  
def add():  
    # Defining local variables. They has scope only within a function  
    a = 20  
    b = 30  
    c = a + b  
    print("The sum is:", c)  

# Calling a function  
add()  

Output:
The sum is: 50

If we try to use them outside the function, we get the following error.

add()      #calling function (u will learn abut functinos topic later)
#      Now Accessing local variable outside the function   
print(a)  


Output:
The sum is: 50
    print(a)
NameError: name 'a' is not defined.

Global Variables

Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function.

A variable declared outside the function is the global variable by default.

Python provides the global keyword to use global variable inside the function.

If we don't use the global keyword, the function treats it as a local variable. Let's understand the following example.

# Declare a variable and initialize it  
x = 101                            #inializing "x "outside function

# Global variable in function  
def mainFunction():  
    # printing a global variable  
    global x  #remember we have assigned x as global by using global keyword

    print(x)  
    # modifying a global variable  
    x = 'Welcome To mytech_blog'  
    print(x)  

mainFunction()  
print(x)

AND THATS IT FOR TODAY LETS CHECK OUT IN 2ND SERIES

QUOTE OF THE DAY:

The Future Depends Upon What You Do, Today.

Did you find this article valuable?

Support Smd Sohail by becoming a sponsor. Any amount is appreciated!