Powersets in Python using itertools

itertools functions return iterators, objects that produce results lazily, on demand. You could either loop over the object with a for loop, or turn the result into a list by calling list() on it: from itertools import chain, combinations def powerset(iterable): “powerset([1,2,3]) –> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)” s = list(iterable) return … Read more

Why are str.count(”) and len(str) giving different outputs when used on an empty string?

str.count() counts non-overlapping occurrences of the substring: Return the number of non-overlapping occurrences of substring sub. There is exactly one such place where the substring ” occurs in the string ”: right at the start. So the count should return 1. Generally speaking, the empty string will match at all positions in a given string, … Read more

How to move a sprite according to an angle in Pygame

you just need a little basic trig def calculat_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy.X + (speed*math.cos(angle_in_radians)) new_y = old_xy.Y + (speed*math.sin(angle_in_radians)) return new_x, new_y — edit — Here is your code from above edited to work import pygame, math, time screen=pygame.display.set_mode((320,240)) clock=pygame.time.Clock() pygame.init() def calculate_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy[0] + (speed*math.cos(angle_in_radians)) new_y = old_xy[1] + (speed*math.sin(angle_in_radians)) return new_x, … Read more

Floor division with negative number

The // operator explicitly floors the result. Quoting the Binary arithmetic operations documentation: the result is that of mathematical division with the ‘floor’ function applied to the result. Flooring is not the same thing as rounding to 0; flooring always moves to the lower integer value. See the math.floor() function: Return the floor of x, … Read more

Reading a file using a relative path in a Python project

Relative paths are relative to current working directory. If you do not want your path to be relative, it must be absolute. But there is an often used trick to build an absolute path from current script: use its __file__ special attribute: from pathlib import Path path = Path(__file__).parent / “../data/test.csv” with path.open() as f: … Read more

What does the slash mean when help() is listing method signatures?

It signifies the end of the positional only parameters, parameters you cannot use as keyword parameters. Before Python 3.8, such parameters could only be specified in the C API. It means the key argument to __contains__ can only be passed in by position (range(5).__contains__(3)), not as a keyword argument (range(5).__contains__(key=3)), something you can do with … Read more

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