Best Python Variable Declaration Scope Global Keyword |Local Variables

What is Variable in Python and How to Create Variables in Python

Python in Variable: It is a name of storage space which is used to store data. Its value may be changed. It always contains the last value stored to it. There is no need to declare a variable in python. A variable is created by assigning a value to it.

Variable is a name which is used to refer to a memory location. Variable also known as identifier and used to hold value.

In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type. 

Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore. It is recommended to use lowercase letters for variable name.  and x both are two different variables. 

How do you initialize a variable in Python?

An identifier is used to identify the literals used in the program. The rules to name an identifier are given below.

What are the 5 rules to declare variable name?

  • The first character of the variable must be an alphabet or underscore (-).
  • All the characters except the first character may be an alphabet of lower-case(a-z). upper-case (A-Z), underscore or digit (0-9).
  • Identifier name must not contain any white-space, or special character (, @ #, %, &,).
  • The identifier name must not be similar to any keyword defined in the language.
  • Identifier names are case
  • sensitive, for example my name, and MyName is not the same. Examples of valid identifiers a123, n, n_9, etc.
  • Examples of invalid identifiers 1a, n%4, n 9, etc


 Best Python Variable Declaration Scope Global Keyword |Local Variables 


    What is variable initialization example?

    student_name="Ravi Kumar"

    student_rollno=202324

    student_marks=95.6

    Here, the student_name student_rollno and student_marks are the name of variables. Value of student_name is Ravi Kumar, student_rollno is 202324 and student_marks is 95.6.

    Variable Type Casting(Conversion) str number to integer number in Python

    n1=input("Enter first number:")

    n1=int(n1)

    n2=input("Enter second number:")

    n2=int(n2)


    print("Value of n1:",n1)

    print("Value of n2:",n2)


    print(type(n1))

    print(type(n2))


    **********OUTPUT**********

    Enter first number:34

    Enter second number:67

    Value of n1:34

    Value of n2:67

    type(int)

    type(int)

    Variable Type Casting(Conversion) str number to float number in Python

    n1=input("Enter first number:")

    n1=float(n1)

    n2=input("Enter second number:")

    n2=float(n2)


    print("Value of n1:",n1)

    print("Value of n2:",n2)


    print(type(n1))

    print(type(n2))


    **********OUTPUT**********

    Enter first number:34.67

    Enter second number:67.89

    Value of n1:34.67

    Value of n2:67.89

    type(float)

    type(float)

    Printing value of a variable: We can print the value of a variable using print() function.

    #creating variables in python


    student_name="Ravi Kumar"

    student_rollno=202324

    student_marks=95.6


    print("Student Name:", student_name)

    print("Student Roll:", student_rollno)

    print("Student Marks:", student_marks)


    **********OUTPUT**********

    Student Name: Tom

    Student Roll: 202324

    Student Marks: 95.6


    How do you assign a value to a variable in Python?

    We can assign a single value to multiple variables using a single statement in python.

    #creating variables in python

    a, b, c=50,60,70


    print("Value of a:", a)

    print("Value of b:", b)

    print("Value of c:", c)


    **********OUTPUT**********

    Value of a: 50

    Value of b: 60

    Value of c: 70


    How do you assign a single value to a multiple variable in Python?

    We can also assign multiple values to multiple variables using a single statement in python.

    #creating variables in python

    a, b, c=50,60,70


    print("Value of a:", a)

    print("Value of b:", b)

    print("Value of c:", c)


    **********OUTPUT**********

    Value of a: 50

    Value of b: 60

    Value of c: 70


    What are the 5 rules to declare variable name?

    1. The first letter of a variable should be alphabet or underscore(_).

    2. The first letter of the variable should not be a digit.

    3. After the first character, it may be a combination of alphabets and digits.

    4. Blank spaces are not allowed in variable name.

    5. Variable name should not be a keyword.

    6. Variable names are case-sensitive, for example marks and MARKS are different variables.


    What is a local variable example?

    Local Variable: A variable declared inside the body of the function is called a local variable. A local variable can be used only inside that function in which it is defined.

    def add():

        x=50

        y=30

        z=x+y

        print("Addition of z:", z)

    add()


    #This line will raise an error because x is a local variable, and it can't be accessed outside a function.

    print(x)


    **********OUTPUT**********

    Addition of z: 80

    NameError: name 'x' is not defined


    What is global and local in Python?

    Global Variable: A variable which is created outside a function is called a global variable. It can be used anywhere in the program.

    #creating global variable in python

    x=50

    y=30

    def add():


        print("Inside function Sum:", x+y)

    add()


    print("Outside function Sum:", x+y)


    **********OUTPUT**********

    Inside function Sum: 80

    Outside function Sum:80


    Local and Global variable with same name in python.

    x=50

    def add():

        x=20

        print("Inside function x=", x)


    add()

    print("Outside function x=", x)


    **********OUTPUT**********

    Inside function x= 20

    Outside function x= 50


    What is the use of a global keyword?

    global keyword: We can't modify the value of global variable inside a function, but by using global keyword we can modify the value of global variable inside a function.

    x=50

    def add():

        global x

        x=20

        print("Inside function x=", x)

    add()

    print("Outside function x=", x)


    **********OUTPUT**********

    Inside function x= 20

    Outside function x= 20


    How do you create a token in Python?

    Tokens: Tokens can be defined as a Punctuation mark, reserved words and each individual word in a statement.
    Token is the smallest unit inside the given program. There are the following tokens in Python:

    1. Keywords

    2. Identifiers

    3. Literals

    4. Operators

    Tuple: Tuple is another form of collection where different type of data can be stored. . It is similar to a list where data is separated by commas. The only difference is that list uses square bracket and tuple uses parenthesis. Tuples are enclosed in parentheses and cannot be changed.

    Python Dictionary: Dictionary is a collection which works on a key-value pair. It works like an associated array, where no two keys can be the same. Dictionaries are enclosed by Curly braces () and values can be retrieved by square bracket(0).

    How do you take user input in Python?

    User input in python: Python input() function is a predefined function which is used to take user input in Python. Default user input is of type string.

    name=input("Enter your name:")

    print("Your Name is:", name)

    print(type(name))


    **********OUTPUT**********

    Enter your name:Ravi Kumar

    Your Name is:Ravi Kumar

    <class 'str'="">


    How to declare python user-input of integer value

    Integer: int() function is a predefined function which is used to convert string into integer.

    number=input("Enter any number:")

    print("Type of number:", type(number))


    num=int(number)

    print("Given Number:", num)

    print("Type of number:", type(num))


    **********OUTPUT**********

    Enter any number:204

    Type of number: <class 'str'="">


    Given Number: 204

    Type of number: <class 'int'="">


    Integer value user-input in python language

    number=int(input("Enter any number:"))

    print("Given Number:", number)

    print("Type of number:", type(number))


    **********OUTPUT**********

    Enter any number:204

    Given Number: 204

    Type of number: <class 'int'="">


    What is a float value in Python?

    user-input float value: float() is a predefined function which is used to convert string into float.

    marks=float(input("Enter your marks:"))

    print("Your Marks is:", marks)


    print("Type of number:", type(marks))


    **********OUTPUT**********

    Enter your marks:84.2

    Your Marks is: 84.2

    Type of number: <class 'float'="">


    FAQ Variables People ask Questions

    Q. Python declare variables types.
    Ans: a=100 
    b= 18.4
    c="Python"
    Here a is a integer type value or b is a floating value and c is a string value in python Language.

    Q. Variables declaration in python rules.
    Ans: The first letter of a variable should be alphabet or underscore(_).

    2. The first letter of the variable should not be a digit.

    3. After the first character, it may be a combination of alphabets and digits.

    4. Blank spaces are not allowed in variable name.


    Q. Variables in python Example
    Ans: a =100
    b=200
    This is a variable in python Language.
    Here, an is a variable name and 100 is a value in python Language.
    print(a)
    print(b)

    Q. Python Variables Types.
    Ans: Python Language are provided different types of variables like integer, floating, character, string, sign, unsigned,
    longint and more variables types.

    Q. Python variables scope.
    Ans: Python Language provide variables scope like local scope and global scope in python Language.

    Conclusion: Hello python programmer, you can learn python variables in this post. In this post, all topic cover in python Variables. You can any suggestions, please comment.




    Post a Comment

    0 Comments