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

lecture1

Uploaded by

wong oscar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lecture1

Uploaded by

wong oscar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

2024 – 2025 Semester I

COMP1012 Programming Fundamentals and Applications


Lecture 01 (September 7, 2024)

Dynamic

HLL
Introduction to Python
Versatile
Why do we need programming?

COMP 1012 Slide 02


Semester I (2024 – 2025)
Whistle
(animals)

COMP 1012 Slide 03


Semester I (2024 – 2025)
Calculators vs Programming Languages

5 x 10 ?
7315 x 2 ? The Prediction/Data Analysis:
Q.1. What will be the temperature on 11th Sep?

7615 x 987 ?
Q.2. Will it rain
Q.3. Can Python be on 11th Sep?
used as Calculator?

COMP 1012 Slide 04


Semester I (2024 – 2025)
Why learn Python?

COMP 1012 Slide 05


Semester I (2024 – 2025)
The History of Python
Who – Guido Van Rossum
between 1989 – 1991 at Centrum
Wiskunde & Informatica (CWI),
Netherlands.

Why – Guido
was watching
a show by name
Monty Python’s
Circus when
working on this
language and
thought why not
name it as Python.

COMP 1012 Slide 06


Semester I (2024 – 2025)
Simple and
Easy to
Learn

Extensible
Platform
and
Independent
Embeddable

Portable and
Benefits of Python Robust

Free and Rich Library


Open Source Support

Interpreted
(bytecode-
compiled)

COMP 1012 Slide 07


Semester I (2024 – 2025)
Applications of Python

137000 libraries
Python has a vast and continuously growing ecosystem of libraries. The total
numbers of Python are more than 137000 libraries. All these libraries are used
in machine learning, data science, data manipulation and visualization, and more.

COMP 1012 Slide 08


Semester I (2024 – 2025)
COMP 1012 Slide 09
Semester I (2024 – 2025)
“Hello World” – Python vs Others
C Program Java Program Python Program

Fortran Program Pascal Program BASIC Program


program helloworld program HelloWorld(output); 10 PRINT "HELLO, WORLD!"
print *, "Hello, World!" begin
end program helloworld WriteLn('Hello, World!')
end.

COMP 1012 Slide 010


Semester I (2024 – 2025)
Python Installation Options

Option A Option B Option C


Installation on your PolyU Labs: Online Python IDE:
personal laptop Room# M409
PyCharm | Jupyter | https://www.onlinegdb.com/
https://www.python.org/
Visual Studio
if own_notebook(student_id):
BYON()
# Bring Your Own Notebook
else:
option_C()
COMP 1012 Slide 011
Semester I (2024 – 2025)
Python Installation – On Your Computer Option A

> Install Python on your computer. https://www.python.org/downloads/


• Newest stable version is 3.12.5.
• We cover standard Python features, so most versions 3.x would be sufficient.

COMP 1012 Slide 012


Semester I (2024 – 2025)
Python Installation – Windows Option A

> Install as shown in the figure below


• Add Python.exe to PATH, so that you can use “python” in the terminal (just in case)

Step 2: Click Install Now

Step 1: Check all these options

COMP 1012 Slide 013


Semester I (2024 – 2025)
Python Installation – Windows Option A

> Install as shown in the figure below


• Add Python.exe to PATH, so that you can use “python” in the terminal (just in case)

COMP 1012 Slide 014


Semester I (2024 – 2025)
Python Installation – Windows Option A

> Now, we can use Python in the terminal


• Click “Start”, or Press “Win” on your keyboard
• Enter “cmd”↙ (for win 7~11) or “terminal”↘ (recommended for win 11) or “powershell”

This black window This black window


is called terminal / is called terminal /
command prompt command prompt

COMP 1012 Slide 015


Semester I (2024 – 2025)
Python Installation – MacOS Option A

> Click the downloaded


installation package
• Press “Continue” or
“Agree” until “Install”

COMP 1012 Slide 016


Semester I (2024 – 2025)
Python Installation – MacOS Option A

> Now, we can use Python in the terminal


• Press “Command+Space” ↓ or Click “LaunchPad”↗, enter “terminal”. Enter “python3” ↓.

COMP 1012 Slide 017


Semester I (2024 – 2025)
Python Installation – MacOS Option A

> If you want to enter “python” instead of “python3”


• First exit the Python interpreter by typing “exit()”
• vim .zprofile (or vim .bash_profile, see which one do you have)
• Add “alias python=python3” and save (by pressing “esc” and typing :w (to save) and :q (to quit)).
• Reopen the terminal, now you can use “python” instead of “python3”

COMP 1012 Slide 018


Semester I (2024 – 2025)
Congratulations,
Python Programmer!

012-S01/S02/S04 COMP1012 Slide 019


Fall, Semester I (2024 – 2025)
Starting IDE / IDLE Option A

>You can find a few options once installing Python.


