Categories
Python Answers

How to get the current time in Python?

Sometimes, we want to get the current time in Python.

In this article, we’ll look at how to get the current time in Python.

How to get the current time in Python?

To get the current time in Python, we can use the datetime.datetime.now method.

For instance, we write:

import datetime
print(datetime.datetime.now())

Then we print the current date and time.

Conclusion

To get the current time in Python, we can use the datetime.datetime.now method.

Categories
Python Answers

How to sort a Python dictionary by value?

Sometimes, we want to sort a Python dictionary by value.

In this article, we’ll look at how to sort a Python dictionary by value.

How to sort a Python dictionary by value?

To sort a Python dictionary by value, we can use the call sorted on the dictionary items.

For instance, we write:

x = {'a': 2, 'b': 4, 'c': 3, 'd': 1, 'e': 0}
sorted_dict = { k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
print(sorted_dict)

to get the items from x with x.items.

Then we call sorted on it with a function that gets the value with item[1] to sort the entries by the value of each entry.

Finally, we use the comprehension syntax to put the items into a new dictionary.

Therefore, sorted_dict is {'e': 0, 'd': 1, 'a': 2, 'c': 3, 'b': 4}.

Conclusion

To sort a Python dictionary by value, we can use the call sorted on the dictionary items.

Categories
JavaScript Answers Python Answers

How to list all files of a directory with Python?

Sometimes, we want to list all files of a directory with Python.

In this article, we’ll look at how to list all files of a directory with Python.

How to list all files of a directory with Python?

To list all files of a directory with Python, we can use the os.walk method.

For instance, we write:

from os import walk

f = []
for (dirpath, dirnames, filenames) in walk('./'):
    f.extend(filenames)

print(f)

We use the walk method with the root path to list everything from the root path and down.

dirpath is the directory path.

dirname is an array of directory names.

filenames is an array of file names.

We use f.extend to put the filenames entries into f.

Conclusion

To list all files of a directory with Python, we can use the os.walk method.

Categories
Python Answers

How to check if a Python string contains a substring?

Sometimes, we want to check if a Python string contains a substring.

In this article, we’ll look at how to check if a Python string contains a substring.

How to check if a Python string contains a substring?

To check if a Python string contains a substring, we can use the in operator.

For instance, we write:

some_string = 'blah blah'
if "blah" in some_string: 
    print('has blah')

We use the in operator to check if 'blah' is in some_string.

This should return True since it’s in some_string.

Therefore, 'has blah' is logged.

Conclusion

To check if a Python string contains a substring, we can use the in operator.

Categories
Python Answers

How to iterate over dictionaries using for loops with Python?

Sometimes, we want to iterate over dictionaries using for loops with Python.

In this article, we’ll look at how to iterate over dictionaries using for loops with Python.

How to iterate over dictionaries using for loops with Python?

To iterate over dictionaries using for loops with Python, we can use the items method.

For instance, we write:

d = {'a': 1, 'b': 2, 'c': 3}
for key, value in d.items():
    print(key, value)

to call the d.items to return an iterable with key and value of each entry of the d dictionary.

In the loop body, we print both with print.

Therefore, we get:

a 1
b 2
c 3

as a result.

Conclusion

To iterate over dictionaries using for loops with Python, we can use the items method.