Found 34488 Articles for Programming

How to Find Hash of File using Python?

Arjun Thakur
Updated on 05-Mar-2020 10:46:21

2K+ 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

How to Find ASCII Value of Character using Python?

Chandu yadav
Updated on 05-Mar-2020 10:39:47

134 Views

The ord function in python gives the ordinal value of a character(ASCII). You can use this function to find the ascii codes as followsExamples = "Hello" for c in s:    print(ord(c))OutputThis will give the output72 101 108 108 111

How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?

karthikeya Boyini
Updated on 05-Mar-2020 10:38:46

955 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.

How to Display the multiplication Table using Python?

Ankith Reddy
Updated on 05-Mar-2020 10:36:28

306 Views

You can create the multiplication table for any number using a simple loop. exampledef print_mul_table(num):    for i in range(1, 11):       print("{:d} X {:d} = {:d}".format(num, i, num * i)) print_mul_table(5)OutputThis will give the output5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 5 X 10 = 50

How to Find the Largest Among Three Numbers using Python?

Samual Sam
Updated on 05-Mar-2020 10:35:19

903 Views

You can create a list of the three numbers and call the max method to find the largest among them. examplemy_list = [10, 12, 3] print(max(my_list))OutputThis will give the output −12ExampleIf you want to calculate it yourself, you can create a simple function likedef max_of_three(a, b, c):    if a > b and a > c:       return a    elif b > c:       return b    else:       return c print(max_of_three(10, 12, 3))OutputThis will give the output −12

How to print current date and time using Python?

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

5K+ 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 to Solve Quadratic Equation using Python?

George John
Updated on 05-Mar-2020 10:28:41

309 Views

You can use the cmath module in order to solve Quadratic Equation using Python. This is because roots of quadratic equations might be complex in nature. If you have a quadratic equation of the form ax^2 + bx + c = 0, then,Exampleimport cmatha = 12 b = 8 c = 1 # Discriminent d = (b**2) - (4*a*c) root1 = (-b - cmath.sqrt(d)) / (2 * a) root2 = (-b + cmath.sqrt(d)) / (2 * a) print(root1) print(root2)OutputThis will give the output(-0.5+0j) (-0.16666666666666666+0j)

How to Calculate the Area of a Triangle using Python?

Lakshmi Srinivas
Updated on 17-Jun-2020 12:42:15

1K+ Views

Calculating the area of a triangle is a formula that you can easily implement in python. If you have the base and height of the triangle, you can use the following code to get the area of the triangle,def get_area(base, height):    return 0.5 * base * height print(get_area(10, 15))This will give the output:75If you have the sides of the triangle, you can use herons formula to get the area. For example,def get_area(a, b, c):    s = (a+b+c)/2    return (s*(s-a)*(s-b)*(s-c)) ** 0.5 print(get_area(10, 15, 10))This will give the output:49.607837082461074

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

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

147 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

302 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

Advertisements