Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1022 Computer Programming & Principles

Similar presentations


Presentation on theme: "CS1022 Computer Programming & Principles"— Presentation transcript:

1 CS1022 Computer Programming & Principles
Lecture 2.2 A brief introduction to Python (2)

2 Plan of lecture Intro to Portable Python
Integrated development environment (IDE) Edit-run workflow From maths to pseudo-code to Python Codecademy Learning to learn What next? CS1022

3 Portable Python A pre-configured Python distribution
Runs directly on Windows from a USB device Portable, lightweight, hassle-free No need to fiddle with class paths, access rights Large set of libraries bundled together No need to get these separately Drawbacks: Only for Windows CS1022

4 Portable Python (2) We’ll cover the basics of the IDE
Editor and edit-run loop Making sense of mistakes Here’s what you should have: Double-click on “PyScripter-Portable.exe” CS1022

5 Python interpreter pane
Portable Python (3) Here’s what you should see: Files pane Editor pane Python interpreter pane CS1022

6 Portable Python (4) 2 1 3 Basic edit-run loop: What kinds of problems?
Load a file Edit it (editor gives feedback) Run it If there are problems, go to 2 else stop What kinds of problems? Program aborts (does not run) Program runs but does not do what we intended 1 2 3 CS1022

7 Editing a Python program
Let’s edit a new Python program: Highlight the text in 2 Delete all of it Type the following in 2 1 2 3 # absolute value n = -20 if n < 0: abs = -n else: abs = n print(abs) CS1022

8 Running a Python program
To run the program we typed, we can: Type <ctrl-F9> or click on green arrow above pane 2 Program will be run in pane 3 CS1022

9 Running a Python program (2)
If we wanted to try the previous program with different values we would have to edit line 1 Instead, we can provide parameters from keyboard CS1022

10 Editing a Python program (revisited)
Let’s edit our Python program and add the following: import “argv” (functionality) from module “sys” from sys import arg # absolute value n = int(argv[1]) if n < 0: abs = -n else: abs = n print(abs) from sys import arg # absolute value n = -20 if n < 0: abs = -n else: abs = n print(abs) Get first parameter and convert it into an integer CS1022

11 Parameters from keyboard
“argv” allows us to read data from keyboard Data are parameters which we assign to variables It is possible to write programs without parameters We need to initialise variables in the program “initialise” = “give initial values” Every time we want to try different values we need to change these This is tedious... CS1022

12 Parameters from keyboard (2)
We need to tell Python we want to use parameters: CS1022

13 Parameters from keyboard (2)
And provide the actual value(s) CS1022

14 Defining functions By defining a function we can pass parameters from the interpreter line: def absolute(n): if n < 0: abs = -n else: abs = n return(abs) CS1022

15 Defining functions In the IDE: CS1022

16 Formatting your program
The editor helps you formatting your program Indentations are important! You still need to confirm what the editor does For instance, when the “if” command is finished CS1022

17 Program with a “for” loop
# sum first n integers n = int(argv[1]) sum = 0 for i in range(1,n): sum = sum + i print(sum) CS1022

18 Program with a “while” loop
Let’s write a program to count down from an input n to 0 We must use a while-loop! while condition: statement CS1022

19 Codecademy You would have been exposed to it by now...
It combines Python with “expected outcomes” Compares your program with an expected solution Errors confusing! Plan of topics Gentle, comprehensive Try both: IDE and Codecademy CS1022

20 Learning to learn How does one learn to program computers?
How should we teach programming? Going over syntax, pragmatics and “tricks of trade” Tedious, very time-consuming Different people have different learning pace/speed Hold back or rush people... Route we took: Expose you to basics of a programming language Support and guide you to learn it in your own pace Use existing on-line resources (we cannot compete with them!) This is an opportunity to “learn to learn” A “soft skill” for life Know what you know; know what you don’t know (AND do something about it!) Help is at hand if you have difficulty (demonstrators, lecturers) CS1022

21 What next? Work your way through lessons and materials on-line
Programming is a skill you get by doing Learn with your mistakes (and successes) The more mistakes, the more you learn! Breaking stuff is fun (no-one is hurt, no damages!) CS1022

22 Further reading Python’s official Web site https://www.python.org/
Wikipedia’s entry on Python Codecademy on-line Python training Python Humour! (who said we are not fun/funny?) Portable Python CS1022


Download ppt "CS1022 Computer Programming & Principles"

Similar presentations


Ads by Google