Programming Fundamentals with Python
Chapter 1 - Why Program?
Le The Anh
[email protected]
Why should we learn to write program?
Computers are built for one purpose - to do things for us
But we need to speak their language to describe what we want done
Users have it easy - someone already put many different programs
(instructions) into the computer and users just pick the ones they want to use
2 / 19
Users vs. programmers
Users see computers as a set of tools - word processor, spreadsheet, map,
to-do list, etc.
Programmers learn the computer “ways” and the computer language
Programmers have some tools that allow them to build new tools
Programmers sometimes write tools for lots of users and sometimes
programmers write little “helpers” for themselves to automate a task
3 / 19
Why be a programmer?
To get some task done - we are the user and programmer
Clean up survey data
To produce something for others to use - a programming job
Fix a performance problem in the Sakai software
Add a guestbook to a website
4 / 19
What is code, software, and program?
A sequence of stored instructions
It is a little piece of our intelligence in the computer
We figure something out, encode it, and then give it to someone else to save
them the time and energy of figuring it out
A piece of creative art - particularly when we do a good job on user experience
5 / 19
Hardware architecture
Center Processing Unit (CPU): runs the program. The CPU is always
wondering ”what to do next”. Not the brains exactly - very dumb but very
very fast
Input devices: keyboard, mouse, touch screen, ..
Output devices: screen, speaker, printer, DVD burner
Main memory: fast small temporary storage - lost on reboot - aka RAM
Secondary memory: slower large permanent storage - lasts until deleted - disk
drive / memory stick
Figure 1: Hardware architecture
6 / 19
IDE setup and introduction
IDE installation
VS Code
Google Colab
Write the first program
print("Hello World! I'm the first Python program.")
name = input("Enter your name: ")
print(f"Hello {name}. Nice to meet you!")
---
Hello World! I'm the first Python program.
Enter your name: Garry Kasparov
Hello Garry Kasparov. Nice to meet you!
7 / 19
Elements of Python
Vocabulary / Words - Variables and reversed words (Chapter 2)
Sentence structure - Valid syntax patterns (Chapter 3 - 5)
Story structure - Constructing a program for a purpose
8 / 19
Reserved words
You cannot use reserved words as variable names / identifiers
False class return is finally
None if for lambda continue
True def from while nonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise
9 / 19
Sentences or lines
x = 2 # assignment statement
x = x + 2 # assignment with expression
print(x) # print statement
"""
x: variable
=: operator
2: constant
print: function
"""
10 / 19
Interactive Python
anh@mac ~ % python3
Python 3.11.2 (main, Feb 16 2023, 03:07:35) [Clang 14.0.0
,→ (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more
,→ information.
>>> x = 3
>>> y = 7
>>> x + y
10
>>>
11 / 19
Python scripts
Interactive Python is good for experiments and programs of 3-4 lines long.
Most programs are much longer, so we type them into a file and tell Python
to run the commands in the file.
In a sense, we are “giving Python a script”.
As a convention, we add “.py” as the suffix on the end of these files to
indicate they contain Python.
Interactive Python vs. Python Script
Interactive: You type directly to Python one line at a time and it responds
Script: You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file
Demo Interactive Python and Python Script
12 / 19
Program steps or program flow
Like a recipe or installation instructions, a program is a sequence of steps to
be done in order.
Some steps are conditional - they may be skipped.
Sometimes a step or group of steps is to be repeated.
Sometimes we store a set of steps to be used over and over as needed several
places throughout the program (Chapter 4).
13 / 19
Sequential steps
When a program is running, it flows from one step to the next. As
programmers, we set up ”paths” for the program to follow.
14 / 19
Conditional steps
15 / 19
Repeated steps (loops)
Loops (repeated steps) have iteration variables that change each time
through a loop.
16 / 19
Complete Python program
# Write a program that takes as input three integers and output the
,→ largest one.
# get input data
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
# find the largest integer
max = a
if max < b:
max = b
if max < c:
max = c
# display the result
print(f"The largest integer is {max}")
17 / 19
Summary
This is a quick overview of Chapter 1
We will revisit these concepts throughout the course
Focus on the big picture
18 / 19
Acknowledgements / Contributions
This presentation is heavily adapted from the FPF191 course at FPT
,→ University whose content mostly based on the book titled "Python
,→ for Everybody (Exploring Data Using Python 3)" by Dr. Charles R.
,→ Severance. www.py4e.com.
19 / 19