0% found this document useful (0 votes)
1 views

4a - Introduction to Python

This document serves as an introduction to the Python programming language, highlighting its popularity and applications in various fields such as business, scientific research, and artificial intelligence. It outlines the origins of Python, reasons for its widespread use, and provides guidance on getting started with Python, including installation instructions for Python and Anaconda, as well as creating simple programs using IDLE and Jupyter Notebooks. The document also includes activities for practicing basic programming skills in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

4a - Introduction to Python

This document serves as an introduction to the Python programming language, highlighting its popularity and applications in various fields such as business, scientific research, and artificial intelligence. It outlines the origins of Python, reasons for its widespread use, and provides guidance on getting started with Python, including installation instructions for Python and Anaconda, as well as creating simple programs using IDLE and Jupyter Notebooks. The document also includes activities for practicing basic programming skills in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to Python

© H. Chiname 2023. Version 05


Introduction
 This lecture introduces you to the Python programming
language.
 You will get to appreciate why Python is one of the most
popular programming languages today with wide
applications in areas that include, among others:
 Business;
 Scientific research;
 Engineering;
 Artificial intelligence; and
 Big data analysis.
 If you are using Python for the first time, you will also get
to create your first Python program.

© H. Chiname 2023. Version 05


Python: The Origins
 The Python programming language was created by
Guido van Rossum.
 He named the program after a favourite BBC show of
his – "Monty Python's Flying Circus".

© H. Chiname 2023. Version 05


Why Python?
 The following are twelve good reasons why Python is the
language of choice for learning programming, and especially so,
a first programming language:
 Python is easy to learn. Python programs are also some of the
easiest to read, write, and understand. Its syntax is simple.
 Python has some very powerful features built into the language.
 Python supports lots of programming styles from the very simple
structure programming, through to the state of the art Object
Oriented techniques.
 Python has a friendly and helpful user community. When you get
stuck, and call upon them, they are always ready to bail you out.

© H. Chiname 2023. Version 05


Why Python?
 Python is freeware. This means you can download and use
Python for free. There are also many free programs which
are fun and useful, which are written in Python. All these
you can download use and manipulate for free.
 Python is open source software. Part of what open source
means is that any user can extend Python (create things
that let you do more with Python, or do the same things
more easily).
 Python is a serious programming language many thousands
of professionals around the world, including programmers at
institutions like NASA and Google use Python every day.
Therefore, once you learn Python, you don’t have to switch
to another language; you only need to learn other languages
just to extend your boundaries of knowledge.

© H. Chiname 2023. Version 05


Why Python?
 Python runs on different kinds of computers. Python is
available for Windows PCs, Mac OS (Apple), and Linux,
for example. Most of the time, the same Python
program that works on your Windows PC at home will
work on the Mac at your workplace.
 Python is related to many other languages, which
means what you will have learned about programming
using Python, is easily transferable to other
programming languages, which makes learning new
programming languages easier for you.
 Python in an enjoyable program to learn and use.

© H. Chiname 2023. Version 05


Why Python?
 Python is a programming language not only for today, but
also for the future as well. It is not a dying programming
language; it is being actively developed, updated and used
for programming on a daily basis. For this reason, Python is
likely to be around for a very long time to come.
 For these reasons and more, Python has become a favourite
language for programming instruction for business courses,
at institutions around the world, including at Harvard.
 By the time you are through with your course, you might
have found other reasons of your own for learning and using
Python.

© H. Chiname 2023. Version 05


Getting Started
 To start using Python, the program has to be installed on
your computer.
 If you are using Linux, it is most likely that Python is already
installed on your computer.
 If you are using Mac OS, Python may or may not come
preinstalled. If not installed, you can install it using the
software discs that come with the system. It is always part
of the system files.
 If you are using MS Windows, it is most likely that you have
to install Python, because in most cases it does not come
pre-installed.
 Download Python from the Python website or from other
sites.

© H. Chiname 2023. Version 05


Choosing the Right Program to Install
 You have three options, either to install Python,
Anaconda, or both programs.
 If you choose to install Python program on its own:
 the installation is fast; and
 the program file is relatives small; but then
 You only have the option to use the Idle IDE; and
 install any packages or modules that you might need
later. This will give you the burden of knowing which
packages/modules to install, where to find them, and
how to install them.

© H. Chiname 2023. Version 05


Choosing the Right Program to Install
 If you choose to install the Anaconda program:
 the installation is slower; and
 You will not have to install any packages or modules
later;
 You have the option to use the Idle IDE, or Jupyter
Notebooks; but then
 The program file is relatively large.

© H. Chiname 2023. Version 05


Downloading and Installing Python
 The Python website is recommended. There, you will find
various versions of Python. Version 3.9 or a later upgrade of
Version 3 is recommended, The current latest version is
3.11:(https://www.python.org/downloads/)
 Choose the Windows version that suits your machine’s
specifications and download it.
 Once downloaded, double click to install.
 Do not uncheck any options when given the opportunity to
do so. You might need them.
 A Python installation guide is provided together with the
notes for this module.
© H. Chiname 2023. Version 05
Downloading and Installing Anaconda
 The Anaconda website is recommended:
(https://www.anaconda.com/download/)
 The latest edition appears on the default download
page. On that page, download the individual version, it
is available for free.
 Once downloaded, double click to install.
 An Anaconda installation guide is provided together
with the notes for this module.

© H. Chiname 2023. Version 05


Getting Started with Idle
 You can run Python from Windows command (DOS
Prompt) or from the shell (editor) that comes with
Python.
 The shell is recommended because, as you will see, it
has some functionalities that are not found on the
command line.
 For ease of access; you are better off setting up your
Python such that it is accessible from the start menu.
There you will be able to view both Python and the
editor (IDLE).

© H. Chiname 2023. Version 05


Activity 1: Creating Your First Python Program
Using Idle
 Once Python or Anaconda has been installed on your
machine you are now ready to create your first
program.
 Open Python in Idle and create a program such that if
you run it, it writes HELLO WORLD on the monitor.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle
 Search for the Python
program in the start menu.
 If you drill down on Python,
IDLE (with the version of
Python) should appear.
 Click on IDLE and the IDLE IDE
(Integrated Development
Environment) should open.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle

 The IDE should be titled Python with the version


number and the word “Shell” at the end of the
title line.
 Below this you should see the menu with the Tabs
File, Edit, etc.
 Click on File and select New File.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle

• A new window should pop up with the title “Untitled”


• Save the window using your first name as follows:
• x.py : where x is your first name, and add the
extension .py after the dot.
• Then type the following in the work area:
• print(‘Hello World’)
• Take note of the syntax highlighting (automatic text
colours) if you have correctly followed the instructions.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle
• If you have correctly
run your program, this
is what it should look
like before you run it.

• And this is the output


that you should get
after running.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle
 Once again save your program.
 Click on the Run Tab in the menu and select “Run
Module”.
 If you have done the correct thing, your Python should
switch back to the shell and print the output of the
program.
 Congratulations on creating your very first successful
program!
 The program does not do anything spectacular, except print
back Hello World, but it still ticks all the important boxes
of a proper Python program all the same.

© H. Chiname 2023. Version 05


Activity 1 Solution: Creating Your First Program
Using Idle
 Take note how we opened a new window in the shell.
 Also take note how we saved the untitled window that we
created.
 Note the use of parentheses (rounded brackets) and
quotation marks around the words Hello World. This is
essential and not optional.
 Take note how we saved the program again after editing.
 And then how we finally made the program run.
 You will have to get used to these steps because this is
how we will always create all our programs when using
Idle.

© H. Chiname 2023. Version 05


Activity 2: Creating Your First Python Program
Using Jupyter Notebooks
 Once Python or Anaconda has been installed on your
machine you are now ready to create your first
program.
 Open Python in Idle and create a program such that if
you run it, it writes HELLO WORLD on the monitor.

© H. Chiname 2023. Version 05


Activity 2 Solution: Creating Your First Program
Using Jupyter Notebooks
 Search for the Anaconda
program in the start menu.
 If you drill down on
Anaconda, Jupyter Notebooks
should appear.
 Click on it and Jupyter will
open, first through Anaconda
Prompt (a command line
screen), then into your
default web browser.

© H. Chiname 2023. Version 05


Activity 2 Solution: Creating Your First Program
Using Jupyter Notebooks

 From here, you can navigate to a folder of your choice,


and there, you will either:
 Find an existing Notebook and open it, or
 You can create a new Notebook by drilling down on New and
then clicking on Python 3.
© H. Chiname 2023. Version 05
Activity 2 Solution: Creating Your First Program
Using Jupyter Notebooks

• If you created a New Notebook, a new blank Notebook


appears, with the title “Untitled”
• Save it using the Course Code by clicking on File  Rename:
• x.ipynb : where x is the Course Code. The file extension
(.ipynb) will be automatically added.

© H. Chiname 2023. Version 05


Activity 2 Solution: Creating Your First Program
Using Jupyter Notebooks
• Then type the following
inside the cell:
• print(‘Hello World’)
• Take note of the syntax
highlighting. The default
colours are different from
those in Idle.
• If you have correctly run your
program, this is what it
should look like before you
run it.
• And this is the output that
you should get after running.

© H. Chiname 2023. Version 05


Activity 2 Solution: Creating Your First Program
Using Jupyter Notebooks
 Congratulations on creating your very first successful
Python program in Jupyter Notebooks!
 You will have to get used to these steps because this
is how we will always create all our programs when
using Jupyter Notebooks.

© H. Chiname 2023. Version 05


Activity 3: Experimenting with Changes to Your
First Program
 For you to be satisfied that the approach we used in
Activity 1 is the best to create a program, make the
following changes to the “Hello World” program.
 Remove the parenthesis around “Hello World”, then
save, and run the program again.
 What was the outcome?
 Now, add back the parenthesis that you removed and
this time remove the quotes around the “Hello World”,
then save, and run the program again.
 What was the outcome?

© H. Chiname 2023. Version 05


Activity 3: Experimenting with Changes to Your
First Program
 Now, add back the quotes that you removed and this
time replace the letter “p” in the command print with
a capital letter (P), save, and run the program again.
 What was the outcome?
 Now, correct the letter “p” that you had changed, and
this time run the program without saving it.
 What was the outcome?

© H. Chiname 2023. Version 05


Activity 4: Adding the File Name
 Now change your program so that you add the file
name before the print command.
 Then run the program again.

© H. Chiname 2023. Version 05


Activity 4 Solution: Adding the File Name
 # Filename: x.py
 print(‘Hello World’)

 COMMENTS
 Take note of the hash sign before and the colon that comes
after the word Filename.
 Also notice the .py extension after the name of the file.
 In Python, the hash is reserved for comments.
 A comment is a string typed in a program which will be
ignored by the program when it is run.
 A full explanation of comment is the subject of a separate
lecture.
© H. Chiname 2023. Version 05
Activity 4 Solution: Adding the File Name
 For all the programs that you will create, the Filename
should always, always be the first line that you type in
your program!
 Take note how the comment appears in a red font
colour.
 Also notice how Python prompts you to save your
program if you try to run it without first saving it.
 Constantly saving your work as you create your
programs cannot be stressed enough. It is a key
component of programming skills!

© H. Chiname 2023. Version 05


Activity 5: Saving Programs
 So far you have saved all your programs created in Idle
in the default Python folder.
 Now save your program again by changing the location
for saving to documents.

© H. Chiname 2023. Version 05


Activity 5 Solution: Saving Programs

© H. Chiname 2023. Version 05


Activity 4 Solution: Saving Programs

• To change the location in which your program is


saved, you will need to do this when the Save or
Save As dialogue box appears, by drilling down on
Save In to select the appropriate location.
• This way you can even be able to save your program
on a flash disk.
• Take note that for examination or assignment
purposes every program that you create and save
is supposed to be saved as follows:

© H. Chiname 2023. Version 05


Activity 4 Solution: Saving Programs

 Student number first;


 Then the Underscore (_);
 Then Question number or assignment number
 Lastly the program extension (in this case, the Python or
Notebooks extension).
 No spaces in between.
 As examples:
 N0000001_Q2.py; or
 N0000002_ASS02.ipynb
 This is the required convention.
 Failure to master and follow this convention is at your own
risk.
© H. Chiname 2023. Version 05

You might also like