0% found this document useful (0 votes)
2 views

4c - Programming in Python - The Simple Sequence

This document provides an introduction to programming in Python, focusing on operators, creating expressions, debugging, and using the input function. It includes detailed explanations of various operators, their precedence, and associativity, along with practical activities and exercises for applying the concepts learned. Additionally, it discusses the importance of debugging and provides methods for identifying and fixing bugs in Python programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4c - Programming in Python - The Simple Sequence

This document provides an introduction to programming in Python, focusing on operators, creating expressions, debugging, and using the input function. It includes detailed explanations of various operators, their precedence, and associativity, along with practical activities and exercises for applying the concepts learned. Additionally, it discusses the importance of debugging and provides methods for identifying and fixing bugs in Python programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAMMING IN

PYTHON
Using the simple Sequence

© H. Chiname 2023. Version 05


INTRODUCTION
 The previous lecture introduced you to Python rules for
literal constants and variables.
 Some basic operators were introduced, but not in detail.
 This lecture introduces further aspects of Python, that
include:
 Operators;
 Creating expressions;
 Debugging; and
 Using the input function.
 By the end of the lecture you should be comfortable enough
to created small programs that make use of the simple
sequence building block.
© H. Chiname 2023. Version 05
MORE ON OPERATORS
 We introduced operators earlier on.
 We talked about and experimented with:
 Addition;
 Subtraction;
 Multiplication; and
 Division.
 Lets revisit operators again and add a little bit more
flesh.

© H. Chiname 2023. Version 05


MORE ON OPERATORS
Operator Name Explanation Examples
+ Plus Adds the two objects 3 + 5 gives 8. 'a' + 'b'
gives 'ab'.
- Minus Either gives a negative -5.2 gives a negative
number or subtracts one number.
number from the other 50 – 24 gives 26.
* Multiply Multiplies two numbers 2 * 3 gives 6. 'la' * 3
or repeats a string that gives 'lalala'.
many times.
© H. Chiname 2023. Version 05
MORE ON OPERATORS
Operator Name Explanation Examples
** Power Returns x to the power 3 ** 4 gives 81 (i.e. 3 *
of y 3 * 3 * 3)
/ Divide Divide x by y 4 / 3 gives
1.3333333333333333
// Floor Returns the floor of the 5 // 3 gives 1
Division quotient
% Modulo Returns the remainder of 5 % 3 gives 2.
the division -25.5 % 2.25 gives 1.5
© H. Chiname 2023. Version 05
MORE ON OPERATORS

Operator Name Explanation Examples


<< Left Shifts the bits of the 2 << 2 gives 8. 2 is
Shift number to the left by the represented by 10 in
number of bits specified. bits. Left shifting by 2
(Each number is bits gives 1000 which
represented in memory represents the decimal
by bits or binary digits 8.
i.e. 0 and 1)

© H. Chiname 2023. Version 05


MORE ON OPERATORS
Operator Name Explanation Examples
>> Right Shifts the bits of the 11 >> 1 gives 5. 11 is
Shift number to the right by represented in bits by
the number of bits 1011 which when right
specified. shifted by 1 bit gives
101 which is the
decimal 5.
& Bitwise Bitwise AND of the 5 & 3 gives 1.
AND numbers
© H. Chiname 2023. Version 05
MORE ON OPERATORS

Operator Name Explanation Examples


<< Left Shifts the bits of the 2 << 2 gives 8. 2 is
Shift number to the left by the represented by 10 in
number of bits specified. bits. Left shifting by 2
(Each number is bits gives 1000 which
represented in memory represents the decimal
by bits or binary digits 8.
i.e. 0 and 1)

© H. Chiname 2023. Version 05


MORE ON OPERATORS

 There are many more operators than these.


 Do not worry about copying them, you will get a list of
all the operators in Python.

© H. Chiname 2023. Version 05


THE PRIORITY LEVELS OF OPERATORS
 We are all aware of the BOMDAS rule.
 Mathematical operators have different levels of priority.
 In Python, operators also have priority or precedence
levels.
 These are listed on the Python website, but once again,
do not worry, you will get all the operators and their
precedence levels.

© H. Chiname 2023. Version 05


ASSOCIATIVITY OF OPERATORS
 Operators are generally associated from left to right if they
have the same level of precedence.
 If the operators have the same level of precedence, the
operator to the left of the other is evaluated first.
 For example, 2 + 3 + 4 is evaluated as:
(2 + 3) + 4
 Because two addition operators have the same level of
precedence, Python evaluates the addition sign on the left
first.
 Python considers operators like addition and subtraction to
have the same level of precedence.
 Multiplication and division also have the same level of
precedence.
© H. Chiname 2023. Version 05
ASSOCIATIVITY OF OPERATORS
 The different forms of division (division, floor division,
and modulo) also have the same level of precedence.
 However, multiplication has a higher precedence over
addition, therefore, even if the addition is operator to
the left of the multiplication operator, Python evaluates
the multiplication first.
 Therefore: 3 + 5 * 4 is evaluated as: 3 + (5 * 4) = 23 and
not as: (3 + 5) * 4 = 32.
 An expression that is inside brackets is always given a
higher precedence.

© H. Chiname 2023. Version 05


ASSOCIATIVITY OF OPERATORS
 Therefore, if you wish Python to give higher precedence
to an operator with a lower precedence, simply put
that operator inside brackets, then it will be evaluated
first.
 For example: 5 * (2 + 8) = 50.
 You will be issued with a document that shows all the
precedence levels of operators separately.
 Some operators like assignment operators have right to
