Download presentation
Presentation is loading. Please wait.
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 argv # 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
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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.