Knowledge7

The Linux and Mobile Solution Provider

  • About
  • Training
  • Services
  • Clients
  • In the news
  • Blog
  • Contact Us
You are here: Home / Archives for Avinash Meetoo

Inheritance and Object-Orientation

This topic is part of our Mastering Computer Programming training

Object-Oriented Programming, as opposed with Object-Based Programming, is when the programmers uses Inheritance between classes to implement the application. In many cases, inheritance makes the code easier to extend and maintain. Furthermore, in the real world, inheritance exists (in the sense that a Dog and a Cat are both Animals and, consequently, they share some behaviours) and, by properly using inheritance, the program becomes much clearer. Chapter 17 of the book explains how to use inheritance.

Work to do

We are now going to enhance our race simulation program by using inheritance to make it fully object-oriented.

Up to now, a race was won by the driver who has the best ability and, if there are many such drivers, the one driving the fastest car. This is artificial in the sense that it’s always the same drivers (those with maximum ability) who win. In real-life, the winner is not always the best driver:

  • Sometimes, because of luck, a lesser driver wins. This can be modeled, for example, by simulating an actual race over a distance and assuming that a car travels along a distance which is proportional to the performance of the car but biased with an element of randomness.
  • Some races might be handicap-based where those drivers with more ability start behind others. Naturally, here also it is good to have an element of randomness to make things more interesting.
This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Object-Based Programming

This topic is part of our Mastering Computer Programming training

It is important to know Object-Oriented Analysis, Design and Programming as this allows the programmer to find solutions to complex problems relatively easily.

Chapter 13 of the book explains how classes, objects (instances), attributes and methods are used in Python. Chapter 14 distinguishes between immutable classes and mutable classes. Chapter 15 examines methods, including the initialization methods (called a constructor in other programming languages). Chapter 16 is about collections of objects.

Work to do:

We are now going to develop a race simulation program. Here is a statement describing the simulation:

A race has cars and drivers. Each car has a maximum speed and each driver has his own ability. A driver is assigned to a car randomly. The driver who has the best ability wins and, if there are many such drivers, the one driving the fastest car wins.

From the above statement and using object-oriented analysis, design and programming, implement a solution to this simulation problem using Python.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Advanced Data Structures

This topic is part of our Mastering Computer Programming training

A second kind of sequence: the list

A list in Python is very similar to a string with two important differences:

  • a list can contain any type of elements (and not only characters like strings)
  • a list is mutable (unlike strings which are immutable)

Strings and lists are both called sequences or linear containers.

Lists are introduced in chapter 9 of the book. Go through the examples and make sure you properly understand:

  • creating a list
  • empty and nested lists
  • indexing, slices, the in operator
  • the range() function
  • changing and deleting elements
  • cloning a list (read about object, values and aliasing before)
  • Traversing a list
  • Converting a list of characters into a string and vice-versa

Sections 9.14 (List parameters) to 9.19 (Test-driven development) are advanced topics and can be skipped initially.

Work out exercises 1-6 and 17-18 in section 9.22.

An advanced data structure: the dictionary

Dictionaries (which are basically key-value pairs) are described in chapter 12 of the book. It is important to understand:

  • creating a dictionary (empty or non-empty)
  • adding a key-value pair
  • obtain a value given a key
  • using keys(), values(), has_key() and get()
  • using a dictionary to speed up fibonacci() (section 12.5)
  • counting letters (section 12.7)

Sections 12.4 (Sparse Matrices) and 12.8 (Robots) can be skipped on a first reading.

Work out exercises 1 and 2 in section 12.10. Can you think of a way to solve the problems without using dictionaries? Most modern programming languages have dictionaries. Can you see why?

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Using Linear Containers

This topic is part of our Mastering Computer Programming training

A string (e.g. “Hello”) is a collection of characters (unlike a boolean, an integer or a float which are scalar value). In Chapter 7 of the book, we will use strings to build progressively more complex (and more interesting) programs.

