0% found this document useful (0 votes)
58 views77 pages

Slides WK1

hwww

Uploaded by

ricky14625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views77 pages

Slides WK1

hwww

Uploaded by

ricky14625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

CB2240:

Introduction to Business
Programming in Python

1
Instruction team
David Xu (S01) Teaching Assistants
Office hours: By appointment LAU Man Kit: [email protected]
Office: LAU-6-246 HUO Xuenan: [email protected]
Tel: 3442 8530 XIAN Chunyi: [email protected]
Email: [email protected] SHI Zhuzhi: [email protected]

Zhe Wang (S02, S03)


Office hours: By appointment
Office: LAU-6-276
Tel: 3442 8815
Email: [email protected]

2
About Me
• Assistant Professor at the Department of Information Systems

• Background
• Undergraduate in Supply Chain Management
• PhD in Information Systems

• Speak Mandarin and English


• Current research uses Python and Mathematica

3
Class goals
• Understand the basics of Python.

• Learn some business applications of Python.

• Learn how to think like a programmer.

4
Course overview
• It is easy to learn and fun to use Python.

• Students without programming background are perfectly


fine.

• Students' skill set upon course completion should include


implementing decision-making structure, repetition
structure, function procedures, among others.

5
Course overview
• Python is a popular open source programming language
widely used by business professionals to develop
applications in big data, artificial intelligence, financial
technology, and so on.

• This course provides an introduction to the Python


language and helps students master its fundamentals and
apply them in different contexts to solve business
problems.

6
Key topics of CB2240
• Basics:
• Numbers, strings, list, tuples.
• if statements, for and while loops, functions, etc.

• Applications
• Data handling, data visualization, etc.

• Textbook: David I. Schneider, An Introduction to Programming Using


Python, 1st edition, Pearson, 2016.
• Reading: Eric Matthes, Python Crash Course, 3rd Edition: A Hands-
On, Project-Based Introduction to Programming 3rd Edition, 2016

7
Module Topic Reading
1 Introduction Overview Chapter 1
2 Core objects Numbers 2.1
3 Strings 2.2
Schedule

4 Lists and tuples 2.4


5 Conditionals and Conditions and if statements (1) 3.1, 3.2
(S02)

6 loops Conditions and if statements (2) 3.1, 3.2


7 for loops 3.4
8 while loops 3.3
9 Functions and Functions I 4.1
10 program design Functions II, recursion 4.2, 4.3, 6.4
11 Public holiday (April, 01) Chapter 7
11 Object-oriented programming (March, 23) Chapter 7
12 Data handling Data processing 5.1, 5.2
13 Course Summary Review and summary

8
Module Topic Reading
1 Introduction Overview Chapter 1
2 Core objects Numbers 2.1
3 Strings 2.2
4 Lists and tuples 2.4
Schedule
(S01, S03)

5 Conditionals and Conditions and if statements (1) 3.1, 3.2


6 loops Conditions and if statements (2) 3.1, 3.2
7 for loops 3.4
8 while loops 3.3
9 Functions and Functions I 4.1
10 program design Public Holiday (March, 29) 4.2, 4.3, 6.4
10 Functions II, recursion (March, 23) 4.2, 4.3, 6.4
11 Object-oriented programming Chapter 7
12 Data handling Data processing 5.1, 5.2
13 Course Summary Review and summary

9
Advanced classes
• IS3240 Advanced Python (sample topics: NumPy, Pandas, matplotlib).

• IS4861 Machine Learning for Business.

• Computer science classes

10
Weekly routine
• Lecture (≈1 hour)
• Quiz (10 minutes)
• Basic concepts and examples

• Lab (≈ 2 hours)
• Examples
• Classwork

• Homework
• Take-home

11
Assessment
• Class Participation: 5%
• Piazza Participation: 5%
• Classwork: 10%
• Homework: 20%
• Quizzes: 20%
• Final Exam: 40%

• For the weekly class participation, classwork, homework, and quizzes, the top 10
out of 13 scores in each category will count toward the final grade.

