
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10400 Articles for Python

17K+ Views
You can use relational operators in python to compare numbers(both float and int) in python. These operators compare the values on either side of them and decide the relation among them. Assume variable a holds 10 and variable b holds 20, thenOperatorExample==(a == b) is not true.!=(a != b) is true.>(a > b) is not true.=(a >= b) is not true.= b) print(a

3K+ Views
You can find the hash of a file using the hashlib library. Note that file sizes can be pretty big. Its best to use a buffer to load chunks and process them to calculate the hash of the file. You can take a buffer of any size.Exampleimport sys import hashlib BUF_SIZE = 32768 # Read file in 32kb chunks md5 = hashlib.md5() sha1 = hashlib.sha1() with open('program.cpp', 'rb') as f: while True: data = f.read(BUF_SIZE) if not data: break md5.update(data) sha1.update(data) print("MD5: {0}".format(md5.hexdigest())) print("SHA1: {0}".format(sha1.hexdigest()))OutputThis will give the outputMD5: 7481a578b20afc6979148a6a5f5b408d SHA1: ... Read More

1K+ Views
Python provides straightforward functions to convert Decimal to Binary, Octal, and Hexadecimal. These functions are −Binary: bin() Octal: oct() Hexadecimal: hex()ExampleYou can use these functions as follows to get the corresponding representation −decimal = 27 print(bin(decimal),"in binary.") print(oct(decimal),"in octal.") print(hex(decimal),"in hexadecimal.")OutputThis will give the output −0b11011 in binary. 0o33 in octal. 0x1b in hexadecimal.

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

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

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

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

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

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 } }