The Mutable Default Argument In Python

Summary: Passing mutable objects as default arguments leads to unexpected outputs because Python initializes the default mutable object only once, not (as you may have expected) each time the function is called. To fix this, initialize the mutable default argument with the None keyword in the argument list and then initialize it within the function. … Read more

Slice Notation – A Simple Illustrated Guide

Summary: Slicing is a Python concept to extract a subsequence from a string or list—that lies within a start and stop index range. There are two syntactical ways to define a slice. (1) The extended slice notation makes use of a colon : in string_name[start:stop:step]. (2) The slice() constructor defines the index range in string_name[slice(start:stop:step)]. … Read more

How To Pass a Variable By Reference In Python?

Summary: Variables are passed by object reference in Python. Therefore, mutable objects are passed by reference while immutable objects are passed by value in Python. To pass immutable objects by reference you can return and reassign the value to the variable, or pass it inside a dictionary/list, or create a class and use the self … Read more

How To Format A String That Contains Curly Braces In Python?

Summary: Use one of the following methods to format strings that contain curly braces: Use double curly braces {{}} Use the old string formatting, i.e. the % operator Use the JSON Library Use Template Strings Problem: Given a string literal with curly braces; how to format the string and ensure that the curly braces are … Read more

Accessing The Index Of Iterables In Python

Summary: To access the index of iterables like lists in Python, use one of the following methods: Use enumerate() function. Use a counter variable with For Loop/While Loop. Use a list comprehension. Use the NumPy library. Use the itertools module. Introduction An index can be considered as the position of an element in an ordered … Read more

Python Unicode Encode Error

Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your … Read more

String Formatting Comparison: format() | Percent | f-string

Summary: f-string is more readable and easier to implement than % and .format() string formatting styles. Furthermore, using f-strings is suggested for Python 3.6 and above while .format() is best suited for Python 2.6 and above. Versions prior to Python 2.6 only provide % option for string formatting. In terms of speed, % is the … Read more

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Problem Formulation πŸ”Problem: Given a string in Python; how to split the string and also keep the separators/delimiter? A sequence of one or more characters used to separate two or more parts of a given string or a … Read more

Replacements For Switch Statement In Python?

Summary: Since switch-case statements are not a part of Python, you can use one of the following methods to create a switch-case construct in your code : Using a Python Dictionary Creating a Custom Switch Class Using if-elif-else Conditional Statements Using a Lambda Function Problem: Given a selection control switch case scenario in Python; how … Read more

Python raw_input() vs input()

Summary: The key differences between raw_input() and input() functions are the following: raw_input() can be used only in Python 2.x and is obsolete in Python 3.x and above and has been renamed input() In Python 2.x, raw_input() returns a string whereas input() returns result of an evaluation. While in Python 3.x input() returns a string … Read more