Why should you learn to write programs?
SE 113 – Introduction To Programming
Contents (Chapter 1)
● Creativity and Motivation
● Computer hardware architecture
● Understanding programming
● Words and sentences
● Conversing with Python
● Interpreter and compiler
● Writing a program
● What is a program?
● Debugging
Introduction
● Writing programs (or programming) is a very creative and rewarding activity.
○ You can write programs for many reasons,
■ ranging from making your living
■ to solving a difficult data analysis problem
■ to having fun
■ to helping someone else solve a problem.
■ To make a Money ☺
Introduction
● Programmers add an operating system and a set of applications to the hardware
(computer, laptop, tablet, phone etc.) and we end up with a Personal Digital Assistant
that is quite helpful and capable of helping us do many different things.
● Our computers are fast and have vast amounts of memory and could be very helpful to us
if we only knew the language to speak to explain to the computer what we would like it to
“do next”.
● If we knew this language, we could tell the computer to do tasks on our behalf that were
repetitive.
Introduction
● For example,
○ look at this slide and tell me the most commonly used word and how many times the word is used.
○ While you were able to read and understand the words in a few seconds, counting them is almost
painful because it is not the kind of problem that human minds are designed to solve.
○ For a computer, the opposite is true, reading and understanding text from a piece of paper is
hard for a computer to do but counting the words and telling you how many times the most used
word was used is very easy for the computer:
■ Our “personal information analysis assistant” quickly told us most commonly used in this slide in milliseconds.
Creativity and motivation
● Your computer or Personal Digital Assistant (PDA) usually contains many different programs
from many different groups of programmers, each competing for your attention and
interest.
● They try their best to meet your needs and give you a great user experience in the process
Creativity and motivation
● For now, our primary motivation is not to make money or please end users, but instead for
us to be more productive in handling the data and information that we will encounter in
our lives or in our jobs.
● Now you are just a user,
● After this course, you will be both the programmer and the end user of your programs. As
you gain skill as a programmer and programming feels more creative to you, your
thoughts may turn toward developing programs for others.
Computer hardware architecture
● Before we start learning the language we speak to give instructions to computers to
develop software, we need to learn a small amount about how computers are built.
The Central Processing Unit (or CPU) is the part of the computer that
is built to be obsessed with “what is next?” If your computer is rated
at 3.0 Gigahertz, it means that the CPU will ask “What next?” three
billion times per second.
The Main Memory is used to store information that the CPU needs in
a hurry. The main memory is nearly as fast as the CPU. But the
information stored in the main memory vanishes when the computer
is turned off.
Computer hardware architecture
● Computer architecture
The Secondary Memory is also used to store information, but it is
much slower than the main memory. The advantage of the
secondary memory is that it can store information even when there
is no power to the computer.
The Input and Output Devices are simply our screen, keyboard,
mouse, microphone, speaker, touchpad, etc. They are all of the
ways we interact with the computer.
Most computers also have a Network Connection to retrieve
information over a network.
Computer hardware architecture
● As a programmer, your job is to use and orchestrate each of these resources to solve the
problem that you need to solve and analyze the data you get from the solution.
● As a programmer you will mostly be “talking” to the CPU and telling it what to do next.
● Sometimes you will tell the CPU to use the main memory, secondary memory, network, or
the input/output devices.
● For these activities, operating systems helps us.
Understanding programming
● After this course, (about 3 months later) you will be a programmer - perhaps not a
professional programmer, but at least you will have the skills to look at a data/information
analysis problem and develop a program to solve the problem.
● In a sense, you need two skills to be a programmer:
○ First, you need to know the programming language (Python) - you need to know the vocabulary
and the grammar. You need to be able to spell the words in this new language properly and
know how to construct well-formed “sentences” in this new language.
○ Second, you need to “tell a story”. In writing a story, you combine words and sentences to convey
an idea to the reader. There is a skill and art in constructing the story, and skill in story writing is
improved by doing some writing and getting some feedback.
In programming, our program is the “story” and the
problem you are trying to solve is the “idea”.
Understanding programming
● Once you learn one programming language such as Python, you will find it much easier to
learn a second programming language such as JavaScript or C++.
○ The new programming language has very different vocabulary and grammar but the
problem-solving skills will be the same across all programming languages.
Words and sentences in PL
● Unlike human languages, the Python vocabulary is actually pretty small.
● We call this “vocabulary” the “reserved words”.
● These are words that have very special meaning to Python.
● When Python sees these words in a Python program, they have one and only one
meaning to Python.
● Later as you write programs you will make up your own words that have meaning to you
called variables.
Words and sentences in PL
● The reserved words in the language where humans talk to Python include the following:
and , del, global, not, with,
as, elif, if, or, yield , assert,
else, import, pass, break,
except, in, raise, class, finally,
is, return, continue, for, lambda,
try , def, from, nonlocal, while…
Words and sentences in PL
● We will learn these reserved words and how they are used in good time, but for now we
will focus on the Python equivalent of “speak”.
● The nice thing about telling Python to speak is that we can even tell it what to say by
giving it a message in quotes:
○ print('Hello world!')
● And we have even written our first syntactically correct Python sentence.
● Our sentence starts with the function print followed by a string of text of our choosing
enclosed in single quotes.
● The strings in the print statements are enclosed in quotes. Single quotes and double quotes
do the same thing
Conversing with Python
● Now that we have a word and a simple sentence that we know in Python, we need to
know how to start a conversation with Python to test our new language skills.
● Before you can converse with Python, you must first install the Python software on your
computer and learn how to start Python on your computer.
○ We install python on your computer tomorrow, in lab section.
Python 3.9.1 (v3.9.1:37a07cee5969, Mar 6 2022,
11:54:25)
[MSC v.1900 64 bit (AMD64)] on win64
Type "help", "copyright", "credits" or "license" for
more information. >>>
Conversing with Python
● After installation, when we write python on prompt, you will see this screen.
Python 3.9.1 (v3.9.1:37a07cee5969, Mar 6 2022,
11:54:25)
[MSC v.1900 64 bit (AMD64)] on win64
Type "help", "copyright", "credits" or "license" for
more information. >>>
● The >>> prompt is the Python interpreter’s way of asking you, “What do you want me to do
next?” Python is ready to have a conversation with you. All you have to know is how to
speak the Python language.
Conversing with Python
● Let’s say for example that you did not know even the simplest Python language words or
sentences. You might want to use the standard line that astronauts use when they land on
a faraway planet and try to speak with the inhabitants of the planet:
>>> I come in peace, please take me to your leader
File "", line 1
I come in peace, please take me to your leader
^
SyntaxError: invalid syntax >>>
This is not going so well ☺ Because you didn’t talk python using appropriate language
>>> print('Hello world!')
Hello world!
Now you are talking with python ☺
Conversing with Python
>>> print 'We will have a feast tonight unless you say
File "", line 1
print 'We will have a feast tonight unless you say
^
SyntaxError: Missing parentheses in call to 'print'
>>> good-bye
● To close the python Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'good' is not defined
>>> if you don't mind, I need to leave
File "<stdin>", line 1
if you don't mind, I need to leave
^
SyntaxError: invalid syntax
>>> quit()
Interpreter and compiler
● Python is a high-level language intended to be relatively straightforward for humans to
read and write and for computers to read and process.
● Other high-level languages include Java, C++, PHP, Ruby, Basic, Perl, JavaScript, and
many more.
● The actual hardware inside the Central Processing Unit (CPU) does not understand any of
these high-level languages.
● The CPU understands a language we call machine language.
● Machine language is very simple and frankly very tiresome to write because it is
represented all in zeros and ones:
○ 001010001110100100101010000001111 11100110000011101010010101101101 ...
Interpreter and compiler
● Since machine language is tied to the computer hardware, machine language is not
portable across different types of hardware.
● Programs written in high-level languages can be moved between different computers by
using a different interpreter on the new machine or recompiling the code to create a
machine language version of the program for the new machine.
● These programming language translators fall into two general categories:
○ (1) interpreters
○ (2) compilers.
Interpreter and compiler
● An interpreter reads the source code of the program as written by the programmer, parses
the source code, and interprets the instructions on the fly.
● Python is an interpreter and when we are running Python interactively, we can type a line
of Python (a sentence) and Python processes it immediately and is ready for us to type
another line of Python.
>>> x = 6
>>> print(x)
6
>>> y = x * 7
>>> print(y)
42
>>>
It is the nature of an interpreter to be able to have an
interactive conversation.
Interpreter and compiler
● A compiler needs to be handed the entire program in a file, and then it runs a process to
translate the high-level source code into machine language and then the compiler puts
the resulting machine language into a file for later execution.
Interpreter and compiler
● If you have a Windows system, often these executable machine language programs have
a suffix of “.exe” or “.dll” which stand for “executable” and “dynamic link library”
respectively.
● In Linux and Macintosh, there is no suffix that uniquely marks a file as executable.
● If you were to open an executable file in a text editor, it would look completely crazy and
be unreadable:
^?ELF^A^A^A^@^@^@^@^@^@^@^@^@^B^@^C^@^A^@^@^@\xa0\x82
^D^H4^@^@^@\x90^]^@^@^@^@^@^@4^@ ^@^G^@(^@$^@!^@^F^@
^@^@4^@^@^@4\x80^D^H4\x80^D^H\xe0^@^@^@\xe0^@^@^@^E
^@^@^@^D^@^@^@^C^@^@^@^T^A^@^@^T\x81^D^H^T\x81^D^H^S
^@^@^@^S^@^@^@^D^@^@^@^A^@^@^@^A\^D^HQVhT\x83^D^H\xe8 ....
Writing a program
● Typing commands into the Python interpreter is a great way to experiment with Python’s
features, but it is not recommended for solving more complex problems.
● When we want to write a program, we use a text editor to write the Python instructions into
a file, which is called a script. By convention, Python scripts have names that end with .py.
● To execute the script, you have to tell the Python interpreter the name of the file. In a
command window, you would type python [Link] as follows:
$ python [Link]
Hello world!
Writing a program
● In this course, We use pyCharm as IDE (Integrated Development Environment)
What is a program?
● The definition of a program at its most basic is a sequence of Python statements that have
been crafted to do something.
● Even our simple [Link] script is a program.
○ It is a one-line program and is not particularly useful, but in the strictest definition, it is a Python
program.
● It might be easiest to understand what a program is by thinking about a problem that a
program might be built to solve, and then looking at a program that would solve that
problem with any PL.
Debugging
● When Python spits out an error or even when it gives you a result that is different from what
you had intended, then begins the hunt for the cause of the error.
● Debugging is the process of finding the cause of the error in your code.
● In this course we learn how can we debug our code and fix it.
● Please read chapter 1 from the book ☺
Thank you for listening…