Skip to main content

Python Variables: How to Define/Declare String Variable Types

 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.

How to Declare and use a Variable

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


a=100 

print (a)

Re-declare a Variable

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 “Welcome FutureCode"


Python 2 Example

This code is editable. Click Run to Execute
1
# Declare a variable and initialize it
2
f = 0
3
print f
4
# re-declaring the variable works
5
f = 'FutureCode'
6
print f


Python 3 Example

This code is editable. Click Run to Execute
1
# Declare a variable and initialize it
2
f = 0
3
print(f)
4
# re-declaring the variable works
5
f = 'FutureCode'
6
print(f)

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 the 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 these Python variable types with the difference between local and global variables in the below program.

  1. Let us define a variable in Python where the variable “f” is global in scope and is assigned value 101 which is printed in the output
  2. Variable f is again declared in function and assumes local scope. It is assigned the value “I am learning Python.” which is printed out as an output. This Python declare a 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

Variables in Python


Python 2 Example

This code is editable. Click Run to Execute
1
# Declare a variable and initialize it
2
f = 101
3
print f
4
# Global vs. local variables in functions
5
def someFunction():
6
# global f
7
    f = 'I am learning Python'
8
    print f
9
someFunction()
10
print f


Python 3 Example

This code is editable. Click Run to Execute
1
# Declare a variable and initialize it
2
f = 101
3
print(f)
4
# Global vs. local variables in functions
5
def someFunction():
6
# global f
7
    f = 'I am learning Python'
8
    print(f)
9
someFunction()
10
print(f)


While Python variable declaration uses 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 the 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”

Variables in Python

Python 2 Example

This code is editable. Click Run to Execute
1
f = 101;
2
print f
3
# Global vs.local variables in functions
4
def someFunction():
5
  global f
6
  print f
7
  f = "changing global variable"
8
someFunction()
9
print f


Python 3 Example

This code is editable. Click Run to Execute
1
f = 101;
2
print(f)
3
# Global vs.local variables in functions
4
def someFunction():
5
  global f
6
  print(f)
7
  f = "changing global variable"
8
someFunction()
9
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 the error “variable name is not defined” which means you have deleted the variable.

Variables in Python

Example of Python delete variable or Python clear variable :

This code is editable. Click Run to Execute
1
f = 11;
2
print(f)
3
del f
4
print(f)









Comments

Popular posts from this blog

Tether (cryptocurrency)

The most liquid, stable , and trusted stable coin . Launched in 2014 by a group of Bitcoin enthusiasts and early adopters, Tether is a blockchain -enabled platform designed to facilitate the use of fiat currencies digitally. Tether is a token backed by actual assets, including USD and Euros. One Tether equals one underlying unit of the currency backing it, e.g., the U.S. Dollar, and is backed 100% by actual assets in the Tether platform’s reserve account. Being anchored or “tethered” to real-world currency, Tether provides protection from the volatility of cryptocurrencies . Tether enables businesses — including exchanges, wallets, payment processors, financial services, and ATMs — to easily use fiat-backed tokens on blockchains . By leveraging Blockchain technology, Tether allows you to store, send and receive digital tokens person-to-person, globally, instantly, and securely for a fraction of the cost of alternatives. Tether’s platform is built to be fully transparent at all times....

What to expect from AI in 2023

  As a rather commercially successful author once wrote, “the night is dark and full of terrors, the day bright and beautiful and full of hope.” It’s fitting imagery for AI, which like all tech has its upsides and downsides. Art-generating models like  Stable Diffusion , for instance, have led to incredible outpourings of creativity, powering  apps , and even  entirely new business models . On the other hand, its open-source nature lets bad actors use it to create  deepfakes  at scale — all while  artists protest that it’s profiting off of their work . What’s on deck for AI in 2023? Will regulation rein in the worst of what AI brings, or are the floodgates open? Will powerful, transformative new forms of AI emerge, a la  ChatGPT , and disrupt industries once thought safe from automation? Expect more (problematic) art-generating AI apps With the success of  Lensa , the AI-powered selfie app from Prisma Labs that went viral, you can expect...

What is Boxing Day

  Boxing Day is a holiday that is celebrated on December 26th in several countries, including Canada, the United Kingdom, Australia, and New Zealand. It is a public holiday that traditionally follows Christmas Day and is often a day off work for many people. The origins of the holiday are not definitively known, but it may have originated as a day for servants and tradespeople to receive gifts from their employers or as a way for the wealthy to give back to the less fortunate. In modern times, Boxing Day is often celebrated with activities such as shopping, sports events, and visiting with friends and family.