Unit 6
FUNDAMENTALS OF
PROGRAMMING
Types of Programming Languages
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 and uses memory
efficiently. However, writing programs in machine
language is very difficult.
Assembly Language
Assembly language is also a low-level computer language
but, instead of 1s and 0s, it uses symbols known as
mnemonics. Though 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, and it is used to translate
assembly language instructions into machine language.
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.
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.
What is Python ?
Python is a high-level, object-oriented programming language.
Most beginners in the development field prefer Python as one of
the first languages to learn because of its simplicity and versatility.
It is also well supported by the community and keeps up with its
increasing popularity.
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.
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, on the other hand,
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 for the program 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.
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. Figure shows the
Interactive Interpreter.
The >>> is where codes are written and is called the prompt. After a
user writes a code and presses the enter key, the prompt (>>>)
reappears for the user to write the next code.
The following example demonstrates what the Interactive
Interpreter does when the enter key is pressed after a simple
expression is written.
>>> 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 the following syntax
error:
>>>5+
SyntaxError: invalid syntax
>>>
Note that the area of a circle in the above example in Figure is incorrectly
calculated as “area=PI*radius*2”. The correct formula for the area of a circle is
“area=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 intended correct
output.
The * is multiplication operator in Python and ** is exponentiation operator.
To display something on the screen, the print() function is used in
Python. The following example shows how a string is displayed
using the print() function.
>>> 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.
Variables and Data Types
Variables
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
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
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.
Identifier
Identifier in python is the name which can be used for
identification purpose.
For example a person can be identified by name such as
Abebe, Almaz…etc
A name in python program is called identifier, it can be
class name, function name or variable name
example:- a=10 ( a is identifier and 10 is identifiers value)
Rules to Define identifiers
1. The only allowed character in identifier
Alphabet symbols (either lower or upper case )
Digits 0-9
Underscore symbol
By mistake if you use any other symbol like $ then you will
get syntax error
cash=10 correct
ca$h=10 wrong
2. Identifiers should not start with digits
123total
total123
3. Identifiers are case sensitive.(python in general it is case
sensitive)
4. We cannot use reserved word as identifier
example:- def=10
5. Dollar symbol ($) is not allowed in python identifier
Data type
A data type is a classification that specifies the type of
values a variable can have, and the type of operations
defined on the values. Integer (int), floating-point number
(float), string (str), and Boolean (bool) are some of the
major built-in data types in Python.
Notes
• Floating point numbers are numbers with decimal points
that represent real numbers.
•String that is used to represent text rather than numbers.
A string is a sequence of characters and can contain
letters, numbers, symbols and even spaces.
• Bool is a data type that has one of two possible values
(usually denoted true and false)
The data type of variables in Python is set when values are
assigned to the variables. If an integer number is assigned to a
variable, the variable will be of type int. If a string is assigned to a
variable, the variable then will be of type str. The following
example in the interactive interpreter shows how data types are set
to variables based on the value assigned to the variables.
Notes
As you can see in the following example, string values should be
placed in quotation marks.
To learn the data type of a particular variable, the type() function
can be used. Simply write type(variable_name) and press enter.
The interactive interpreter displays the data type of the variable as
shown above. In the above example, the data type of the variable x
is int while the data type of the variable y is str.
Example
>>> x=54
>>> y=“This is python”
>>> type(x)
<class ‘int’>
>>> type(y)
<class ‘str’>
>>>
Statements and Expressions
A statement is an instruction that the Python interpreter executes.
Assignment and print() statements are the two types of statements
that are in some ways mentioned in the previous sections. An
assignment statement, for instance, assigns value to a variable
while a print() statement displays the value that is given to it.
There are several other types of statements in Python including
the “if” statement, “while” statement, and “for” statement. Ex.
>>> print(“python”)
python
>>>x=5
Expressions are what the Python interpreter evaluates to produce
some value. An expression could be a simple literal value, a
variable, or something more complex which is a combination of
literals, variables, and operators.
Using the print() and assignment statements, following Figure
further shows the differences and relationships between
statements and expressions in Python.
Class work
1. What is the difference between statements and expressions in
Python?
2. Identify the statements from the following list that produce
outputs.
a) x=4*3
b) y=”Programming in Python is fun”
c) print(y)
d) z=x/2
e) print(z)
f) y=10
g) print(type(x))
3. When the code given in activity #2 is executed, what would be
the output on the screen?