The Python Game Book

code games. learn Python.

User Tools

Site Tools


Sidebar

Github:

en:glossary:w:what_is

what is...

Important python concepts

callable

callable: a function or some object than can be called (function call)

argument

argument: parameter is what is declared in the function, while an argument is what is passed through when calling the function

parameter

parameter: parameter is what is declared in the function, while an argument is what is passed through when calling the function

comprehension

comprehension: basically an in-line for…loop that creates an data container.

  • list comprehension: usually the fastest but demand much memory

>>>a = [x**2 for x in range(5) ]
>>>a
[0, 1, 4, 9, 16]
>>> type(a)
<class 'list'>

  • list comprehension with filter:

>>>a = [x**2 for x in range(20) if x%2==0]
>>>a
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]

  • set comprehension

>>>a={x**2 for x in range(5)}
>>>a
{0,1,4,9,16}
>>>type(a)
<class 'set'>

  • dictionary comprehension

>>>a = {x:x**2 for x in range(5)}
>>>a
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>>type(a)
<class 'dict'>

  • generator expression/comprehension: Generators use minimal memory

>>>g = (x**2 for x in range(5))
>>>type(g)
<class 'generator'>
>>>for element in g:
        print(element)
        
0
1
4
9
16
>>>

  • tuple comprehension does not exist in python. You can create use the function tuple on a list comprehension or generator:

>>> # tuple from list comprehension
>>> a = tuple([x**2 for x in range(5)])
>>> a
(0, 1, 4, 9, 16)
>>> type(a)
<class 'tuple'>
>>> # tuple from generator
>>> b = tuple((x**2 for x in range(5)))
>>> b
(0, 1, 4, 9, 16)
>>> type(b)
<class 'tuple'>

sequence

  • sequence: strings, list, tuples

iterable

  • iterable: ? a list or other sequence that can be iterated over with a for loop. See also __iter__

data structure

  • data structure: ? tuple, list, dict, set

mapping

  • mapping: dicts and sets

mutable

  • mutable:
    • a list or other data structure where items can be changed. A tuple is NOT mutable. like:
      • list → list should be used for items of the same type
      • set
      • dict

immutable

  • immutable:
    • data structure where items can NOT be changed. like:
      • tuple → tuples should be used for items of different type
      • bool
      • int
      • float
      • str

hashable

  • hashable : “A hashable object has a hash value that never changes during its lifetime (see __hash__), and it can be compared to other objects (see __eq__). Hashable objects that compare as equal must have the same hash value.” (from https://realpython.com/python-data-structures/)

decorator

a function that 'decorates' another function. Can in python coded using the @ character. examples: to decorate a method as a classmethod or staticmethod, write the decorator line above the method.

class Person:
   @classmethod
   def show_all_persons(cls):
       print("...")
   
   def __init__(self):
       print("...")

generator

  • generator: A function that does not (just) end with an return statement but instead has an yield statement. Yield returns a value to the function calling code, but the function “stays alive sleeping” → it remembers all it local variables and scope. The next time the function is called, it continues after the yield statement. Generators are good for processing large data, like files

closure

  • closure: a function defined inside a function
en/glossary/w/what_is.txt · Last modified: 2020/12/17 06:14 by horst