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.
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.
<< 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)
<< 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)
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.
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.
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
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.
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.
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.
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.
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.
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.