You can apply multiple assignments to swap any two variables in a concise and elegant manner, without introducing the third one:
Ordinary multiple assignments are not all Python can do. You don’t need the same number of elements on the left and right sides: In this case, x takes the first value (2) because it comes first. z is the last and takes the last value (8). y takes all other values packed in a list because it has the asterisk (*y).
You can assign multiple variables in a single statement using tuple unpacking: Please, note that 2, 4, 8 in the first statement is a tuple equivalent to (2, 4, 8).
You can merge multiple comparisons to a single Python expression by chaining the comparison operators. This expression returns True if all comparisons are correct or False otherwise: This is similar to (2 < x) and (x ≤ 8) and (6 < x) and (x ≤ 8), but more compact and requires x to be evaluated only once. This is also legal: You can chain more than two comparisons:
If you need multiple variables to reference the same object, you can use the chained assignment: Logical and elegant, right?
The Zen of Python also known as PEP 20 is a small text by Tim Peters that represents the guiding principles to design and use Python. It can be found on Python Web site, but you can also get it in your terminal (console) or Jupyter notebook with a single statement:
Anagram words are the words in which all the letters are the same in two different words, but the letters of words are arranged in a different order within the word. We can check that if the given two words are pair of anagram words or not. We can perform this action of checking anagram words, by using the following two methods: a. Without importing an external module in program: Look at the following example Python program: Output: b. By importing an external module in program: Output:
We can print the multiple values or elements from a given single function simply by using only one print statement. It will save a lot of time for us from writing multiple lines of code in the program. Example: Output:
Sometimes, we have a given string variable, and we may have to print or use the reverse order of that string. Therefore, we should know the easiest way to print the reverse format of a given string. Example: Look at the following Python program: Output:
We can use the chain of comparison operator to compare the given variable number with multiple values in a single comparison. Example: Output: