Tuesday 3 March 2020

Python Inheritance

Python Inheritance

If we have a basic Student class which we want to modify to create a University Student object, we can create a new class that incorporates the methods and variables in the Student class but adds new features such as the name of the University. 

Student class is referred o as the parent class while University Student class is called the child class. We say that UniversityClass is inheriting from Student class.

The Unified Modeling Language (UML) is a set of diagrams that allows you to represent many types of relationship. One type of UML diagram is called a Class Diagram and is used to represent relationships between classes. An example is shown:


To specify the visibility of a class method use the following prefixes:
+Public
-Private
#Protected
~Package
Different types of arrows have the following meanings:
The one in the example diagram above means that UniversityStudent inherits from Student. All non-private methods in Student can be directly accessed in UniversityStudent. If the method in UniversityStudent has the same name as that in Student, we call this situation, method overriding. 




Python Constructors (def __init__)

Python Constructors (def __init__)

In Object-Oriented Programing, a constructor is used to initialise object variables when we create a new object based on a given class. If we had a class called  Student with variables Name, Phone, Email, each time we create a new Student object, we need to supply the values of Name, Phone and Email.

In Python, we would create such a class as follows:

class Student:
    def __init__(self, name, phone, email):
        self.name = name
        self.phone= phone
        self.email= email
        # return statement removed from here
We can create a Student object as follows:

s1=Student("Genghis Khan", "+123456789","Genghis.Khan@gmail.com")
We can acess the object variables of the Student called s1 as follows:
print(s1.name)
print(s1.phone)
print(s1.email)
In IDLE, the program looks as follows:
Output:
The keyword "self" refers to the object we have created. 
Sometimes we have default values for the constructor variables. 



Friday 21 February 2020

Python Tools for Science and Engineering

Python Tools for Science and Engineering 

The following are selected Python tools for science and engineering 

Web App Development

  1. Flask - for building web apps - install - resources
  2. Django - web framework (Google) - install - resources

Engineering and Science

  1. SciPy - packages for mathematics, science, and engineering.
  2. Pandas -  data analysis and modelling library. Tutorial.
  3. Numpy - Numerical computing and analysis.
  4. NLP Natural Language Processing library, together with sub-libraries, used to build applications and services that can understand and analyse human languages and data.
  5. Matplotlib - create charts and visualizations, see also Seaborn

Computing


  1. SQLAlchemy - managing SQL databases
  2. TensorFlow  - Artificial Intelligence applications
  3. Keras - neural networks and machine learning
Data Science libraries

How to Run Python Code From Notepad++

How to Run Python Code From Notepad++

There are number of ways to run Python code from within Notepad++. I found the following to be the most elegant and it works:

1. Open a new file Notepad++ and enter a simple Python statement such as: print("Hello World!")




2. Run the program by pressing F5 in Notepad++. You will get the following dialogue box:
    Enter the following in the input box provided: py -3 -i "$(FULL_CURRENT_PATH)"


3. Click Save and provide an appropriate name such as "Python38_Run"

4. Press Run to run the program. You should see a Python shell and "Hello World!"



Source: https://stackoverflow.com/questions/1702586/how-to-execute-a-python-file-in-notepad

Thursday 6 February 2020

Python Resources

Python Resources

Python Official Website  and version 3.8.1 tutorial
Python documentation

Useful Tutorials

1. W3schools:  https://www.w3schools.com/python/default.asp

2. CodeAcademy:  https://www.codecademy.com/learn/learn-python

Learn Python App by Programiz (also available for IOS)




Why Python?

1. https://www.linkedin.com/pulse/why-engineers-learning-python-koen-van-viegen

Why Another Python Blog?

Why Another Python Blog?

Because I have started teaching Python and need all my ideas and resources in one place!

The focus will be Python for engineers and scientists.

I will post interesting Python resources such as tutorial sites, description of useful Python libraries, Youtube playlists, books, interesting applications and so on. 

Welcome and enjoy.