Like in Java, strings in python are immutable to increase performance and decrease space requirements.

It is important to understand the following aspects of Strings:

  • Indexing
  • The len() function
  • Traversal using an index and a for loop
  • Slices
  • The in and not in operators
  • The Python string module

String formatting is an advanced topic and is not essential at this point.

Work out all the examples in the book (except those pertaining to string formatting which can be skipped on a first reading). When you have understood everything, work out the two exercises in section 7.14.

Comment on the power of doctest and slices. For those who have prior exposure to other programming languages, what do you think of Python’s way of managing string?

Powerful strings

Work out exercises 1, 2, 5, 6 and 8 in section 7.16. How do you find is_palindrome? What about remove_all?

A lot of people say that modern programming languages like Python allow programmers to program elegantly. Do you think so?

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Looping Constructs

This topic is part of our Mastering Computer Programming training

We will now study iteration as explained in Chapter 6 of the book. In Wikipedia, iteration is defined as repeating a process with the aim of approaching a desired goal.

Python provides a number of constructs for iterations (i.e. repetitions) and one of the most useful is the while statement. The statement is used in the following fashion:

while condition:
        statement1
        statement2
        statement3
statement4

statements 1, 2 and 3 will be repeated while the condition is True. As soon as the condition becomes False, the while loop stops and statement 4 executes. In other words, this is a possible flow of execution:

statement1
statement2
statement3
statement1
statement2
statement3
statement1
statement2
statement3
statement4

Work out the exercises in section 6.3 (countdown, sequence and num_digits). Comment on the condition used for the while loop in the num_digits function. Is there something better we can use?

Looping constructs can be used to generate tabular data like a multiplication table. Work out the programs in section 6 until part 6.12. The last program can be nicely used to print out any multiplication table e.g.

Programs generally solve a problem. And the steps needed to solve a problem in a satisfactory manner is called an algorithm. Section 6.14 shows an algorithm, Newton’s method for calculating the square root of a number. Run it and make sure you understand how it works. Where does the name algorithm come from?

Work out the exercises 1-9 at the end of Chapter 06. Some of the exercises are relatively challenging but we would advise you to work hard trying to find the solutions. Do not hesitate to contact us if you are stuck.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Control Structures

This topic is part of our Mastering Computer Programming training

Like all programming languages, Python has a boolean type (with True and False being the only two possible boolean values). A comparison (e.g. 5==5, 2>3, etc.) evaluates to a boolean. Boolean can be combined (using Boolean Algebra) using the and, or or not logical operators. This is explained in Chapter 4 of the book.

People use boolean expressions in general to write conditional statements (e.g. if, if/else and variations like chained conditionals / nested conditionals).

Work out exercises 1-7 in Chapter 4 of the book. Note that question question 2 starts at the line “if x < y:” and question 5 at the line “if choice == ‘a’:”.

Type conversion

Python is a strongly typed programming language. Yet, it allows the conversion of values of one type into another. For example, int(“42”) evaluates to 42 (an integer). Python provides int(), float(), str() and bool().

Work out exercise 8.

Note that we are skipping GASP (Graphics API for Students of Python) for the time being. We’ll come back to it in a later session.

Fruitful functions

As mentioned in Chapter 5 of the book, the built-in functions we have used, such as abs, pow, and max, have produced results. Calling each of these functions generates a value, which we usually assign to a variable or use as part of an expression. As from now, we are going to write functions that return values.

Remember we wrote a function called distance in the previous session? Modify it to return the distance instead of displaying it. Use that function to implement the function area found in section 5.3 of the book.

Like many programming languages with a LISP heritage, Python allows functions to be passed as parameters to other functions. This is called higher-order programming and is explained in section 5.4.

Unit-testing (using doctest and explained in section 5.8) allows programmers to test the code written easily.

Work out the nine exercises found at the end of Chapter 5. In case you don’t have time to finish in class, do it at home and don’t hesitate to contact us if you are stuck.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Using Functions

