Uncategorized

  • Generator Expressions

    We’ve seen list, set and dictionary comprehensions but we haven’t seen tuple comprehensions. That’s because there aren’t any tuple comprehensions in Python. The syntax that you might think would create one, actually creates an iterator. This is known as a generator expression. This code prints some square numbers.

    Read more

  • Yield: Creating Generator Functions

    An easier way to create an iterator in Python (easier than implementing __next__ and __iter__) is to use the yield keyword to create a generator function. Although yield may at first seem counter-intuitive, it really behaves a lot like the return keyword. The difference is, you can iterate over the yielded values. In this code…

    Read more

  • Installing Python Modules

    Modules in Python are files containing Python code which you can use in your program, by “importing” them. Packages are collections of modules. Many pre-made modules and packages are available to use in Python. You can install them using pip, the package installer for Python. It’s best to ensure you are working in an activated…

    Read more

  • Python Mixins

    The object-oriented concept of a “mixin” is used in Python, as in many programming languages (but not all). The Python concept of a mixin doesn’t quite literally conform to the Wikipedia definition of a mixin, but it’s close. Usually a subclass is a version of the superclass. In the example below, a VW literally is…

    Read more

  • Multiple Inheritance

    It’s possible for a class to inherit methods from multiple superclasses. Typically, a subclass IS a kind of the superclass. For example, if Tiger is a subclass of Cat, this is because a “Tiger” literally is a kind of “Cat“. In this example, a SmartPhone is a Phone, but it’s also a Camera. Class Hierarchies…

    Read more

  • Boolean Operators: And, Or, Not

    We can combine boolean values, or expressions like value < 3 or text == “Hello” that evaluate to a boolean value, using boolean operators. and and or are binary operators: the accept two arguments and return a boolean value. not is a unary operator. It accepts only one argument and returns a boolean value. Boolean…

    Read more

Blog at WordPress.com.