Category Archives: Library

25. Modules

What is a Module? Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. Create a Module To create a module just save the code you want in a file with the file extension .py: Example Save this code in a… Read More »

24. Scope

A variable is only available from inside the region it is created. This is called scope. Local Scope A variable created inside a function belongs to the local scope of that function, and can only be used inside that function. Example A variable created inside a function is available inside that function:def myfunc():  x = 300  print(x)myfunc() Output:300 Function Inside Function… Read More »

23. Iterators

Python Iterators An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__(). Iterator vs Iterable Lists,… Read More »

22. Inheritance

Python Inheritance Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class. Create a Parent Class Any class can be a parent class, so… Read More »

21. Classes and Objects

Python Classes/Objects Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a “blueprint” for creating objects. Create a Class To create a class, use the keyword class: Example Create a class named MyClass, with a property named x:class MyClass: … Read More »

20. Arrays

Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. Arrays Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library. Arrays are used to store multiple values in one single variable: Example Create an… Read More »

19. Lambda

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. Syntax lambda arguments : expression The expression is executed and the result is returned: Example Add 10 to argument a, and return the result:x = lambda a : a + 10print(x(5)) Output:15 Lambda functions can take any number of… Read More »

18. Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Creating a Function In Python a function is defined using the def keyword: Example def my_function():  print(“Hello from a function”) Calling a Function To call a function,… Read More »

17. For Loops

Python For Loops A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set… Read More »

16. While Loops

Python Loops Python has two primitive loop commands: while loops for loops The while Loop With the while loop we can execute a set of statements as long as a condition is true. Example Print i as long as i is less than 6:i = 1while i < 6:  print(i)  i += 1 Output:12345 Note: remember to increment i, or else the loop will continue forever.… Read More »