• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Delete Variable – How to Delete Variables with del Keyword

Python Delete Variable – How to Delete Variables with del Keyword

February 14, 2022 Leave a Comment

In Python, we can easily delete a variable with the del keyword. The del keyword deletes variables in your Python code.

string = "this is a string"

del string

print(string)

#Output:
Error: string is not defined

When working in Python, sometimes it makes sense to be able to delete a variable or delete multiple variables in your program.

The Python del keyword allows us to delete objects. We can use the del keyword to delete variables, user-defined objects, lists, items within lists, and dictionaries.

For example, let’s say we have a simple string variable. We can delete it with the Python del keyword as shown in the following code. When you try to access the string variable after deleting it, you receive an error.

string = "this is a string"

del string

print(string)

#Output:
Error: string is not defined

The rest of this article will show you various examples in Python of how to delete variables in your Python programs.

How to Delete a Variable in Python with del Keyword

In Python, we can delete any variable with del.

Below are a handful of examples of how you can use del to delete a string variable or a float variable.

string = "this is a string"
float = 10

del string
del float

print(string)
print(float)

#Output:
Error: string is not defined
Error: float is not defined

Deleting Dictionary Variables in Python with del Keyword

You can also delete dictionary variables in Python easily.

Below is an example of how you can delete a dictionary variable using the del keyword in Python.

dict = {"name":"Kelly", "height":55}

del dict

print(dict)

#Output:
Error: dict is not defined

Deleting Lists in Python with del Keyword

You can also delete list variables in Python easily.

Below is an example of how you can delete a dictionary variable using the del keyword in Python.

list = [0,1,2,3]

del list

print(list)

#Output:
Error: list is not defined

Hopefully this article has been useful for you to understand how you can delete variables in Python with the del keyword.

Other Articles You'll Also Like:

  • 1.  pandas variance – Compute Variance of Variables in DataFrame
  • 2.  Why Dictionaries Can’t Have Duplicate Keys in Python
  • 3.  How to Check if Number is Negative in Python
  • 4.  pandas tail – Return Last n Rows from DataFrame
  • 5.  Creating a List of Zeros in Python
  • 6.  Using Python to Flatten Array of Arrays
  • 7.  Convert String into Tuple in Python
  • 8.  How to Check if Variable Exists in Python
  • 9.  Get Day of Week from Datetime in pandas DataFrame
  • 10.  Using Selenium to Get Text from Element in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy