Found 10784 Articles for Python

Why doesn’t list.sort() return the sorted list in Python?

AmitDiwan
Updated on 20-Sep-2022 10:53:24

356 Views

Example In this example, let’s first see the usage of list.sort() before moving further. Here, we have created a List and sorted in Ascending order using the sort() method − # Creating a List myList = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ", myList) # Sort the Lists in Ascending Order myList.sort() # Display the sorted List print("Sort (Ascending Order) = ", myList) Output List = ['Jacob', 'Harry', 'Mark', 'Anthony'] Sort (Ascending Order) = ['Anthony', 'Harry', 'Jacob', 'Mark'] Where performance matters more, making a copy of the list just ... Read More

How to print a calendar for a month in Python

Md Waqar Tabish
Updated on 20-Sep-2022 07:14:28

6K+ Views

Introduction Your helpful garden snake language python has you covered if you want to put up a personal calendar or even practise your daily coding challenge. How so? The Calendar module in Python is a built-in module that you may use to execute date, month, and calendar-related actions and modify your code for a particular day or month. Calendar module The idealised calendar, which is the current Gregorian calendar, is used by the Python Calendar module. It continues endlessly in both the past and the future. These calendars designate Monday as the start of the week and Sunday ... Read More

How to match whitespace in python using regular expressions

Md Waqar Tabish
Updated on 20-Sep-2022 07:09:49

15K+ Views

Regular expressions, often known as RegEx, are a string of characters corresponding to letters, words, or character patterns in text strings. It implies that you may use regular expressions to match and retrieve any string pattern from the text. Search and replace procedures benefit from the usage of regular expressions. The most common application is searching for a substring that matches a pattern and substituting something else. What are whitespace characters? "Whitespace" refers to any letter or set of characters representing either horizontal or vertical space. Using regular expressions, the metacharacter “\s” matches whitespace characters in python. Algorithm ... Read More

How do we find the exact positions of each match in Python's regular expression?

Md Waqar Tabish
Updated on 20-Sep-2022 07:07:24

1K+ Views

Introduction The re-module is what we use in Python for regular expressions. Text searches and more complex text manipulation employ regular expressions. Tools like grep and sed, text editors like vi and emacs, and computer languages like Tcl, Perl, and Python all have built-in regular expression support. The re-module in Python offers functions for matching regular expressions. A regular expression that defines the text we are looking for or modifying is called a pattern. Text literals and metacharacters make up this string. The compile function is used to create the pattern. Raw strings are advised since regular expressions ... Read More

Why must dictionary keys be immutable in Python?

AmitDiwan
Updated on 19-Sep-2022 14:25:30

1K+ Views

To understand why dictionary keys must be immutable. Let us related it to hash table. The hash table implementation of dictionaries uses a hash value calculated from the key value to find the key. If let’s say the key was a mutable object, its value could change, and thus its hash could also change. As stated above, since whoever changes the key object can’t tell that it was being used as a dictionary key, it can’t move the entry around in the dictionary. Then, When you try to look up the same object in the dictionary it won’t be ... Read More

Why isn’t all memory freed when CPython exits?

AmitDiwan
Updated on 19-Sep-2022 14:16:22

492 Views

CPython is the default and most widely used interpreter or implementation of Python. It is the original Python version and understands the code written using python specifications. Python is quite serious about cleaning up memory on exit and does try to destroy every single object, but unfortunately objects referenced from the global namespaces of Python modules are not always deallocated when Python exits. The reason are circular references. There are also certain bits of memory that are allocated by the C library that are impossible to free. Use the atexit module to force Python to delete certain things on deallocation. ... Read More

Why can’t Python lambda expressions contain statements?

AmitDiwan
Updated on 19-Sep-2022 14:15:57

1K+ Views

Yes, Python Lambda Expressions cannot contain statements. Before deep diving the reason, let us understand what is a Lambda, its expressions, and statements. The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax − lambda arguments: expressions The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression. Lambda Example Let us see an example − myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr) Output Thisisit! Sort a List by values from ... Read More

How do I write a function with output parameters (call by reference) in Python?

AmitDiwan
Updated on 19-Sep-2022 14:15:33

4K+ Views

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. Achieve this in the following ways − Return a Tuple of the Results Example In this example, we will return a tuple of the outcome − # Function Definition def demo(val1, val2): val1 = 'new value' val2 = val2 + 1 return val1, val2 x, y = 'old value', 5 # Function call print(demo(x, y)) ... Read More

How do I program using threads in Python

AmitDiwan
Updated on 19-Sep-2022 13:49:08

188 Views

Threads are sometimes called light-weight processes and they do not require much memory overhead; they are cheaper than processes. A thread has a beginning, an execution sequence, and a conclusion. There are two modules which support the usage of threads in Python3 − _thread − Deprecated in Python 3 Threading − Introduced in Python 2.4 The threading Module The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module. The threading module exposes all the methods of the thread module and provides some additional methods − threading.activeCount() ... Read More

What’s up with the comma operator’s precedence in Python?

AmitDiwan
Updated on 19-Sep-2022 13:47:49

545 Views

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. The comma is not an operator in Python; therefore, the precedence concept doesn’t work here. Before moving further, let us first see the precedence of operators in Python from highest precedence to lowest. S.No. Operator & Desc 1 ** Exponentiation (raise to the power) 2 ~ + - Complement, unary plus and minus (method names for the last two are +@ and -@) 3 * / % // Multiply, divide, modulo and floor division ... Read More

Advertisements