Iterations in Python

Python is a high-level programming language, which is becoming more and more common. This is why learning Python is NECESSARY for Computer Scientists.

A loop is a series of instructions that is repeated. A loop falls in one of three categories:

  • Count-control loop: This is a loop that occurs for a defined amount of times.
  • Post-condition loop: This is a loop that repeats the sequence as long as the condition is FALSE. Python does not have post-condition loops, so, you’ll have to use a pre-condition loop.
  • Pre-condition loop: This is a loop that repeats the sequence as long as the condition is TRUE.

Syntax

Now it’s time to look at the syntax for each type of loop. When learning a new language the only thing that you learn for the first time is the language’s syntax.

In Python, indentation is absolutely necessary for the program to execute correctly. In addition, it’s important to insert colons after the loop. Finally, it’s important to note that Python doesn’t require semicolons at the end of each line of code, unlike other languages.

Count-control loop syntax:

for <x> in range (s,e,i):
 <statements>

Now, let’s define some of the values. These values (s, e, i) can only be integers [int() must be used to convert a string into an integer]. The lower bound, s, is where <x> starts at, incrementing by i after each loop. The loop ends when <x> reaches e.

The values of s and i do not have to be included, and are set to 0 and 1 when omitted.

Pre-condition loop:

while <condition>:
 <statements>

This is pretty self explanatory.

Examples:

Create a program to take as input two numbers. The program is supposed to count down by 1 from the first number to the second.

We can use a count controlled loop to complete the task. Our first attempt at writing this program might look like this:

num1 = input("Input large number: ")
num2 = input("Input smaller number: ")
for x in range (int(num1),int(num2)-1,-1):
 print(x, end = ' ')

However, if the user input results in num1 being greater than num2, then the program will crash. So, we need to reprompt the user until num1 > num2.

This can be achieved with a pre-condition loop (since Python doesn’t have post-condition loops). We can ammend the code to look like this:

while int(num1) < int(num2)
 num1 = input("Input large number: ")
 num2 = input("Input smaller number: ")
for x in range (int(num1),int(num2)-1,-1):
 print(x, end = ' ')

Summary:

  • Python is a common high-level programming language.
  • The Python syntax involves the use of indentation and colons.
  • In general, there are three types of loops: count-controlled, post-condition, and pre-condition.
  • Python only has count-controlled pre-condition loops.
Advertisements

The Scope of Variables

When programming, one of the most fundamental concept is variables. These are defined values, which are stored, and can be redefined. These variables are important because they’ll typically be referenced later on in the program.

In programming, the scope of a variable refers to whether it is local or global. A local variable can only be accessed within a specified part of a program, while a global variable can be accessed from any part of the program.

Local variables occur when a variable is defined inside a function, making them accessible only to that function and other nested functions.

JavaScript Example

Let’s look at a basic example of these scopes in one of the most popular programming languages: JavaScript.

A variable is initially definied with var and can later be redefined without it.

Similarly, a function occurs with the presence of function, the name, and the function embedded in these brackets {}.

var a = 18;
function myFunction() {
 var b = "Hello World!"
 alert(b);
 }

The above code defines a as a number, 18, and b as a message, “Hello World!” In this case, b is a local variable because it can only be called when myFunction is called. However, a can be called anywhere in the code.

The distinction between these two variables is important to make, so, that someone who is only a beginner at programming doesn’t end up calling a local variable outside of its function.

Summary:

  • Variables are defined values, that store information which will be needed later on in the code.
  • The scope of a variable refers to whether it is local or global.
  • Local variables can be called only in the parent function, while global variables can be called anywhere in the code.
  • In JavaScript, both global and local variables can be created and accessed.

Introduction to Entity Relationships (with Worked Example)

In Comptuter Science, an entity relationship is the comparison of two entities. The relationship between them will fall into one of four categories:

  • One to One (1:1)
  • One to Many (1:M)
  • Many to One (M:1)
  • Many to Many (M:M)

As an example, let’s do an entity relationship for a school. We can build several entity relationships with this scenario. Let’s choose school, students, teachers, and rooms as our entities.

After you identify your entities, you should identify which of those will have relationships. For this example I’m going to be using the following as the relationships:

  • School to Students
  • School to Teachers
  • School to Classrooms
  • Students to Teachers
  • Teachers to Classrooms

