Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CSC1015F 16 March 2021

Python –
Basics

James Gain
Department of Computer Science
University of Cape Town
jgain@cs.uct.ac.za

Problem 1 Introduction
Write a program to print out the following haiku:
Type, type, type away

Compile. Run. Hip hip hooray!

No error today!

(By Samantha W.)

CSC1015F 2

Page 1
CSC1015F 16 March 2021

Programs
A program is a set of instructions given to a
computer, corresponding to an algorithm to solve a
problem
The act of writing a program is called programming
Programs are written in a precise language called a
programming language

CSC1015F 3

Sample Program
Sample Program (in Python):
letters = [32, 85, 67, 84]
for a in [70364627,43298,432932769,43298645]:
while a>0:
print (chr(letters[a & 3]),end="")
a = a >> 2
print ()

CSC1015F 4

Page 2
CSC1015F 16 March 2021

Process of Program Execution


Programs work as follows:
Ingest information from the real world (input)
Process data internally
Send computed data back to real world (output)
Because of different input, each time a program
executes the results can be different

CSC1015F 5

Python
There are many different types of computer N
A
languages R
RT
This course is based on Python FO COBO
A general-purpose interpreted programming
L
language invented in the 1980s/1990s by Guido C
van Rossum C++
Named after Monty Python’s Flying Circus
JA C#
A good first language to learn because of its VA
simple elegance n
t ho
Py
CSC1015F 6

Page 3
CSC1015F 16 March 2021

How We Program in Python


We write programs, stored in text files
Each program is a set of instructions that the Python
interpreter will execute when the program is executed by
the user
We often do both of these things in an Integrated
Development Environment (IDE)
We can also use the interactive interpreter to run short programs
while testing our ideas
Later, we will neaten our code into blocks called functions
Python is an OOP language but we will not use this
CSC1015F 7

Python Interpreter
Python programs are run one instruction at a time by the
interpreter
We can run the interpreter (usually “python3”) and then
enter instructions one at a time
Python will execute each instruction then prompt the user for
the next one
Enter exit() to end

CSC1015F 8

Page 4
CSC1015F 16 March 2021

Integrated Development Environment (IDE)


Graphical interface with menus and windows, much like a
word processor
We recommend WingIDE 101
Free, scaled-down Python IDE designed for use in teaching
introductory programming classes
You are welcome to use any other tools – e.g. IDLE
Even a separate text editor and interpreter if you like doing things
the hard way

CSC1015F 9

Python program files


The interpreter is useful for testing code snippets and
exploring functions and modules
However, to save a program permanently we need to write
it into a file and save the file
Python files are commonly given the suffix “.py”
e.g. HelloWorld.py
Always save your programs while you are working!

CSC1015F 10

10

Page 5
CSC1015F 16 March 2021

Skeleton Python Program


# what the program does
# author of program
# date

PythonInstruction1
PythonInstruction2
PythonInstruction3
PythonInstruction4
CSC1015F 11

11

Example Program
helloworld.py:
# first program ever
# billgates
# 14 march 1968
print(“Hello World”)

output:
Hello World
CSC1015F 12

12

Page 6
CSC1015F 16 March 2021

What does it mean?


print(“Hello World”)

print is the name of a function


“Hello World” is the argument or value sent to this function
A function is a common task that we can reuse.
Printing on the screen is quite complicated
So we reuse Python's built-in function to make it easier
In a few weeks we will also create our own functions

CSC1015F 13

13

Problem 1 – Haiku
Write a program to print out the following haiku:
Type, type, type away

Compile. Run. Hip hip hooray!

No error today!

(By Samantha W.)

CSC1015F 14

14

Page 7

You might also like