12
Weekly classwork & homework
• Deadlines:
• 4:00 pm on Thursday in the week (S02)
• 4:00 pm on Monday in the following week (S01, S03)

• Late submission will result in point deductions.


• For each hour after the deadline, the submission will lose 10% of the maximum
possible score.

13
Piazza interface

14
Piazza participation
• Post all Python questions on Piazza (NOT via emails). Q&A on Piazza (accessible
via Canvas) will count towards the participation score.
• Your questions will be valuable to your classmates.
• Answering questions will help others and yourselves.

• Both raising and answering questions count.


• Quality questions and answers carry more weight.

• You can post anonymously (your classmate can’t see your name, instructors still
can).

• Please be respectful to your peers in the exchanges.

15
Communication policy
• If you have questions about class logistics, feel free to email us.
• All Python questions should be posted on Piazza.

• What the instructor & TAs won’t do: debug programs for you.
• You can post error messages on Piazza.

16
8

Some ground rules


• Actively participate
• Be prepared
• Be punctual & don’t miss the quiz
• Be professional
• Emails include “CB2240” in subject line
• Include greeting and signature
• Include session number
What I expect from you
• Be professional

• Be consistent

• Be engaging

18
Weekly quiz on Canvas
• Weekly quizzes will test your knowledge about content
from the previous week.
• Review before coming to class.

• Starting from week 2, the quizzes will only be available on


Canvas at the beginning of each lecture.

• Let’s work on Quiz 1 (survey) now on canvas.


• Quiz 1 is graded based on completion.
• Given course add/drop, no penalty for late submission of Quiz 1.

19
Lecture 1.
An Introduction to Computing
and Problem Solving

20
Why programming?
• Programming allows us to

• Communicate with computers and machines.

• Harness computing powers; automate tasks.

• Tap into machine learning (ML) and artificial intelligence (AI).

21
Why programming?
• Today, almost every profession needs programming to
some extent.
https://www.youtube.com/watch?v=Dv7gLpW91DM

22
Real world application example
• Walmart and Amazon scrape each other’s product data.

23
Why Python?
• Large and still growing
Python community

24
Why Python?
• Powerful, multi-purpose
• Accounting
• Finance (e.g. quant, trader)
• Digital marketing
• Supply chain analytics
• Academic research
• Etc.

• Huge demand in the job market

25
Why Python?
• Beginner-friendly

• Plain English

• Vast open-source libraries

• Python

26
About Python
• Python is named after the British comedy group Monty Python.

• Python is a high-level programming language.

27
A simple program
• What is an example of a program?

28
Problem Solving

• Examine a small problem


• Please identify the largest number from the
following list:

18 23 53 08

• Most of you know what the answer is, but


how did you arrive at this answer?

29
Problem Solving
• (Step 1) Write down the first number on a
piece of paper (largest so far and is the
largest if it is the only number)
• (Step 2) Repeat the following steps until no
more numbers:
• (a) Take the next number from the list
• (b) Compare the two numbers, if the new
number is bigger it replace the old number on
the paper
• (c) Return to the first step
• After step 2, the last number of the paper is
the answer.

30
The problem-solving process

31
Questions and Answers
• Where will new programs be saved?
• Create a special folder to hold your programs
• Where can I research questions I have about Python?
• Documentation at https://www.python.org/doc/
• W3schools https://www.w3schools.com/python/

32
Program Planning and Tools

33
Program Planning
1. Analyze: Define the problem.
2. Design: Plan the solution to the problem.
3. Code: Translate the algorithm into a programming
language.
4. Test and correct: Locate and remove any errors in the
program.
5. Complete the documentation: Organize all the material
that describes the program.

34
Algorithm Development
Algorithm to determine the number of stamps for a letter

• Rule of thumb: 1 stamp for every 5 sheets of paper


1. Request sheets of paper
2. Divide by 5
3. Round quotient up to next whole number
4. Reply with the number of stamps

35
Problem Solving for Stamps

36
Think before you code
• Flowcharts

• Pseudocode

• Hierarchy charts

37
Flowcharts

38
Example
Flowchart

39
Pseudocode
• Abbreviated plain English version of actual computer code

• Symbols used in flowcharts replaced by English-like statements

• Allows programmer to focus on steps required to solve problem

40
Example Pseudocode

41
Hierarchy Chart
• Shows the overall program structure

• Depict organization of program, omit specific processing logic

• Describe what each part, or module, of the program does

• Each module subdivided into a succession of submodules

42
Example Hierarchy Chart

43
Three structures
• Sequence structure

• Decision structure

• Repetition/loop structure

44
Sequence structure
• The postage-stamp problem has a sequence structure:
• Moved from one line to the next without skipping any lines.

45
Decision Structure

46
Direction of Numbered
NYC Streets Algorithm
• Problem: Given the street number of a one-
way street in New York City, decide the
direction of street - eastbound or westbound.

• In NYC
• Even-numbered streets: eastbound
• Odd-numbered streets: westbound

47
Direction of Numbered NYC Streets
Algorithm
• Input: Street number.

• Processing: Decide if the street number is


divisible by 2.

• Output: “Eastbound” or “Westbound”.

48
Street Direction Algorithm

Flowchart for the


numbered
New York City
streets problem.

49
Street Direction Algorithm

Pseudocode for the numbered


New York City streets problem.

50
Street Direction Algorithm

Hierarchy chart for the numbered


New York City streets problem.

51
Repetition Structure
• A programming structure that executes instructions many times.

• Need a test (or condition) to tell when the loop should end
• Check condition before each pass through loop

52
Repetition Structure

Pseudocode and flowchart for a loop.


53
Dating algorithm

Are
you
single
?
While you are single
Look for a partner

Look for a
partner

54
Class Average Algorithm
• Problem: Calculate and report the average grade for a class.

• Discussion: Average grade equals the sum of all grades divided by


the number of students.
• Need loop to read and then add (accumulate) grades for each student in
the class.
• Inside the loop, we also need to total (count) the number of students in class.

55
Class Average Algorithm
• Input: Student grades.
• Processing: Find the sum of the grades;
count the number of students; calculate
average grade = sum of grades / number of
students.
• Output: Average grade

56
Class Average
Algorithm

57
Class Average Algorithm

Pseudocode for the class average problem.


58
Class Average Algorithm

Hierarchy chart for the class average problem.

59
Kahoot
Classroom projector Student laptop/cellphone

60
Kahoot
• Go to https://kahoot.it/

• Enter the game PIN as shown on the classroom projector.

• Enter your 8-digit student ID, E.g., 55123456


Do NOT use names.

• Kahoot is separate from the weekly quiz on Canvas. Kahoot


engagement (not merit) will partially determine class participation
scores.

61
Lab 1

62
Objectives
• Install python

• Save & run programs

• Good programming practices

• The print, input functions

63
Python installation
• Install Python
• https://www.python.org/downloads/

• Versions:
• Some operating systems come with earlier versions of Python by default.
• All material in this class will be based on Python 3.12.1.

64
Open IDLE
• After installation, open IDLE.
• IDLE stands for Integrated DeveLopment Environment

65
Hello World!

66
The print function

67
Good programming practices
• There are a number of symbols in programming languages which
work in pairs, e.g., parentheses () and quotations marks “”.

• To prevent omission of the closing pair, it is a common practice for


programmers to complete the pair first, before filling in the contents.

68
The Zen of Python, by Tim Peters

69
Editor

70
Save program

71
Run program output

72
Documentation
• Document what you did in your
program so that it will be more
understandable by other people
or by yourself in the future.

• Use # for comments in Python.

• Python will skip the red part when


running the program.

73
The input function

74
Errors
• Don’t panic.

• Read the error message.

• Find the corresponding line and correct it.

75
Show line numbers

76
Remember to submit Classwork 1 and Homework 1.
Review for Quiz 2.

77

You might also like