The first three relationships will be 1:M because there is one school with many students, teachers, and classrooms. In addition, the students, teachers, and classrooms are all allocated to the same school.

The relationship between students and teachers will be M:M, because one student has many teachers and one teacher teaches many students.

The final relationship can vary. For example, a teacher might not have their own classroom, and instead have to teach in different classrooms. This would also mean that one classroom might have more than one teacher who use it. But, not all rooms have multiple teachers and not all teachers use multiple rooms. So, let’s assume that each teacher has their own classroom and each room has only one teacher. So, the relationship will be 1:1.

It would be redundant to create a new relationship for Students to Classrooms because it will be the same as Students to Teachers.

Let’s now recall the relationships.

  • School to Students (1:M)
  • School to Teachers (1:M)
  • School to Classrooms (1:M)
  • Students to Teachers (M:M)
  • Teachers to Classrooms (1:1)

Summary:

  • An entity relationship is the comparison of two entities
  • A relationship can be either 1:1, 1:M, M:1, or M:M
  • Figuring out an entity relationship takes practice

Normalising a Database

Sometimes data may be stored in a manner that is redundant. Data redundancy is an issue that costs time and, ultimately, money.

Storing your data in a single relational database like the one below comes with its consequences.

Note: The primary key is identified with an underline

You might come up with problems when:

  1. Inserting data – Trying to insert a new row of data, or ‘tuple’, will be tedious, because you’d have to repeat redundant data.
  2. Updating data – Updating data will cause a similar waste of time. If the old teacher left and was replaced, you’d have to replace every instance of the teacher within the database.
  3. Deleting data – If you’d delete every student from the database you’d also delete the teacher and subject information.

Normalisation seeks to solve such problems be reducing it. This will be done by splitting the relational table up and connecting them with a foreign key.

Doing so will make the normalised data look like this:

Notice how you still have redundancy in the first table. That’s because it’s impossible to completely eleiminate reundancy – the purpose of normalisation is to reduce redundancy.

Let’s consider the previous scenario in which the school got a new computer science teacher. With normalised data, you’d only need to update the data in one location.

What if the previous students graduated? You’d have to delete them from this database, however, deleting their information will not delete the teacher name, room number, or course because they’re located in a separate table.

What if there was a new computer science student? You wouldn’t need to reinsert the room number or teacher name, just the course.

One final thing to remember is that data can be normalised more than once. Each level of normalisation (first normal form, second normal form, third normal form, etc.) will further reduce data redundancy. This is why you want to aim for the highest normal form possible.

Summary:

  • Unormalised data has data redundancy, which creates problems when inserting, updating, or deleting data.
  • Normalising a relational table will spilt it into two tables and link them with a foreign key.
  • Normalisation cannot eliminate redundancy, but it can reduce it.
  • There are different levels of normalisation.

Ethics in Computer Science

Ethics is often considered as the field which deals with moral science. However, there are variations in how ethics is defined; one definition is the rules of conduct recognised in a particular profession or area.

This definition aligns more closely to the ethics used in Computer Science. Of course, it all comes to one thing: doing the right thing. This means taking into account the general public, your employer, colleagues, etc. when creating software.

Ethics…

Software Engineering Code of Ethics

This is where the ACM/IEEE Software Engineering Code of Ethics comes into play. Although the Association for Computing Machinery (ACM) and the Institute of Electrical and Electronics Engineers (IEEE) are American-based organisations, they have a global influence. So, you should still take these rules into account if you don’t live in the USA.

The code of ethics has eight principles with several clauses within each principle. The following is a list of the eight principles:

  1. PUBLIC Software engineers shall act consistently with the public interest.
  2. CLIENT AND EMPLOYER Software engineers shall act in a manner that is in the best interests of their client and employer consistent with the public interest.
  3. PRODUCT Software engineers shall ensure that their products and related modifications meet the highest professional standards possible.
  4. JUDGEMENT Software engineers shall maintain integrity and independence in their professional judgment.
  5. MANAGEMENT Software engineering managers and leaders shall subscribe to and promote an ethical approach to the management of software development and maintenance.
  6. PROFESSION Software engineers shall advance the integrity and reputation of the profession consistent with the public interest.
  7. COLLEAGUES Software engineers shall be fair to and supportive of their colleagues.
  8. SELF Software engineers shall participate in lifelong learning regarding the practice of their profession and shall promote an ethical approach to the practice of the profession.

