Python Print Hex Without ‘0x’

Problem Formulation If you print a hexadecimal number, Python uses the prefix ‘0x’ to indicate that it’s a number in the hexadecimal system and not in the decimal system like normal integers. However, if you already know that the output numbers are hexadecimal, you don’t necessarily need the ‘0x’ prefix. How to print hex numbers … Read more

Python Print Float Without Decimal Points

Problem Formulation Given a floating point number x = 123.456. How to print the number without decimal remainder? Solution 1: int() Python’s built-in function int(x) converts any float number x to an integer by truncating it towards 0. For example, positive int(1.999) will be truncated to 1 and negative int(-1.999) to -1. To boost your … Read more

How to Sum the Digits of a Number in Python?

Problem Formulation Given an integer number. How to sum over all digits of this number to compute the crossfoot (cross total)? Consider the following examples: 12 –> 1+2 = 3 123 –> 1+2+3 = 3 244 –> 2+4+4 = 10 981223 –> 9+8+1+2+2+3 = 25 Method 1: sum() with Generator Expression The Python built-in sum(iterable) … Read more

How to Display a 1D and 2D Multiplication Table in Python?

Python Multiplication Table For Loop To calculate the multiplication table for a given number, iterate over all values i=0, 1, …, limit in a for loop and use the following statement as a loop body: print(number, ‘x’, i, ‘=’, number * i). This prints all equations, line by line, in the form i x j … Read more

Python Subtraction Operator

Python provides the subtraction operator – to subtract one object from another. The semantics of the subtraction depends on the operands’ data types. For example, subtracting two integers performs the arithmetic difference operation whereas subtracting two sets performs the set difference operation. The specific return value of the minus operator is defined in a data … Read more

Python Program to Add Two Numbers

Add Two Integers The most basic Python program to add two integer numbers stored in variables num_1 and num_2 is the expression num_1 + num_2 using the addition operator. The following code shows how to add two numbers 20 and 22 and print the result 42 to the shell: Add Two Integers with User Input … Read more

How to Divide Two Integers in Python 2 and 3?

In this article, you’ll learn about the division operators // and / in Python 2 and 3. You can check out the version in your Python script as shown here. A short visual overview of the division operator in Python 2 and 3: Assuming two integer values stored in variables a and b, there are … Read more

How to Get All Divisors of a Number in Python?

Problem Formulation Given an integer number n. Get all divisors c of the number n so that c * i = n for another integer i. The desired output format is a list of integers (divisors). Here are a couple of examples: n = 10 # Output: [1, 2, 5, 10] n = 13 # … Read more

How To Plot SKLearn Confusion Matrix With Labels?

Summary: The best way to plot a Confusion Matrix with labels, is to use the ConfusionMatrixDisplay object from the sklearn.metrics module. Another simple and elegant way is to use the seaborn.heatmap() function. Note: All the solutions provided below have been verified using Python 3.9.0b5. Problem Formulation Imagine the following lists of Actual and Predicted values … Read more

NumPy Tutorial – Everything You Need to Know to Get Started

This tutorial gives you a simple introduction to Python’s NumPy library. You don’t need any prerequisites to follow the tutorial. My goal was to give a practical and fun NumPy introduction for absolute beginners and with many examples. By reading through this tutorial, you will gain a basic understanding of the most important NumPy functionality. … Read more

How to Convert Hex String to Integer in Python

Problem Formulation Given a string in hexadecimal form: How to convert the hex string to an integer in Python? For example, you want to convert the hexadecimal string ‘0xff’ to the decimal integer 255. Here are a few other examples: 0x0 –> 0 0x4 –> 4 0x8 –> 8 0x12 –> 18 0x16 –> 22 … Read more

What are the Applications of Graphs in Computer Science?

[Reading time: 9 minutes] Graphs are everywhere. They are used in social networks, the world wide web, biological networks, semantic web, product recommendation engines, mapping services, blockchains, and Bitcoin flow analyses. Furthermore, they’re used to define the flow of computation of software programs, to represent communication networks in distributed systems, and to represent data relationships … Read more