Knowledge7

The Linux and Mobile Solution Provider

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

Using Web Frameworks

This topic is part of our Mastering Computer Programming training

Quite a lot of web applications allow people to Create new data as well as Read, Update and Delete existing data. They are called CRUD applications. Right now, our racing simulation is not such an application as we cannot, for example, change the name of an existing driver or create a new one…

Work to do

Using the capabilities of Flask, we will enhance our existing race simulation to allow drivers to be manipulated: new ones can be created while exiting ones can be either deleted or some of their information changed (i.e. names and abilities).

Flask allows for very flexible routing as it handles parameters very simply. GET parameters are accesses through the request.args dictionary while POST data can be accessed through the request.form dictionary.

Naturally, we will enhance the existing web application while making sure that it still complies with the MVC architecture.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Programming for the Web

This topic is part of our Mastering Computer Programming training

The Web, invented in 1991 by Tim Berners-Lee, has changed the way people interact with computers. More and more applications are now web-based and, consequently, programmers need to master standards such as HTTP, HTML and CSS. Interestingly, programming platforms, such as Python, have had to evolve too to provide facilities to implement web applications. Over the years, people have moved from low-level networking aspects to focus more on high-level aspects of web application development such as architecture, performance and security. This has been made possible, mostly, by the emergence of web frameworks.

Work to do

Using the Flask framework, we are going to further enhance our race simulation program to make it become a full-fledged web application. Like most modern web frameworks, Flask recommends that applications have a Model-View-Controller (MVC) architecture and that a templating engine be used to render views.

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Changing Text

This topic is part of our Web Design with HTML and CSS training

In the early days of the Web, text was displayed in only one font and in one size. However, a combination of HTML and CSS now makes it possible to control the appearance of text on a web page. Since lists are also commonly used, HTML provides tags that automatically indent text and add numbers, bullets, or other symbols in front of each listed item.

The course covers:

  • Working with Text Blocks and Lists
    • Align text – left, center, right
    • Three types of HTML Lists
      • Ordered list
      • Unordered list
      • Definition list
      • Place lists within lists
  • Working with fonts
    • Use of boldface,italics and special text for formatting
    • Use of special characters
This topic is part of our Web Design with HTML and CSS training

Our forthcoming training courses

  • No training courses are scheduled.

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.
« 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.