Posts

Showing posts with the label Python

Python 101 : Basic formatting

Image
Let's look at some formatting examples: Below is another example: We could also do it as follows - {0} : first argument, {1} : second argument -: Another way, without numbering the arguments: The below example uses a " float " number: Using named arguments - " var " in our case - : Using a " C-like " formatting: %s : String. %d : Decimal notation.

Python 101 : Generators and Lists

Image
A list is a collection of val ues stored in memory. A generator does not build a list, it only tells us how to generate these values with nothing stored in memory. Our examples below use the " range() " iterator and they produce the same results. Below is our list example: And our generator example: The generator expression generate values only when they are needed which saves us system resources, especially with huge amounts of data.

Python 101 : Naming style - Underscores and double underscores -

Image
Names that start and end with two underscores - __var__ - are internal names used by python like - __init__ , __str__ -. Names that start with an underscore - _var - are not imported into other programs using the - from ... import ... - instruction. They are meant to be used within the program as " internals ": Names that start with two underscores - __var - inside a class get prefixed with the class name: The " __var " variable becomes " _A__var ". The name that is represented by single underscore - _ - is used by python to store the result of the last result, for example to go through a loop where the index is of no use to us: Names with a trailing underscore - var_ - are usually used so the name doesn't get mixed up with a python keyword.

Python 101 : Dynamically typed variables

Image
Data in Python is built around the concept of object. In Python , if we write the below instruction: We are defining a pointer named " x " that points to a memory location that contains the value " 2 ". Python variables are just pointer to different objects, so we don't need to declare the them at the beginning of our programs. A variable could point to different types of objects as we can see below: Example: The instruction " b=a " makes the variable " a " and " b " point to the same location. If we change the value of " b " using the instruction " b=2 ", " b " will point to the location of the value " 2 ", and " a " remain unchanged.

Python 101 : Default and arbitrary arguments

Image
The asterisk  " * " when used before a parameter enable a function to receive a variable number of arguments.  We usually use it when we don't know before hand the number of arguments a function will get. Example: The default argument allows us to call a function without using any arguments. If we decide to use arguments, their value will override the default value:

Python 101: Changing the behavior of the "range" function

Image
We create an custom iterator function that we will use with a " for " loop: We use it as below: The " yield " function acts as a return function but it doesn't completely  exit the function - the control is not handed back completely to the caller of the function - and the next instruction in the function is run: