Professional Documents
Culture Documents
CSC1005F - Basics 1
CSC1005F - Basics 1
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
No error today!
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
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
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
CSC1015F 9
CSC1015F 10
10
Page 5
CSC1015F 16 March 2021
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
CSC1015F 13
13
Problem 1 – Haiku
Write a program to print out the following haiku:
Type, type, type away
No error today!
CSC1015F 14
14
Page 7