• IDLE (Integrated Development and Learning Environment)
• Command line
• Doc
• Manual
>IDLE is an integrated development environment for Python.
• This is more preferred than command line, especially for beginners.
• Once started, you will see the standard Python prompt.
• You can try out Python there!

COMP 1012 Slide 020


Semester I (2024 – 2025)
Using IDLE Option A

>After invoking IDLE, you can now test out Python


>Try these:
• Type 4+3-2*1
• Type 99*99
• Type 99**99
• Type 99**999
• Type print("Hello, World!")
• Type name = input("What is your name? ")
• Type print(name)
• Type print("My name is",name)
** means raising to the power –
Python is a very powerful calculator!

COMP 1012 Slide 021


Semester I (2024 – 2025)
Programming in IDLE Option A

Your first Python program can be created


by using the editor within IDLE.
Under File, select New File. You will see a
new window for you to enter your Python
program.
Type your hello world or any other Python
program. Hint on syntax would be provided.

COMP 1012 Slide 022


Semester I (2024 – 2025)
Programming in IDLE Option A

Save it by selecting Save As under File.


Let us save it on Desktop, and call it
demo.py, for example.
Note that all Python programs should be
stored with the extension .py.
To run the program, select Run Module
under Run, or just hit the function key
<F5> for simplicity.
You will see the output from the
program, i.e. Hello world.
You can modify the program and hit
<F5> to run the revised program.
COMP 1012 Slide 023
Semester I (2024 – 2025)
Coding in Python terminals
looks cool but it is boring too,
isn’t it?

COMP 1012 Slide 024


Semester I (2024 – 2025)
Running Python Online Option C

> Official platform to be used for assignment and project submission.


• There are so many versions of Python around. https://www.onlinegdb.com/
• A standard version is needed.
• You can use any Python platform in your future study in different subjects.

COMP 1012 Slide 025


Semester I (2024 – 2025)
Running Python Online Option C

>You can see a sample program.


>Do you know what language is it?
It is in C.

>Change to Python (Python 3)

COMP 1012 Slide 026


Semester I (2024 – 2025)
Running Python Online Option C

COMP 1012 Slide 027


Semester I (2024 – 2025)
Running Python Online Option C

> What does this do?

Which
game is
this?

COMP 1012 Slide 028


Semester I (2024 – 2025)
Running Python Online – Downloading Option C

May be downloaded
directly and stored
under system
Downloads folder

COMP 1012 Slide 029


Semester I (2024 – 2025)
Running Python Online – Uploading Option C

Your program MUST be named


as main.py before upload.

The existing main.py program will


be replaced by your new main.py.

COMP 1012 Slide 030


Semester I (2024 – 2025)
What will be in Lab 01?

COMP 1012 Slide 031


Semester I (2024 – 2025)
Modules, Comments and pip
 A module is a file containing code written by somebody else
which can be imported and used in our programs
 Built in modules (pre-installed) like os, sys, etc, for import.
 External modules (need to be installed using pip)

 pip is the package manager for Python (e.g. pip install


numpy)
 Comments are used to write something which the programmer
does not want to execute
 Single line comments using #
 Multi-line comments using ''' some comment ''' or
""" other comment """
Is p1_game.py a module?

012-S01/S02/S04 COMP1012 Slide 032


Fall, Semester I (2024 – 2025)
Every Python program you write and store in a .py file is a module. In Python, a

Function
module is simply a file containing Python code. This code can include functions, Will be
classes, variables, and runnable code. When you save your Python code in a .py discussed
file, it becomes a module that can be imported and used in other Python programs. in detailed
in Lec 10
Example: Consider you have a file named This file example.py is a module. You can import it into Module and 11
example.py with the following content: another Python script or module and use its functions:

Package

Library
Package is a collection of modules. That means, the package is a directory that
contains collection of modules and a collection of packages or sub-packages
Framework
Library is collection of packages and frameworks are collection of libraries

Slide 033
Do we other IDE’s to install modules?
IDE stands for Integrated Development Environment
>Pycharm
>IDLE
>Jupyter Notebook
>VS Code

An IDE is like a well-equipped kitchen for a chef, providing all the necessary tools and facilities to efficiently create, test, and manage
software projects. Just as a chef uses various kitchen tools to prepare a meal, a programmer uses an IDE to develop software.

While you can write and run Python code using a text editor and the command prompt, an IDE offers a comprehensive set of tools and
features that can significantly improve your development workflow. From code editing and debugging to project management and
collaboration, an IDE provides a more efficient and productive environment for software development.

012-S01/S02/S04 COMP1012 Slide 034


Fall, Semester I (2024 – 2025)
Installing VS Code on Windows
• Suppose that Python has been install on your Windows
• Download installation package (link)

COMP 1012 Slide 035


Semester I (2024 – 2025)
Installing VS Code on Windows