In total, there are 80 clauses within the principles. A full list of this clauses can be found by clicking here.

There is no list of what to do in a given scenario. Instead, a software engineer should be capable of making the right choice based off the guidelines in the code of ethics.

Ownership and Copyright

Another ethical concern in Computer Science is the issue of ownership. The formal regonision of ownership is given with copyright. An individual who creates something new has the right to claim copyright. However, if the individual creates that product under a company, the company may be the one who claims copyright.

In either case, there are laws to protect the owner. Unfortunately, these laws cannot guarantee that people will follow them.

Of course, there are rules regarding copyright.

In addition, different countries have different laws regarding copyright. There is an international agreement, however, that these laws cannot be broken by reproducing the product in a different country from where the work was originally created.

For someone to use a work that is protected by copyright, they’ll have to get permission from the original owner. This usually happens through a monetary transaction.

Software Licensing

Finally, there is software licensing (an agreement between the user and the owner as to how the software is used).

There are different forms of licensing and it’s important to know what they are so that you don’t end up getting in trouble for using software illegaly.

Commercial software is software that has to be paid for to be used. The company decides what the options and rates are for access to the software.

One type of commercial software is shareware, which is a limited version of the software used for demonstration (demos) purposes or trials. This may also come in the form of beta testing.

However, not all software is pay-only. Believe it or not, there is a ton of software available for free. Two types of free licensing software are open source (both the software and source code are included) and freeware (the software is included, but not the source code).

Open source software encourages collaboration to take place. Ultimately, a software can use, modify, or distribute open source software freely.

But, an alternative form comes from the Free Software Foundation, which shares similar objectives. But, it incorporates ‘copyleft’, a condition that if the source code is modified it must be made available under the same conditions.

Copyleft’s logo

Summary:

  • In Computer Science, the definition of ethics deals more with acting within your profession.
  • The Software Engineering Code of Ethics is a set of ethical guidelines that take into account the general public, your employer, and your colleagues.
  • Copyright laws are put into place to protect original work and should be followed.
  • There are different types of licensing agreements such as: commercial software, shareware, freeware, and open source software.

Data Security, Privacy, and Integrity

The fact that the Internet exists means that more data is available than ever. Unfortunately, with all the benefits like live news and more knowledge comes some drawbacks.

One significant drawback is that private data can be made public on the Internet.

There is a difference between data security, data privacy, and data integrity. The previous paragraph deals with data privacy.

Data Privacy

Data privacy is the idea that private data should be kept off of the public domain. Each person should be in control of which data is on and off the public domain.

Whether it be a person or an organisation, the owner of the data still has the right to data privacy. They can choose to allow someone else to view the data, but only under terms and conditions agreed upon by the owner.

When someone accesses the data without permission, it is breached. Certain countries have in place data protection laws, which penalises someone who breaches data.

Unfortunately, having such laws does not guarantee that people will follow them. However, it does deter some people with the consequences such as legal proceedings.

If this guy tries to breach your private data, he can be legally charged.

Data Integrity

For data to stay relevant, it must be up to date with the rest of the world. Data integrity means that data held by a company is accurate and up to date.

This is easy to define on paper, however, it is very difficult to ensure. A large organisation can spendo hundreds of thousands of dollars in an attempt to ensure data integrity.

Without data integrity, there is a lack of trust in an organisation. Having data integrity is critical to the implementation of any system that uses data.

Data Security

The idea of Data Security means that data is secure and can be recovered in the case of a major issue (ie. natural disaster, security breach, system error).

Data security is very hard to keep track of. The bad guys, who dedicate themselves to getting ahold of your data, use advanced techniques each time. As a result, the good guys must keep their techniques up to date.

It’s important to note that without data security, there isn’t a possibility of ensuring data integrity and/or data privacy. However, data security cannot ensure either of the two by itself.

Moreover, data security is very hard to keep, which is why many companies are willing to spend so much money on employees who’ll ensure data security. We’ve all heard of companies having their data breached on a large scale. By spending over $100,000 a year on someone who’ll secure the data, companies end up investing in their security (Remember, people tend to spend more money when they’re scared or worried).