left associativity.
 a = b = c is treated as a = (b = c)
© H. Chiname 2023. Version 05
ACTIVITY 1: CREATING EXPRESSIONS
 Using expressions in Python and your knowledge of variables,
create a program which uses length and width to calculate
area and perimeter of any given rectangle.

© H. Chiname 2023. Version 05


ACTIVITY 1 SOLUTION: CREATING EXPRESSIONS

© H. Chiname 2023. Version 05


COMMENT ON ACTIVITY 1 SOLUTION
 Notice how Python readily puts white space between
“area is” and “area”, even when the programmer has
neglected to do so.
 Explore how you can create useful expressions in the
area of finance using this newly acquired knowledge.

© H. Chiname 2023. Version 05


ACTIVITY 2: A FUTURE VALUE PROGRAM
 Create a program that calculates the Future Value (FV) of a
single cash flow, given the present value, the effective interest
rate, and the number of periods.
 All your variables may be set while your program is in design
mode, and they need not change whilst the program is in run
mode.
 Clue:
 Future value = present value x (1 + i)^n

© H. Chiname 2023. Version 05


ACTIVITY 2 SOLUTION: A FUTURE VALUE PROGRAM

© H. Chiname 2023. Version 05


INTRODUCING THE INPUT FUNCTION
 In all the programs that we created so far, we hardcoded
the values of variables directly into the program, meaning
that we made no distinction between programs and data,
since we typed in both as part of the program.
 While this may not be a big issue for the programmer, it
may turn out to be a big problem for an ordinary user who is
not a Python programmer.
 It may also be an issue when the values of the data need to
change all the time.
 It also means the value of variables cannot be changed in
run mode (while the program is running).
 Is there any way we can enter the values of variables when
the program is in run mode?
 The answer lies in the input function.
© H. Chiname 2023. Version 05
ACTIVITY 3: USING THE INPUT FUNCTION
 Create the following program by typing the code
exactly as it appears below.
 Afterwards, run the program and do exactly what the
program will ask you to do:

© H. Chiname 2023. Version 05


COMMENT ON ACTIVITY 3: USING THE INPUT
FUNCTION
 In Python version 3.X, the default input data type for the
input function is string. Therefore when inputting other
data types, we need functions that convert to the
appropriate datatype first before inputting.
 Hence use int(input()) to convert to integer and
float(input()) to convert to float (decimal).
 Note: when using Python version 2.7, (which we do not
use), the default datatype for the input function is integer
rather than string. Therefore you will need to replace the
input() function with the raw_input() function. The
raw_input() function takes string as its default datatype.

© H. Chiname 2023. Version 05


WHAT IS A BUG?
 A bug is something that makes a program not to function as
intended.
 Debugging is trying to find what is making a program not to
function as intended.
 There are different approaches to debugging.
 Given the experience that you already have with Python,
how would you debug?
 Some bugs are non-executable – the program gives an error
message.
 Other bugs are executable – the program runs and does not
give an error message as if everything is fine, meanwhile the
output that the program will be giving out will be wrong.

© H. Chiname 2023. Version 05


HOW TO DEBUG A PROGRAMME
1. Run parts of a programme as you work on them and look
out for error messages from Python. This approach works
for unexecutable bugs.
2. If the program is fairly long, you can comment out sections
of the program and run the parts that you want to check
only. This approach is suitable where there could be
multiple bugs in a programme, or you are not sure where
the bug is.
3. Working out the output of formulas independently, then
running the same formula in Python and then comparing
the results. This approach is good for executable bugs such
as formulas.

© H. Chiname 2023. Version 05


EXERCISE 1: CREATING AN EXCHANGE RATE
PROGRAM
 Create a small program that gives customers of a bank, the
exchange rates of the other currencies that are part of the
multicurrency basket in Zimbabwe against the US$.
 Each exchange rate should print as a string.
 Everyday, you should be able to quickly change the exchange
rates once you receive them, without tempering with the
strings.
 Be creative in deciding what to include.

© H. Chiname 2023. Version 05


EXERCISE 2: THE FV PROGRAM REVISITED
 In an earlier exercise, you created a program that
calculates future value.
 Revisit that program and this time, when you run the
program, it should ask you to enter the following, one
by one:
 the cash flow;
 the interest rate;
 the number of years;
 Afterwards the program should then output the future
value.

© H. Chiname 2023. Version 05


EXERCISE 3: CONVERTING EXCHANGE RATES
 In an earlier exercise, you created a program that listed the ruling exchange rates
between currencies.
 Now create an exchange rate program that converts the South African Rand (ZAR) to
the US Dollars (USD).
 The program should first ask the user to input the amount of ZAR that you want to
convert.
 It then converts the ZAR to USD.
 Finally it gives output of how many USD you will get for your ZAR, indicating clearly
the is amount is USD.

© H. Chiname 2023. Version 05


EXERCISE 4: ANOTHER EXCHANGE RATE PROGRAM
 Create an exchange rate program that gives out the ruling exchange
rate between the USD and five other currencies.
 The program asks you to enter the currency you want to convert the
USD to.
 It then fetches the correct stored exchange rate for the appropriate
currency and then displays only the exchange rate between the USD
and the selected currency only.
 The program should work only with the following currencies:
 ZWR – Zimbabwe RTGS Dollar;
 ZAR – South African Rand;
 BWP – Botswana Pula;
 GBP – British Pound;
 EUR – Euro
NB: The output should be an exchange rate, not an amount of money!
© H. Chiname 2023. Version 05

You might also like