ICT Fundamentals of Programming - Chapter 6 Notes

Fundamentals of Programming

Learning Outcomes

At the end of this unit, students will be able to:

  • Define computer programs.

  • State and describe types of programming languages.

  • Explain syntax and semantics of programming languages.

  • Explain variables and data types.

  • Differentiate the concepts of statements and expressions in Python.

  • Analyze simple programs written in Python.

  • Write simple programs with input and output.

Unit Overview

  • A computer program is a set of instructions that commands a computer what to do.

  • Computers do whatever task they do by simply following the instructions stated in programs.

  • This unit covers topics related to computer programs including programming languages, syntax, and semantics.

  • The high-level programming language known as Python is used to demonstrate the basic concepts of programming.

6.1. Types of Programming Languages

  • Programming languages are computer languages that are used to write different types of computer programs.

  • They are generally grouped into three types:

    1. Machine language

    2. Assembly language

    3. High-level language

1. Machine Language

  • Machine language is a low-level computer language.

  • It is a language in which everything including instructions, numbers, and memory locations are represented in 1s and 0s – binary system.

  • Machine language is the language that computers understand directly without any need for translation.

  • That is why it is very fast & uses memory efficiently.

  • However, writing programs in machine language is very difficult.

2. Assembly Language

  • Assembly language is also a low-level language.

  • It uses symbols known as mnemonics instead of 1s and 0s.

  • Still, it is easier than using a binary system, assembly language is still difficult.

  • Since computers do not directly understand any program outside the machine language.

  • Programs that are written in assembly language require a special type of software.

  • This software is known as Assembler, it is used to translate assembly language instructions into machine language.

3. High-Level Language

  • High-level languages are closer to human languages compared to both assembly and machine languages.

  • This type of language allows programmers to focus more on the problem they want to solve than on the programming language.

  • Examples of high-level programming languages include C, C++, Java, C#, Python, Perl, and Ruby.

  • Just like assembly language programs, high-level language programs also cannot be directly executed by the computer.

  • The programs have to be first translated into a machine language using translator software.

  • Depending on the programming language, the translator software can be either a Compiler or an Interpreter.

Compiler vs Interpreter

  • Compilers translate high-level language written programs all at once into machine language.

  • The machine language is then executed by the computer.

  • Examples of programming languages that use compilers are C, C++, Java, and C#.

  • Interpreters, on the other hand, translate and execute programs a statement at a time.

  • They don’t translate the whole program together as do compilers.

  • Examples of programming languages that use interpreters include Python, Perl, and Ruby.

6.1.2 Syntax and Semantics

  • Like any human language, all programming languages have syntax and semantics.

  • Syntax refers to the rules of the programming language.

  • It defines the structure or grammar of the language that programs should strictly follow.

  • The semantics of a programming language, is related to the meaning of elements of a program and what they do.

  • A program must be written with the correct syntax dictated by the programming language to be executed by the computer.

  • If a program violates any of the syntax rules of a language, the compiler or the interpreter produces an error message.

  • Such type of error is known as a syntax error.

  • A program can have no syntax error and get executed properly but can still behave in a way different from what it is intended to.

  • This kind of error is known as logic error and is associated with the semantics of a language.

  • Since compilers or interpreters do not catch logic errors, they are far more difficult to identify and fix than syntax errors.

6.2. Basics of Python

  • Python is one of the popular high-level programming languages in use today.

  • It is widely considered a much easier language to learn.

  • This is one of the main reasons why it is a widely chosen language for teaching programming to those who are new to programming.

  • Python has a free integrated development environment known as IDLE.

  • IDLE stands for Integrated Development and Learning Environment.

  • To write Python codes, the interactive interpreter or the text editor of the IDLE can be used.

  • The interactive interpreter is used to write one line of Python code at a time and is less convenient to write and execute a large number of codes.

  • Using the text editor, however, any number of codes can be written and get executed with a single command.

