What is the purpose of the return statement?

The print() function writes, i.e., “prints”, a string in the console. The return statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The return statement is used when a function is ready to return a value to its caller. For example, here’s … Read more

numpy array: IndexError: too many indices for array

Numpy ndarrays are meant for all elements to have the same length. In this case, your second array doesn’t contain lists of the same length, so it ends up being a 1-D array of lists, as opposed to a “proper” 2-D array. From the Numpy docs on N-dimensional arrays: An ndarray is a (usually fixed-size) multidimensional … Read more

How can I represent an ‘Enum’ in Python?

Enums have been added to Python 3.4 as described in PEP 435. It has also been backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4 on pypi. For more advanced Enum techniques try the aenum library (2.7, 3.3+, same author as enum34. Code is not perfectly compatible between py2 and py3, e.g. you’ll need … Read more

What is the use of “assert” in Python?

The assert statement exists in almost every programming language. It helps detect problems early in your program, where the cause is clear, rather than later when some other operation fails. When you do… … you’re telling the program to test that condition, and immediately trigger an error if the condition is false. In Python, it’s … Read more

Changing one character in a string

Don’t modify strings. Work with them as lists; turn them into strings only when needed. Python strings are immutable (i.e. they can’t be modified). There are a lot of reasons for this. Use lists until you have no choice, only then turn them into strings.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)