How do i define a variable in python?


In Python, we need not declare a variable with some specific data type.

Python has no command for declaring a variable. A variable is created when some value is assigned to it. The value assigned to a variable determines the data type of that variable.

Thus, declaring a variable in Python is very simple.

  • Just name the variable

  • Assign the required value to it

  • The data type of the variable will be automatically determined from the value assigned, we need not define it explicitly.

Declare an integer variable

To declare an integer variable −

  • Name the variable

  • Assign an integer value to it

Example

 Live Demo

x=2
print(x)
print(type(x))

This is how you declare a integer variable in Python. Just name the variable and assign the required value to it. The datatype is automatically determined.

Output

2

Declare a string variable

Assign a string value to the variable and it will become a string variable. In Python, the string value cane be assigned in single quotes or double quotes.

Example

 Live Demo

x='2'
print(x)
print(type(x))

Output

2

Declare a float variable

A float variable can be declared by assigning float value. Another way is by typecasting.

We will use both.

Example

 Live Demo

x=2.0
print(x)
print(type(x))
y=float(2)
print(y)
print(type(y))

Output

2.0

2.0

Note: The string variable can also be declared using type casting, when using integer values as string.

Unlike some another languages, where we can assign only the value of the defined data type to a variable. This means an integer varibale can only be assigned an integer value throughout the program. But,in Python , the variable are not of a particular datatype. Their datatype can be changed even after it is set.

The following example will clarify the above concept.

Example

 Live Demo

x=10
print(x)
print(type(x))
x="abc"
print(x)
print(type(x))

Output

10

abc

The variable x was of type int. Later when string value is assigned to it, it changes into a string variable.

How do i define a variable in python?

Updated on 10-Mar-2021 14:07:20

  • Related Questions & Answers
  • How to declare a global variable in Python?
  • How to declare a variable in MySQL?
  • How to declare a variable in C++?
  • How do we declare variable in Python?
  • MySQL how to declare a datetime variable?
  • How to declare a local variable in Java?
  • How to declare a global variable in C++
  • How to declare a global variable in PHP?
  • How to declare a variable correctly in a MySQLProcedure?
  • How do I declare a global variable in Python class?
  • How to declare a variable in Python without assigning a value to it?
  • How to declare a variable inside a procedure in MySQL?
  • How to declare a variable within lambda expression in Java?
  • How to declare a variable in MySQL for a normal query?
  • Using DECLARE to create variable in MySQL?

What is a Variable in Python?

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.

In this tutorial, we will learn,

  • How to Declare and use a Variable
  • Re-declare a Variable
  • Concatenate Variables
  • Local & Global Variables
  • Delete a variable

How to Declare and use a Variable

Let see an example. We will define variable in Python and declare it as “a” and print it.

a=100 
print (a)

You can re-declare Python variables even after you have declared once.

Here we have Python declare variable initialized to f=0.

Later, we re-assign the variable f to value “guru99”

How do i define a variable in python?

Python 2 Example

# Declare a variable and initialize it
f = 0
print f
# re-declaring the variable works
f = 'guru99'
print f

Python 3 Example

# Declare a variable and initialize it
f = 0
print(f)
# re-declaring the variable works
f = 'guru99'
print(f)

Python String Concatenation and Variable

Let’s see whether you can concatenate different data types like string and number together. For example, we will concatenate “Guru” with the number “99”.

Unlike Java, which concatenates number with string without declaring number as string, while declaring variables in Python requires declaring the number as string otherwise it will show a TypeError

How do i define a variable in python?

For the following code, you will get undefined output –

a="Guru"
b = 99
print a+b

Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)= “Guru99” in the output.

a="Guru"
b = 99
print(a+str(b))

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.

  1. Let us define variable in Python where the variable “f” is global in scope and is assigned value 101 which is printed in output
  2. 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
  3. 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

How do i define a variable in python?

Python 2 Example

# Declare a variable and initialize it
f = 101
print f
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print f
someFunction()
print f

Python 3 Example

# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
    f = 'I am learning Python'
    print(f)
someFunction()
print(f)

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

  1. Variable “f” is global in scope and is assigned value 101 which is printed in output
  2. 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
  3. 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”

How do i define a variable in python?

Python 2 Example

f = 101;
print f
# Global vs.local variables in functions
def someFunction():
  global f
  print f
  f = "changing global variable"
someFunction()
print f

Python 3 Example

f = 101;
print(f)
# Global vs.local variables in functions
def someFunction():
  global f
  print(f)
  f = "changing global variable"
someFunction()
print(f)

Delete a variable

You can also delete Python variables using the command del “variable name”.

In the below example of Python delete variable, we deleted variable f, and when we proceed to print it, we get error “variable name is not defined” which means you have deleted the variable.

How do i define a variable in python?

Example of Python delete variable or Python clear variable :

f = 11;
print(f)
del f
print(f)

Summary:

  • Variables are referred to “envelop” or “buckets” where information can be maintained and referenced. Like any other programming language Python also uses a variable to store the information.
  • Variables can be declared by any name or even alphabets like a, aa, abc, etc.
  • Variables can be re-declared even after you have declared them for once
  • Python constants can be understood as types of variables that hold the value which can not be changed. Usually Python constants are referenced from other files. Python define constant is declared in a new or separate file which contains functions, modules, etc.
  • Types of variables in Python or Python variable types : Local & Global
  • Declare local variable when you want to use it for current function
  • Declare Global variable when you want to use the same variable for rest of the program
  • To delete a variable, it uses keyword “del”.

How do you define a variable?

A variable is any characteristics, number, or quantity that can be measured or counted. A variable may also be called a data item. Age, sex, business income and expenses, country of birth, capital expenditure, class grades, eye colour and vehicle type are examples of variables.

Can you define a variable in a function Python?

The practical upshot of this is that variables can be defined and used within a Python function even if they have the same name as variables defined in other functions or in the main program. In these cases, there will be no confusion or interference because they're kept in separate namespaces.

What is an example of a variable in Python?

The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables. 2. The name of the variable cannot start with a number.