FUNDAMENTAL OF
PROGRAMMING
1
• A COMPUTER PRGRAM IS A SET OF INSTRUCTIONS
THAT COMMANDS A COMPUTER WHAT TO DO.
Programming languages are computer languages that are used to
write different types of computer programs.
Two groups of programming language
1. Low – level language
2. High-level language
2
• 1. Low – level language
1.1 Machine language: it is a language in which everything including instructions,
numbers, and memory location are represented in 1s and 0s- binary system.
1.2 Assembly language: uses symbols known as mnemonics(reminders)
Use Assembler to translate assembly Language instruction into machine language.
2. High-level language
they 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: C, C++, Java, Python, etc
Compilers translate high-level language written programs all at once into machine language.
3
• All programming language have syntax and semantics.
1. Syntax refers to the rules of the programming language. It defines the structure or
grammar of the language that programs should strictly follow. A program must be written
with the correct syntax. 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
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 semantic of a language.
2. The semantics of a programming language, on the other hand, is related to the
meaning of elements of a program and what they do.
4
Basic of Python
• Python is one of the popular high-level programming languages
• 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 IDEL
can be used.
5
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.
>>>5+6
11
>>>print(“Hello World”)
Hello World
>>>
6
2. Using the Text Editor
Python codes can be written in a file using the text editor of Python’s IDEL. 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.
By using Python IDLE, you can create, modify, and execute script files. To create a new
python script file, go to File Select New File from the menu, as shown below.
After creating the new file, write the code as shown below and save the
file with .py extension. 7
to execute the python script file, go to Run select Run
Module or press F5. The Python IDLE shell will show the
result as shown below.
Like this, you can use the python IDLE editor to create, write, and execute
the python programs based on your requirements.
8
Variables and Data Types
I. Variables: are computer memory locations. They are the means to store
data in computer memory. In Python a variable is created along with its
value. Values are assigned to variables using the assignment(=),which has
two operands.
Example: >>> x=5, “x” is variable and “5” is value
y = “hello” “y” is variable and “hello” is value
9
• Identifier: the name that is given to variable is called an identifier. Identifiers are a string
of one or more characters and have to conform to some rules in Python to be
considered valid:
The rules of identifiers are:
The first character should be either a letter or an underscore ( _ ).
If the identifier is made up of more than one character, the characters after the first
one should be a letter, number, or underscore.
An identifier cannot be the same as any of the keywords in Python.
Identifiers are case-sensitive. (For instance, x and X are considered as different
identifiers in Python)
10
Keywords in Python:
and del from not while
as elif global or with
assert else if pass yields
break except import print
class exec in raise
continue finally is return
def for lambda try
Keywords are reserved words that have predefined meanings in a programming language
11
• Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscore (A-z, 0-9,
and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
Python allows assign a single value to multiple variables.
Example: x = y = z = 5
You can also assign multiple values to multiple variables.
For example − x , y , z = 4, 5, “python” 4 is assigned to x, 5 is assigned to y and string
“python” assigned to variable z respectively. x=12 y=14 x,y=y,x print(x,y) Now the result
will be 14 12
12
II. Data Types in Python
• 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.
• Python has Two data types – 1. Primitive Data Type (Numbers, String)
2. Collection Data Type (List, Tuple, Set, Dictionary)
example 1. List=[1,2,3,[5,6]]
Print(list) => [1,2,3,[5,6]]
2. tuple=(‘a’,b’,c’,3.5)
print(tuple) => (‘a’,b’,c’,3.5)
3. tuple=(“1.hailu\n2.tolla\n3.bantu”)
print(tuple) =>1.hailu
2.tolla
3.bantu
13
Type conversion is the process of converting the value of one data type to another.
Type conversion is of two types in Python:
• Data Type Conversion
implicit conversion and explicit conversion.
Implicit conversion is a type of conversion in which Python does the conversion without
the involvement of the programmer. This is usually done to avoid loss of data.
Explicit conversion is a type of conversion of one data type to another that
is done by the programmer. The predefined functions associated with
the different kinds of data types such as int(), float(), and str() are used for explicit conversion.
Example:>>>x=4 type(x) <class ’int’>
>>>y=5.5 type(y) <class ‘float’>
>>>z= x+y >>> type(z) <class ‘float’>
9
14
Type Casting: To convert one data type into another data type. Casting in python is
therefore done using constructor functions
The syntax for conversion is given as follows:
<required_datatype>(value)>
Example:>>>x=2.2 type(x) <class ‘float’>
Convert to 2.2 int and str
Answer: int(2.2) -> 2
str(2.2) -> ‘2.2’
15
data type Meaning and sample Associated Sample
values
operators expressions
Integer numbers Arithmetic operators
Addition(+) >>>5+3 8
Example: Subtraction(-) >>>6-2 4
int 34 Division(/) >>>9/2 4.5
4 Floor division(//) >>>2*3 6
multiplication(*) >>>5%2 1
56 Modulus(%) >>>2**3 8
10 Exponentiation(**)
floating-point Arithmetic operators
numbers example: Addition(+) >>>2.5+3.2 5.7
float Subtraction(-)
4.5 >>>5.5-3.2 2.4
Division(/)
10.6 >>>5.5/2 2.75
Floor division(//)
100.0 >>>4*2.2 8.8
multiplication(*)
Exponentiation(**) >>>2.5**2 6.25 16
Meaning and sample Associated Sample expressions
data type
values operators
A sequence of Concatenation(+) >>>”Hello” +”World”
str characters enclosed ‘Hello World’
in a double
quotation
Example:
”Hello World”
bool True or false values Boolean operators
Example: . And
True . or
False
17
Statements and Expressions
• Statement are two types: Assignment and print() statements
• A statement is an instruction that the Python interpreter executes.
• An assignment statement, for instance, assigns value to a variable
• 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
Example: Assignment and print()
Python Script Output on the IDLE Shell
x=5
y=10 15
print(x+y) >>>15
18
• 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.
• Literals: These are language-independent terms in Python and should exist
independently in any programming language. In Python, there are the string literals,
byte literals, integer literals, floating point literals, and imaginary literals.
19
• Functions: Functions and its use: Function is a group of related statements that
perform a specific task.
• Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable. It avoids
repetition and makes code reusable.
• Basically, we can divide functions into the following two types: 1. Built-in functions -
Functions that are built into Python.
Ex: abs(),all().ascii(),bool()………so on…. integer = -20 print('Absolute value of -20
is:', abs(integer))
Output: Absolute value of -20 is: 20
2. User-defined functions - Functions defined by the users themselves. def
add_numbers(x,y):
sum = x + y return sum
print("The sum is", add_numbers(5, 20)) Output: The sum is 25
20
• Flow of Execution:
1. The order in which statements are executed is called the flow of execution
2. Execution always begins at the first statement of the program.
3. Statements are executed one at a time, in order, from top to bottom.
4. Function definitions do not alter the flow of execution of the program, but remember
that statements inside the function are not executed until the function is called.
5. Function calls are like a bypass in the flow of execution. Instead of going to the next
statement, the flow jumps to the first line of the called function, executes all the statements
there, and then comes back to pick up where it left off
21