6.2.1 Using the Interactive Interpreter

  • The Interactive Interpreter contains a Python shell.

  • which is a textual user interface used to work with the Python language.

  • The Interactive Interpreter is displayed when the IDLE is opened.

  • The >>> is where codes are written and is called the prompt (ready).

  • After a user writes a code and presses the enter key, the prompt (>>>) reappears for the user to write the next code.

  • For example:
    >>> 5+6 11 >>>

  • 5+6 is a syntactically valid expression that evaluates to 11.

  • Therefore, the Python interpreter evaluated the expression and displayed 11.

  • If, for example, a syntactically invalid expression like 5+ is given, the interpreter generates a syntax error:
    >>> 5+ File "<python-input-1>", line 1 5+ ^ SyntaxError: invalid syntax >>>

  • To display something on the screen, the print() function is used in Python.

  • For example:
    >>> print("hello world") hello world >>>

  • Note that the IDLE uses different types of colors for the different elements of a Python code to make them easily distinguishable.

  • By default, outputs are displayed in blue color; functions are displayed in purple color, and strings are displayed in green color.

  • A string is a sequence of characters placed under quotation marks such as “Hello World” as can be seen in the above example.

6.2.2 Using the Text Editor

  • Python codes can be written in a file using the text editor of Python’s IDLE.

  • The code that is kept in such files is known as a script.

  • A file that keeps Python scripts is saved with the .py extension and is called a script file.

  • A script file can be easily created by selecting “New File” from the “File” menu in the IDLE’s interactive interpreter.

  • The prompt (>>>) is not shown in the text editor.

  • The >>> appears only when code is written in the interactive interpreter, not in the text editor.

  • While only one line of code is written and executed in the interactive interpreter, as many lines of code as required can be written in the text editor.

  • The example shown calculates and displays the area of a circle for a given radius value of 3.

  • Before the script is run/executed, the script has to be saved with the .py extension by selecting the “save” option from the “file” menu in the text editor.

  • To execute the script, the “Run Module” option from the “Run” Menu should be selected.

  • After the script is executed, the output is displayed in the IDLE shell/interactive interpreter.

  • Example:

pi=3.14
radius=3
area=pi*radius**2
print(area)

Output:

RESTART: 
28.26
  • There are four statements in the script. However, the script is executed with a single command.

  • There is no prompt (>>>) in the script. The prompt is shown only in the interactive interpreter.

  • Though there are four statements in the script, only one output is shown in the IDLE shell.

  • This is because it is only the last statement that produces an output.

  • The output of a script written in the text editor is shown in the IDLE shell or the interactive interpreter.

  • The * is multiplication operator in Python and ** is exponentiation operator.

  • The example demonstrates a logic error that is related to the semantics of programming languages.

pi=3.14
radius=3
area=pi*radius * 2
print(area)

Output:

RESTART:
18.84
  • The area of a circle in the above example is incorrectly calculated as area=PI*radius*2.

  • The correct formula for the area of a circle is area=PIradius2area = PI * radius^2

  • However, the interpreter did not generate any error and simply displayed the incorrect output.

  • This is a logic error: even though the program does not have any syntax error, it does not produce the proposed correct output.

6.3. Variables and Data Types

  • Variables are computer memory locations.

  • They are the means to store data in computer memory.

  • A variable is used every time user data or intermediary data is needed to be kept.

  • In Python, a variable is created along with its value.

  • Values are assigned to variables using the assignment (=) operator, which has two operands.

  • The operand to the left of the operator is always a variable while the operand to the right of the operator is either a literal value or any other type of expression.

  • The following example demonstrates how a variable named “x” is created with the value 5 in the interactive interpreter.

>>> x=5
>>> x
5
  • The example that follows shows how a variable named “y” is created and assigned to the result of an expression.

  • The value of “y” would be the sum of 5 and 9, which is 14.

>>> y=5+9
>>> y
14
  • To see the current value of a variable, you can simply type the name of the variable and press the enter key in the interactive interpreter.

  • The value will then be displayed as shown in the following example.

