Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler? In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more and way like a pro. readable efficient Let’s get started Swapping value in Python Instead of creating a variable to hold the value of the one while , you can do this instead temporary swapping >>> FirstName = >>> LastName = >>> FirstName, LastName = LastName, FirstName >>> print(FirstName, LastName) ( , ) "kalebu" "Jordan" 'Jordan' 'kalebu' Create a single string from all elements in the list Instead of Iterating each element over the list using for loop and appending each element to the string, you can do this instead. >>> python = [ , , , ] >>> python_string = .join(python) >>> print(python_string) Python Syntax is elegantly "Python" "Syntax" "is" "elegantly" ' ' Merging two dictionaries Use to combine two sets of update( ) dictionaries a = { : , : } b = { : , : } a.update(b) print(a) { : , : , : , : } >>> "name" "Python" "creator" "Guido" >>> "age" 30 "gender" None >>> >>> 'gender' None 'age' 30 'name' 'Python' 'creator' 'Guido' List comprehension to create a specific list Use List Comprehension instead of for loop on a case where you want to create a list from a sequence with certain criteria. You’re going to specify the criteria. For instance in the example below we have created a list of odd numbers in just one line of code (one-liner). >>> odd = [x x range( ) x% != ] >>> print(odd) [ , , , , , , , , , ] for in 20 if 2 0 1 3 5 7 9 11 13 15 17 19 Reversing a string in Python You can easily your string with just a bit of slicing as shown below. reverse >>> quote = >>> print(quote[:: ]) God is good "doog si doG" -1 Unpacking a Python List Instead of or reassigning each variable to index in a list, you do this at once by unpacking them as shown in the code below. names = [ , , ] niece, friend, crush = names print( .format(niece, friend, crush)) Melisa Lisa Peace >>> 'Melisa' 'Lisa' 'Peace' >>> >>> '{} {} {}' Iterating over two python list You can actually Iterate over two lists at once without using nested for a loop as shown below >>> books = [ , , ] >>> pages = [ , , ] >>> book ,page zip(books, pages): ... print( .format(book, page)) ... mabala---> money heist---> GOT---> 'mabala' 'money heist' 'GOT' 200 300 600 for in "{}--->{}" 200 300 600 Using all or any in a conditional statement Instead of writing many if-else statements for your logic, you can actually do this using any and all keywords. all will evaluate to True only if all elements are true while any will evaluate to True if any of the multiple conditions evaluate to True. >>> data = [True, True, False] >>> any(data): ... print( ) ... at least >>> not all(data): ... print( ) ... That if "at least" if "Thats right" 's right Cumulative summing a sequence of numbers In case you want the cumulative sum of numbers to do some plotting, you can easily do this using numpy using its function cumsum () >>> numpy np >>> factors = list(np.cumsum(range( , , ))) >>> print(factors) [ , , , , , , ] import as 0 20 3 0 3 9 18 30 45 63 Adding Index to Iterables(list, tuples, etc..) Do this to automatically index your elements within an Iterable >>> students = [ , , ] >>> student_with_index = list(enumerate(students)) >>> print(student_with_index) [( , ), ( , ), ( , )] 'Immaculate' 'Prisca' 'Lizy' 0 'Immaculate' 1 'Prisca' 2 'Lizy' import this (Python design principles) One you do this, You will be surprised by seeing Zen of Writing Python code pop up in front of you this import ''' The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!''' Hope you find this post interesting, Now share the with your fellow peers that you have made it, . Tweet now The Original Article can be found on kalebujordan.com