Found 10406 Articles for Python

How to print current date and time using Python?

Abhinanda Shri
Updated on 05-Mar-2020 10:31:00

6K+ Views

You can get the current date and time using multiple ways. The easiest way is to use the datetime module. It has a function, now, that gives the current date and time. exampleimport datetime now = datetime.datetime.now() print("Current date and time: ") print(str(now))OutputThis will give the output −2017-12-29 11:24:48.042720You can also get the formatted date and time using strftime function. It accepts a format string that you can use to get your desired output. Following are the directives supported by it.DirectiveMeaning%aLocale's abbreviated weekday name.%ALocale's full weekday name.%bLocale's abbreviated month name.%BLocale's full month name.%cLocale's appropriate date and time representation.%dDay of the month ... Read More

How can we do the basic print formatting for Python numbers?

Ankith Reddy
Updated on 17-Jun-2020 12:39:20

184 Views

You can format a floating number to the fixed width in Python using the format function on the string. For example, nums = [0.555555555555, 1, 12.0542184, 5589.6654753] for x in nums:    print("{:10.4f}".format(x))This will give the output0.5556 1.0000 12.0542 5589.6655Using the same function, you can also format integersnums = [5, 20, 500] for x in nums:    print("{:d}".format(x))This will give the output:5 20 500You can use it to provide padding as well, by specifying the number before dnums = [5, 20, 500] for x in nums:    print("{:4d}".format(x))This will give the output5 20 500The https://pyformat.info/ website is a great resource ... Read More

How to generate statistical graphs using Python?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

406 Views

Python has an amazing graph plotting library called matplotlib. It is the most popular graphing and data visualization module for Python. You can start plotting graphs using 3 lines! For example, from matplotlib import pyplot as plt # Plot to canvas plt.plot([1, 2, 3], [4, 5, 1]) #Showing what we plotted plt.show() This will create a simple graph with coordinates (1, 4), (2, 5) and (3, 1). You can Assign labels to the axes using the xlabel and ylabel functions. For example, plt.ylabel('Y axis') plt.xlabel('X axis') And also provide a title using the title ... Read More

How to generate a random 128 bit strings using Python?

Abhinaya
Updated on 05-Mar-2020 10:21:59

3K+ Views

You can generate these just random 128-bit strings using the random module's getrandbits function that accepts a number of bits as an argument. exampleimport random hash = random.getrandbits(128) print(hex(hash))OutputThis will give the output −0xa3fa6d97f4807e145b37451fc344e58c

How to find time difference using Python?

Ankith Reddy
Updated on 05-Mar-2020 10:19:02

2K+ Views

It is very easy to do date and time maths in Python using time delta objects. Whenever you want to add or subtract to a date/time, use a DateTime.datetime(), then add or subtract date time.time delta() instances. A time delta object represents a duration, the difference between two dates or times. The time delta constructor has the following function signatureDateTime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])¶Note: All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative. You can read more about it here https://docs.python.org/2/library/datetime.html#timedelta-objectsExampleAn example of using the time ... Read More

How to generate JSON output using Python?

karthikeya Boyini
Updated on 05-Mar-2020 10:16:33

2K+ Views

The json module in python allows you to dump a dict to json format directly. To use it,Exampleimport json my_dict = {    'foo': 42,    'bar': {       'baz': "Hello",       'poo': 124.2    } } my_json = json.dumps(my_dict) print(my_json)OutputThis will give the output −'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'You can also pass indent argument to prettyprint the json. exampleimport json my_dict = {    'foo': 42,    'bar': {       'baz': "Hello",       'poo': 124.2    } } my_json = json.dumps(my_dict, indent=2) print(my_json)OutputThis will give the output −{    "foo": 42,    "bar":    {       "baz": "Hello",       "poo": 124.2    } }

How to generate XML using Python?

Abhinaya
Updated on 05-Mar-2020 10:14:32

2K+ Views

To generate XML from a python dictionary, you need to install the dicttoxml package. You can install it using −$ pip install dicttoxmlOnce installed, you can use the dicttoxml method to create the xml. examplea = {    'foo': 45,    'bar': {       'baz': "Hello"    } } xml = dicttoxml.dicttoxml(a) print(xml)OutputThis will give the output −b'45Hello'You can also prettyprint this output using the toprettyxml method. examplefrom xml.dom.minidom import parseString a = {    'foo': 45,    'bar': {       'baz': "Hello"    } } xml = dicttoxml.dicttoxml(a) dom = parseString(xml) print(dom.toprettyxml())OutputThis will give the output −    45           Hello    

How to generate a 24bit hash using Python?

George John
Updated on 05-Mar-2020 10:11:26

509 Views

A random 24 bit hash is just random 24 bits. You can generate these just using the random module. exampleimport random hash = random.getrandbits(24) print(hex(hash))OutputThis will give the output0x94fbee

How to generate sequences in Python?

SaiKrishna Tavva
Updated on 27-Feb-2025 16:38:42

2K+ Views

A sequence is a positionally ordered collection of items, where each item can be accessed using its index number. The first element's index starts at 0. We use square brackets [] with the desired index to access an element in a sequence. If the sequence contains n items, the last item is accessed using the index n-1. In Python, there are built-in sequence types such as lists, strings, tuples, ranges, and bytes. These sequence types are classified into mutable and immutable. The mutable sequence types are those whose data can be changed after creation such, as list and byte arrays. ... Read More

How to use single statement suite with Loops in Python?

karthikeya Boyini
Updated on 17-Jun-2020 12:29:42

360 Views

Similar to the if statement syntax, if your while clause consists only of a single statement, it may be placed on the same line as the while header. Here are the syntax and example of a one-line for loop:for i in range(5): print(i)This will give the output:0 1 2 3 4

Advertisements