Python Important Tips and TricksFor all the Python developers or any other language developers, it is always fascinating to know about the tricks and tips of the programming language that we are learning. As we all know that Python is one of the most preferred programming languages among developers. Therefore, in this tutorial, we bring the essential tips and tricks in Python that every Python developer should know. 10 Essential Tips and Tricks for PythonThe tricks or tips of Python that we are going to discuss here will save us from writing many lines of code and will save a lot of time. These Python tips are tricks that will also help us to bring up our coding game and boost us while competing with other programmers. Here is the list of such ten essential Python tips and tricks that every Python developer should know: 1. Creating a single string from elements of a list:We can simply create a single string from all the elements of a given list by using “.” with the join() function inside the print statement with the list variable. So, by this, we can easily get the single string data format from the multiple data elements given in list format. Example: Output: Hello Python Developers! Welcome to JavaTpoint 2. Python use of Enums:In Python, we can simply use the Enums to check the number of occurrences of a variable inside the given function where it first occurred. We just have to use the word with the function name inside the print statement with the “.” operator to print the number of the first occurrence of that variable inside the function. Example: Output: Occurrence of JavaTpoint: 5 Occurrence of Hello: 0 Occurrence of Python: 8 Occurrence of Welcome: 3 3. Printing path of the imported module:If we need to print the file directory or path for the Python modules we have imported in the programs, then we just have to use the module name inside the print statement simply, and the file directory will be printed in the output. Example: Looking at the following Python program: Output: <module 'socket' from 'C://Users//Manish//lib//socket.py'> <module 'numpy' from 'C://Users//Manish//lib//site-packages//numpy//__init__.py'> <module 'os' from 'C://Users//Manish//lib//os.py'> 4. Printing the most occurred element of a list:We have given a list with a number of elements in it, and there are many elements in it that are repeated more than one time. Now, if we want to print the number of elements that are occurred most in the list and it is the same as finding mode in statistics from the given data of numbers. We use max() and count function to get the result of the most occurred element. Example: Output: Most occurred element in the given list: 29 5. Printing given string number of times:We can print a given string ‘n’ number of times in the output simply by using ‘String Name * n’ syntax into the print statement. It will print the given string n number of times in continuation in output. Example: Output: Given string for n number of times: Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers! 6. wapping of two variable numbers:We can also do in-place swapping of two variables numbers so we can use their swapped values in the program. Example: Look at the following Python program: Output: m before swapping: 24 n before swapping: 26 m after swapping: 26 n after swapping: 24 7. Using chain of comparison operators:We can use the chain of comparison operator to compare the given variable number with multiple values in a single comparison. Example: Output: True False 8. Reversing a given string: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: Given string in program: Welcome to JavaTpoint Python Developers! Reverse of Given string in program is: !srepoleveD nohtyP tniopTavaJ to emocleW 9. eturning multiple values from a single function: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: 24 25 31 43 37 29 39 23 10. Checking for anagram words: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: Words are anagrams: True Words are anagrams: False b. By importing an external module in program: Output: Words are anagrams: True Words are anagrams: False |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263462.html