This topic is part of our Mastering Computer Programming training


A function “is a named sequence of statements that performs a desired operation” (as explained in Chapter 3 in the book, Think Like a Computer Scientist). Pay special attention to the syntax when defining functions especially the meaning of parentheses “(” and “)”

A function can be given arguments when called and they are bound to variables (called parameters) within that function. The programmer can also create other variables within that function and those are called local variables because they do not exist outside that function.

When a function is defined, it does not execute. The function only runs when it is called and the order in which functions are called gives the flow of execution of the program. Of course, a function needs to be defined before it can be called.

Exercises

Work out the first three exercises at the end of Chapter 3 in the book.

Write a program which calculates and prints the distance between two points. The first point has as coordinates (x1, y1) and the second (x2, y2). Write a function called distance which takes four arguments corresponding to the coordinates. The calculation is done using Pythagoras’ theorem which necessitates the square root function. Import that function from the Python math library using “from math import sqrt”.

Work out the fourth exercise in the book.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Types and Variables

This topic is part of our Mastering Computer Programming training

Welcome to our Mastering Computer Programming training!

Let us start by answering the following questions:

  • What is a computer?
  • What is computer science (also known as Informatique in countries such as France or Germany)?
  • What is a program?
  • What is a programming language?
  • Why Python?

The next step is to work through a series of exercises which will increase our understanding of computer programming using Python. For that, we will use the book How to Think Like a Computer Scientist / Learning with Python written by Jeffrey Elkner, Allen B. Downey, and Chris Meyers and which can be read online.

Try to find solutions to the exercises found at the end of Chapter 1 in the book, The Way of the Program. Pay special attention to the difference between evaluating an expression (e.g. ‘This is a test…’) as opposed to printing the evaluation of an expression (e.g. print ‘This is a test…’)

Variables, expressions and statements

Now, let’s tackle Chapter 2 of the book: Variables, expressions and statements. In Python, values are typed (i.e. integer, string, float, …) and, consequently, variables which contain values are also typed.

Let’s work out the exercises at the end of this Chapter. Pay special attention to the fact that an assignment has no value and, consequently, cannot be used in an expression. Furthermore, it is important to understand the difference between input() and raw_input().

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Download our Euro 2012 TV Guide Android application

We are proud to announce the launching of our Euro 2012 TV Guide Android application available for free in the Google Play Store.

Euro 2012 is starting in a few days in Ukraine and Poland. Use this application to know on which TV channels each match is being shown. Choose among the following countries:

  • Mauritius
  • France
  • United Kingdom
  • United States

You can browse day by day. Click on the flag of a country will open a browser with the latest information on each team participating in the World Cup (courtesy of Wikipedia.)

Changelog

Name    Date            Comment
======= =============== ====================
1.0     2012-06-06      First release
1.1     2012-06-08      Fixed some colours
                        and the Portugal flag
1.2     2012-06-15      Fixed some errors
                        concerning matches
1.3     2012-06-20      Added quarter finals
1.4     2012-06-25      Semi finals updates

Download Euro 2012 TV Guide and have fun!

Our forthcoming training courses

  • No training courses are scheduled.

Android Application Security

Android Applications Security

View more presentations from KnowledgeSeven

Today, I talked about Android Application Security during a workshop organised by the National Computer Board. I had the pleasure to explain to the audience how the permission model works in Android, how Android monitors all apps during runtime and how to use advanced tools such as LBE Privacy Guard on rooted Android devices to add an additional layer of security to Android.

I am happy to say that some people told me they liked my presentation a lot and that’s why I’m sharing it to all of you.

Feel free to ask me questions if you have any.

Our forthcoming training courses

  • No training courses are scheduled.
« Previous Page
Next Page »

Looking for something?

Want to know more?

Get our newsletter

Discover the latest news, tips and tricks on Linux, the Web and Mobile technologies every week for FREE

This work is licensed by Knowledge7 under an Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.