>>> y=5+20
>>> y
25
  • The value 25 that is shown in blue color is the value of the variable y.

  • Moreover, a variable can be assigned to another value than the one it was previously assigned to.

  • The following example shows how the value of y is changed from 25 to 30.

>>> y=5+20
>>> y
25
>>> y=25+5
>>> y
30

Some additional examples:

>>> 5+6
11
>>> 5+
  File "<python -input -1>", line 1
    5+
     ^
SyntaxError: invalid syntax
>>> print("hello world")
hello world
>>> y=5+20
>>> y
25
>>> y=25+3
>>> y
28
>>> x=54
>>> y=("this is python")
>>> type(x)
<class 'int'>
>>> type(y)
<class 'str'>
>>> 5+3
8
>>> 6 - 2
4
>>> 9/2
4.5
>>> 9//2
4
>>> 5%2
1
>>> 2**3
8
>>> x=5.5
>>> type(x)
<class 'float'>
>>> x="hello"
>>> type(x)
<class 'str'>
>>> x=4
>>> type(x)
<class 'int'>
>>> y=5.5
>>> type(y)
<class 'float'>
>>> z=x+y
>>> z
9.5
>>> x=4
>>> type(x)
<class 'int'>
>>> y="5"
>>> type(y)
<class 'str'>
>>> z=x+y
TypeError: unsupported operand type(s) for  +: 'int' and 'str '
>>> x=4
>>> type(x)
<class 'int'>
>>> y="5"
>>> type(y)
<class 'str'>
>>> z=x+ int(y)
>>> z
9
>>> type(z)
<class 'int'>

More code examples:

x=6.5
print(5)
print(x)
print(x*5)
print("this is a string")

Output:

RESTART:
5
6.5
32.5
this is a string
>>> x=5
>>> y=10
>>> print(x+y)
15
>>> x=5
>>> x
5
>>> x+5
10
>>> 5
5
>>> "hello"
'hello'
>>> pi=3.14
radius=3
area=pi*radius**2
print(area)

Output:

RESTART: 
18.84
pi=3.14
radius=3
area=pi*radius*2
print(area)

Output:

RESTART: 
28.26
pi=3.14
x=input("enter the radius:")
radius=float(x)
area=pi*radius**2
circumference=2*pi*radius
print("the area of the circle is : ")
print(area)
print("the circumstance of the circle is: ")
print(circumference)

Output:

RESTART:
enter the radius:6
the area of the circle is :
113.04
the circumstance of the circle is: 
37.68
x=6.5
print(5)
print(x)
print(x*5)
print("this is a string")

Output:

RESTART:
5
6.5
32.5
this is a string
pi=3.14
x=input("enter the radius:")
radius=float(x)
area=pi*radius**2
circumference=2*pi*radius
print("the area of the circle is : ")
print(area)
print("the circumstance of the circle is: ")
print(circumference)

Output:

RESTART:
enter the radius:6
the area of the circle is :
113.04
the circumstance of the circle is: 
37.68
>>> x=5
>>> y=10
>>> print(x+y)
15
>>> x=5
>>> x
5
>>> x+5
10
>>> 5
5
>>> "hello"
'hello'
>>> pi=3.14
radius=3
area=pi*radius**2
print(area)

Output:

RESTART: 
18.84
pi=3.14
radius=3
area=pi*radius*2
print(area)

Output:

RESTART: 
28.26
x=6.5
print(5)
print(x)
print(x*5)
print("this is a string")

Output:

RESTART:
5
6.5
32.5
this is a string
pi=3.14
x=input("enter the radius:")
radius=float(x)
area=pi*radius**2
circumference=2*pi*radius
print("the area of the circle is : ")
print(area)
print("the circumstance of the circle is: ")
print(circumference)

Output:

RESTART:
enter the radius:6
the area of the circle is :
113.04
the circumstance of the circle is: 
37.68