Computer Science, asked by pavani2034, 3 months ago

What are variables in python? Can anyone explain me

Answers

Answered by Anonymous
5

Answer:

A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.

Python Variable Types

Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.

Python Variable Types: Local & Global

There are two types of variables in Python, Global variable and Local variable. When you want to use the same variable for rest of your program or module you declare it as a global variable, while if you want to use the variable in a specific function or method, you use a local variable while Python variable declaration.

Let's understand this Python variable types with the difference between local and global variables in the below program.

Let us define variable in Python where the variable "f" is global in scope and is assigned value 101 which is printed in output

Variable f is again declared in function and assumes local scope. It is assigned value "I am learning Python." which is printed out as an output. This Python declare variable is different from the global variable "f" defined earlier

Once the function call is over, the local variable f is destroyed. At line 12, when we again, print the value of "f" is it displays the value of global variable f=101

While Python variable declaration using the keyword global, you can reference the global variable inside a function.

Variable "f" is global in scope and is assigned value 101 which is printed in output

Variable f is declared using the keyword global. This is NOT a local variable, but the same global variable declared earlier. Hence when we print its value, the output is 101

We changed the value of "f" inside the function. Once the function call is over, the changed value of the variable "f" persists. At line 12, when we again, print the value of "f" is it displays the value "changing global variable"

Answered by unigashotel
1

Answer:

a variable is an identifier which holds a value. in programing we say that we assign a value to a variable, technically,a variable is a reference to the computer memory, where the value is stored. ...

Similar questions