## Why ‘1’+’2′ = ’12’ in Python

In Python, ‘1’ + ‘2’ is equal to ’12’. This is because both ‘1’ and ‘2’ are strings so the + operation adds one string to the end of the other. This is called string concatenation. If you used integers instead, the result would be 3. What Is String Concatenation in Python String concatenation in …

## ‘import’ vs ‘from import’ in Python — What’s The Difference?

The difference between import and from import in Python is: import imports an entire code library. from import imports a specific member or members of the library. For example, let’s import the datetime module as a whole: In comparison, let’s only import the date class from the datetime module: Now you can directly call the …

## Python How to Calculate Age from Birthdate

To calculate the age from a birthdate in Python, use this function: For example: Output: If you have a hard time understanding how the code works, please read along. The Problem with Leap Years A year has ~365.25 days. But a calendar year is 365 days in length. To even this out, every fourth year …

## Python Program to Calculate the Area of a Circle

To calculate the area of a circle in Python, here’s the code: You can call this function by giving it the radius of the circle, for example: Output: How to Calculate the Area of a Circle in Maths To calculate the area of the circle given the radius of the circle, use the following equation. …

## Pythagorean Theorem with Python

To calculate the length of a hypotenuse of a right triangle using Pythagorean theorem: Here is an example. Given sides a = 3 and b = 4 in a right triangle, what is the length of the hypotenuse? The solution: That’s it for the theory. Next, let’s write a small program in Python to calculate …

## Python issuperset() Method

To check if a set A is a superset of set B in Python, use the issuperset() method. For instance: Result: You can also use the built-in comparison operators to check for supersets in Python. For instance, to check if set A is a superset of B, use the less than or equal >= operator: …

## Python issubset() Method

To check if set A is a subset of set B in Python, use the built-in issubset() method. For example: Output: Alternatively, you can use comparison operators to do check if a set is a subset of another. For example, to check if set A is a subset of B, use <= operator: Output: If …

## Python Iterables, Iterators & Generators: A Complete Guide [10+ Examples]

In Python, an iterable object is any object you can loop through using a for loop or while loop. Common examples of iterables are: Lists Strings Dictionaries Tuples Sets For example, you can loop through a list of numbers: Output: You can use a similar approach to loop through the characters of a string: Output: …

## How to Convert Space-Separated String to List in Python

To convert a space-separated string to a list in Python, call the str.split() method: Output: This works because the str.split() splits the string by blank spaces by default. Let’s then take a look at how to split a string of integers into a list of integers. How to Convert Space-Separated String of Integers to a …

## How to Convert Comma-Delimited String to a List in Python

To convert comma-delimited string to a list in Python, split the string with the str.split() method. For example: Output: This results in a list of the parts of the strings that were separated by commas. Don’t Forget to Handle Empty Strings Using the above approach on an empty string returns a list with one empty …