Categories
Python Answers

How to define static class variables in Python?

Sometimes, we want to define static class variables in Python.

In this article, we’ll look at how to define static class variables in Python.

How to define static class variables in Python?

To define static class variables in Python, we can add a property directly to the class.

For instance, we write:

class Test(object):
  foo = 3

Test.bar = 2
print(Test.bar)  

to define the Test.bar static variable.

We assign a value outside the class.

And we access the property value without instantiating the class.

Therefore, Test.bar should be 2, which is what is printed.

Conclusion

To define static class variables in Python, we can add a property directly to the class.

Categories
Python Answers

How to remove a key from a Python dictionary?

Sometimes, we want to remove a key from a Python dictionary.

In this article, we’ll look at how to remove a key from a Python dictionary.

How to remove a key from a Python dictionary?

To remove a key from a Python dictionary, we can use the pop method.

For instance, we write:

my_dict = {'foo': 'abc', 'bar': 2}
deleted = my_dict.pop('foo', None)
print(deleted)
print(my_dict)

We call pop with key of the item to delete and the fallback value of the entry that’s deleted if the entry with the key doesn’t exist.

Therefore, deleted is 'abc'.

And my_dict is {'bar': 2} after pop is called.

If the key exists for sure in the dictionary, we can also use the del operator.

For instance, we write:

my_dict = {'foo': 'abc', 'bar': 2}
del my_dict['foo']
print(my_dict)

Then we get the same result for my_dict.

Conclusion

To remove a key from a Python dictionary, we can use the pop method.

If the key exists for sure in the dictionary, we can also use the del operator.

Categories
Python Answers

How to convert a string to lowercase in Python?

Sometimes, we want to convert a string to lowercase in Python.

In this article, we’ll look at how to convert a string to lowercase in Python.

How to convert a string to lowercase in Python?

To convert a string to lowercase in Python, we can use the Python string’s lower method.

For instance, we write:

s = "Kilometer"
print(s.lower())

And we see 'kilometer' logged.

Conclusion

To convert a string to lowercase in Python, we can use the Python string’s lower method.

Categories
Python Answers

How to get a substring of a string in Python?

Sometimes, we want to get a substring of a string in Python.

In this article, we’ll look at how to get a substring of a string in Python.

How to get a substring of a string in Python?

To get a substring of a string in Python, we can use the MyString[a:b] syntax, where MyString is a string and a and b are the start and end indexes.

The substring between a and b - l will be returned.

For instance, we write:

x = "Hello World!"
y = x[2:-2]
print(y)

to return a substring of x between index 2 and -2 exclusively.

Therefore, the substring will have the 3rd character to the 3rd last character.

Therefore, y is 'llo Worl'.

Conclusion

To get a substring of a string in Python, we can use the MyString[a:b] syntax, where MyString is a string and a and b are the start and end indexes.

The substring between a and b - l will be returned.

Categories
Python Answers

How to check if a given key already exists in a dictionary with Python?

Sometimes, we want to check if a given key already exists in a dictionary with Python.

In this article, we’ll look at how to check if a given key already exists in a dictionary with Python.

How to check if a given key already exists in a dictionary with Python?

To check if a given key already exists in a dictionary with Python, we can use the in operator.

For instance, we write:

d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("has key1")

We have the d dictionary.

And we check if there’s an entry with key 'key1' by writing:

"key1" in d

Since this is True, we see 'has key1' logged.

Conclusion

To check if a given key already exists in a dictionary with Python, we can use the in operator.