Lecture 8 Introduction To Python 2021
Lecture 8 Introduction To Python 2021
Introduction to Python
Alex Kuhudzai
alexkuhudzai@gmail.com
Cape Peninsula University of Technology,
Cape Town, South Africa
Ask
The art and science of asking questions is the source of all
knowledge.
- Thomas Berger
Do not hesitate to ask!
If something is not clear, stop me and ask.
During exercises (you can also ask others).
1. IPython
Python can be run interactively
Used extensively in research
2. Python scripts
What if we want to run more than a few lines of code?
Then we must write text files in .py
Time for a demo..
https://www.youtube.com/watch?v=BBwEF6WBUQs
Noteable (Jupyter notebooks)
Easy to use environment
Web-based
Combines both text and code
into one
Come with a great number of
useful packages
Noteable (Jupyter notebooks)
Easy to use environment
Web-based
Combines both text and code
into one
Come with a great number of
useful packages
Download the Python 3 Installer
Windows doesn’t typically come with a system Python. Fortunately,
installation involves little more than downloading and running the
Python installer from the Python.org website.
Step 1: Download the Python 3 Installer
If your system has a 32-bit processor, then you should choose the 32-bit installer. If you
aren’t sure if your computer is 32-bit or 64-bit, stick with the 64-bit installer mentioned
above.
Step 2 : Run the Installer
Open your Downloads folder in Windows Explorer and double-click
the file to run the installer. A dialog that looks like the following one
will appear:
Open IDLE
You can open the IDLE in two steps:
Click the start menu and locate the Python 3.9 folder
Open the folder and select IDLE (Python 3.9)
The python shell window looks like below
The fun stuff
We are now going to write our first python program
Learn what happens when you run a program with an error
Learn how to declare a variable and inspect its value
Learn how to write comments.
On the first line, you create a variable named greeting and assign it the value
"Hello, World" using the = operator.
print(greeting) displays the output Hello, World because Python looks
for the name greeting, finds that it’s been assigned the value "Hello,
World", and replaces the variable name with its value before calling
the function.
If you hadn’t executed greeting = "Hello, World" before executing
print(greeting), then you would have seen a NameError
How to write a comment
The most common way to write a comment is to begin a new line in your code with the # character.
When you run your code, Python ignores lines starting with #.
Comments that start on a new line are called block comments. You can also write inline
comments, which are comments that appear on the same line as the code they reference. Just put a
# at the end of the line of code, followed by the text in your comment.
Here’s an example of a program with both kinds of comments: