CSP1150_Lecture_1_Introduction_to_Programming - Part B
CSP1150_Lecture_1_Introduction_to_Programming - Part B
Programming Principles
Lecture 1: Introduction to Programming (Part B)
Introduction to Python
Introduction to Python
• The Python shell is useful for quickly trying and testing bits
of code, but most of the time you will want to create a file
– Press Ctrl+N or go to “File > New File” to create a new file
(of course, you can also open existing Python code files)
– This opens a “Python editor” window which you can type
code into, before saving (Ctrl+S) and running (F5) the code
– Output from the code and prompts for input appear in the shell
Our Programming Environment
Save
Run the
the file
file
(Ctrl+S)
(F5)
Activities
• Most units consist of two weekly activities: A lecture and a workshop. Lectures
generally introduce theories and concepts, while workshops give you an opportunity
to apply them. If studying on campus, you should aim to attend both activities every
week. If studying online you are also encouraged to attend activities if able (and if
there is room), but otherwise you should aim to keep up with the unit and study one
module per week; It is easy to fall behind, but hard to catch up.
Workshop Preliminaries
Assessments & Academic Misconduct
• Units have assessments of various types, including assignments, tests and assessable
workshops, and most units also have an end of semester exam. Be sure to dedicate
appropriate time and effort to completing all assessments. Manage your time
responsibly and don’t leave them for the last minute. Seek assistance from staff if you
are struggling, confused, or desire feedback on your work.
• Assessments allow staff to determine whether you have sufficiently understood the
unit content, so it is vital that the work you submit for an assignment is your own.
Submitting unreferenced work that is not entirely your own or working with
classmates in an individual assessment is academic misconduct. We take academic
misconduct very seriously, and the penalties for it will be applied.
Student Email
• All students receive a student email address, which you must use when contacting
ECC staff. This is to ensure that the person we are communicating with in indeed who
they claim to be, and it also helps to ensure that your email is not inadvertently
filtered as spam. Be polite and professional in all emails and be sure to include the
unit code in the email subject line. We operate on a two working day turnaround time
for emails.
• To access your student email, log in to the Student Portal on the ECC portal.
Workshop Setup / Preparation
Setup – Environment Setup
• Throughout the unit, we will be using Python’s development environment “IDLE” to
write and run our code. If you are working on an ECU lab computer, Python should
already be installed. If you are working on your own computer, download and install
the latest version of Python 3 from python.org:
• www.python.org
• I have included a youtube in case you have trouble:
• https://www.youtube.com/watch?v=i-MuSAwgwCU
• Create a folder on a portable storage device such as a thumb drive, or on your own
device if you are using one, where you will save your source code files. Create
subfolders within this as needed to keep things organised, and remember that you
are responsible for the security and backup of your work.
• You WILL have to submit all your workshops as part of your portfolio – so make sure
you keep them organised.
Workshop Task
Task 1 – Concept Revision in the Python Shell
• Launch the “IDLE (Python GUI)” program on your computer. As covered in the lecture,
this will open the Python Shell, where you can type Python code at the “>>>” prompt
to execute it immediately.
• Let’s get acquainted with the shell and Python code by typing some statements. Feel
free to deviate from the workshop and experiment!
• Type the following statements into the shell:
1. (1 + 2) * 3
• This statement demonstrates basic arithmetic and using parentheses.
2. print('Testing')
3. print('Testing)
4. print(Testing)
• Note the different error messages that you receive, depending on the error.
5. # print('Testing')
• Comments ignored when code is run.
Workshop Task
6. x = 6
7. x = x + 1
8. print(x)
• These statements create a variable named “x” with a value of 6, add 1 to the value of
x, then print the value of x. In most languages, a statement of “x++” can be used to
add 1 to x.
1. Use the input() function to prompt the user for a value, and store that value in a variable.
2. Use the float() function to convert the value to a floating-point number, perform the
necessary arithmetic, and store the result in a variable.
3. Use the print() function to display the result in a user-friendly way.
• Once you’ve typed the three statements, save the file (Ctrl+S) into the folder you
created earlier, then press F5 to run the code. The prompt for a value will appear in
the shell window.
• Run the program a few times to test it and make sure that it is working as intended.
Try entering something that isn’t a number into the prompt and note what occurs
(and why). We will examine how to address this problem later in the semester, but
feel free to investigate it if you have time now.
• Write and test a few separate programs to do different conversions. (You will only
need to submit ONE but write as many as you are comfortable with).
Workshop Task
Task 4 – A Slightly Less Simple Program
• For a bit more of a challenge, let’s write a program with slightly more complex
processing. The code for this program will still follow the same basic structure of
obtaining input, performing arithmetic on it, and then displaying the result, but the
conversions/formulas are slightly more advanced. Again, you are welcome to
implement different conversions and formulas than the ones below.
Workshop Task
• Most of these programs will involve an additional statement or two in order to
prompt for all the necessary values or do additional calculations, but the logic behind
them is the same as before.
• Again, write a few of different programs and test them to ensure they are working
correctly. While it is a little bit redundant for such simple programs, write pseudocode
and draw flowcharts for the programs you create. How (if at all) does their design
differ from the design of previous programs?
• As a final exercise, enhance the output of all of the programs you have created by
using the built-in function round(). This function expects you to give it two
parameters – a number to round, and the number of digits to round it to. It returns
the number, rounded to the number of digits.
• Test it out by typing round(3.1416, 2) into the shell, then implement it in your
programs by using it on your result variables to round them to two decimal places.
• That’s all for this workshop. If you haven’t completed the workshop or readings, find
time to do so before next week’s class.