COMP 1012 Slide 036


Semester I (2024 – 2025)
After Installing VS Code
on Windows & MacOS

• Step 01 – Click on extensions


and download python extensions
• Step 02 – ctrl + n = To open the New file
• Step 03 – ctrl + s = To save the file (as name.py)
• Step 04 – Write the program in the file
• Step 05 – Run the program

012-S01/S02/S04 COMP1012 Slide 037


Fall, Semester I (2024 – 2025)
Where is a Terminal in VirtualStudio?

012-S01/S02/S04 COMP1012 Slide 038


Fall, Semester I (2024 – 2025)
How to know if I am outside
of a python environment ?

For windows users: If you see single > in your terminal this means For MacOs users: If you see single % in your terminal this means you
you are outside the python environment and you can install the packages are outside the python environment and you can install the packages

012-S01/S02/S04 COMP1012 Slide 039


Fall, Semester I (2024 – 2025)
How to know if I am inside
of a python environment ?

For windows users: If you see this prompt >>> in your terminal this For MacOs users: If you see this prompt >>> in your terminal this
means you are inside the Python environment. means you are inside the Python environment.

012-S01/S02/S04 COMP1012 Slide 040


Fall, Semester I (2024 – 2025)
Practice Set 1
Use IDLE / REPL (Read-Evaluate-Print-Loop) and print the table of 5 using it.

COMP 1012 Slide 041


Semester I (2024 – 2025)
Practice Set 2

Write a program to print the


nursery rhyme “twinkle
twinkle little star” in Python

Twinkle, twinkle, little star,


How I wonder what you are!
Up above the world so high
Like a diamond in the sky.

COMP 1012 Slide 042


Semester I (2024 – 2025)
Solution 1
print(''' Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky. ''')

print("Twinkle, twinkle, little star, \n\tHow


Solution 2

I wonder what you are! \n\t\tUp above the


world so high \n\t\tLike a diamond in the
sky. \nTwinkle, twinkle, little star, \n\tHow
I wonder what you are!")

012-S01/S02/S04 COMP1012 Slide 043


Fall, Semester I (2024 – 2025)
Installing packages
Step 1 Step 2
For installing packages or libraries first exit the
pip install package_name
Python environment by typing exit()
pip install pyttsx3
If you see this prompt >>> in your terminal this For Windows users
pip install pyjokes
means you are inside the Python environment. use this command

For windows users: If you see single > in your


terminal this means you are outside the python
environment and you can install the packages
pip3 install package_name
For MacOs users: If you see single % in your
For MacOs users pip3 install pyjokes
terminal this means you are outside the python use this command
environment and you can install the packages

012-S01/S02/S04 COMP1012 Slide 044


Fall, Semester I (2024 – 2025)
Practice Set 3
Install an external module (say pyttsx3) and
use it to perform an operation of your interest.

import pyttsx3
engine = pyttsx3.init()
engine.say("Twinkle Twinkle Little Star")
For Windows users
use this program
engine.runAndWait()

from os import system


def my_text(text):
system("say {}".format(text))
For MacOs users
use this program text="twinkle twinkle little star"
my_text(text)

012-S01/S02/S04 COMP1012 Slide 045


Fall, Semester I (2024 – 2025)
Joke Time! Installing modules using pip
Step 1: Open the terminal
Step 2: pip install pyjokes
Practice Set 4

Step 3: WAP to print a joke


import pyjokes
joke = pyjokes.get_joke()
print(joke)
What is the output of this program?
Will the output be same if you run this
program twice? Why and why not?
012-S01/S02/S04 COMP1012 Slide 046
Fall, Semester I (2024 – 2025)
Joke Time! Installing modules using pip

COMP 1012 Slide 047


Semester I (2024 – 2025)
Other Useful Tools
>Anaconda
• A python environment management tool
• Contain may packages and the useful IPython, jupyter notebook, and Spyder
• conda create -n ENV_NAME # create a virtual env named ENV_NAME
• conda activate ENV_NAME # activate the virtual env named ENV_NAME
• conda deactivate # deactivate
• conda install PACKAGE_NAME1 PACKAGE_NAME2 # install packages
• conda update PACKAGE_NAME1 PACKAGE_NAME2 # update a package
• conda list # show all install packages
• conda info -e # list all virtual environments in the computer
• Only for experts or semi-experts
https://www.anaconda.com/products/distribution

COMP 1012 Slide 048


Semester I (2024 – 2025)
Installing Anaconda
> Download installation package (link)

COMP 1012 Slide 049


Semester I (2024 – 2025)
Python vs Anaconda

Anaconda is the heaviest and biggest snake in the world


(and dangerous).
Python is the longest snake in the world (and can be a pet).
COMP 1012 Slide 050
Semester I (2024 – 2025)
Q and A

COMP 1012 Slide 051


Semester I (2024 – 2025)

You might also like