python ch 01 lecture notes
TRANSCRIPT
-
7/27/2019 Python Ch 01 lecture notes
1/18
-
7/27/2019 Python Ch 01 lecture notes
2/18
-
7/27/2019 Python Ch 01 lecture notes
3/18
After all, computers are everywhere. You see them as tiny little machines that look
like toys to massive stacks that take up rooms.
-
7/27/2019 Python Ch 01 lecture notes
4/18
With examples that broad we have to make sure we know what a computer is. A
simple definition is: A machine that stores and manipulates information under the
control of a changeable program
-
7/27/2019 Python Ch 01 lecture notes
5/18
But that means you need to know what a computer program is. Which brings us to
why you are here, to study computer programming and computer science.
-
7/27/2019 Python Ch 01 lecture notes
6/18
Computer science is not the study of computers. Edsger Dijkstra, a famous computer
scientist, once said: Computer science is no more about computers than astronomy
is about telescopes.
-
7/27/2019 Python Ch 01 lecture notes
7/18
So what is computer science about? Computer science tries to answer the question:
What process can we describe? or even simpler, What can be computed? To
answer these questions we look to design, analysis, and experimentation.
-
7/27/2019 Python Ch 01 lecture notes
8/18
To demonstrate if a program can be solved we can design a step-by-step process to
create the desired result. We can examine the problem with mathematical analysis to
see if the solution is practical or even possible. Sometimes the only way to solve an
ill-defined or complex problem is through devising experiments, implementing a
sample system and then studying its behavior.
-
7/27/2019 Python Ch 01 lecture notes
9/18
One aspect of design is to create a solution. If we can devise a solution with a step-
by-step recipe then we need only write code for our solution. Well give a fancy name
to the step-by-step process we discover and call it an algorithm.
-
7/27/2019 Python Ch 01 lecture notes
10/18
A look under the hood of a typical computer. You dont have to know all the details
but a general concept of how information moves inside most computers. The Central
Processing Unit (CPU) is sometimes referred to as the brain of the computer. Some
people also like to think of it as a traffic cop, directing information where to go. The
CPU performs basic arithmetic and logical operations, like adding or comparing
numbers.
Memory stores programs and data. Main memory is often referred to as RAM
(Random Access Memory). RAM is fast but volatile. If it loses power, it loses the
information. Secondary memory stores programs and data more permanently. CDs,
DVDs, hard drives, and USB drives are very common. Humans give input to the
computer through a keyboard or load a program or data from secondary memory.
The CPU processes the information and moves it to and from memory as needed.
Sometimes lesser used information is shuffled off to secondary memory or simply
saved for later use. Finally, as humans request, the computer sends results to outputdevices like your monitor or printer.
-
7/27/2019 Python Ch 01 lecture notes
11/18
Programming languages are codes for writing down the instructions for a computer
to follow. Every computer language has a precise form (syntax) and precise meaning
(semantics). High-level computer languages are designed to be used and understood
by humans.
Computer hardware can only understand a very low level language known as
machine language.
-
7/27/2019 Python Ch 01 lecture notes
12/18
Code is written in a high level language such as Python, C# or Java
Python is technically a hybrid compiled/interpreted language. Interpreted languages
like JavaScript (no relation to Java) are read and executed line by line by some other
program, for example a web browser. The Python Interactive Shell works this way.
Code from compiled languages, like C++ or Java is fed to a special program called a
compiler that translates the human readable source code into some intermediate
language. This language can be machine code that a computer can execute directly or
as in the .NET or Java family of languages it can be used by a runtime, a special
executable that reads the intermediate language and executes. In the case of Java,
this is called the Java Virtual Machine. Since each hardware platform has their own
Java Virtual Machine this makes the same Java code portable to multiple hardware
platforms. Python can work in both ways. When you install Python, you install an
interactive interpreter that reads Python code line by line. Your Python code can be
stored as a module or script that can be sent to other computers. As long as thosecomputers have Python installed they can execute your module.
The first time you import a module file, Python creates a companion file with a .pyc
extension. This is a byte code file. Python takes your module code and compiles it
into byte code. The byte code is then interpreted.
-
7/27/2019 Python Ch 01 lecture notes
13/18
Functions are ways to execute several statements together to solve a common
problem. They may be small and simple such as this one. Later well see more
complex functions. The first line states we are defining a function. The next lines are
indented to show they are part of the hello function. The blank line (hitting enter
twice) lets Python know the function is finished.
-
7/27/2019 Python Ch 01 lecture notes
14/18
To invoke a function, type its name. Notice the parentheses. These are part of its
name and should be typed. Later well see that we sometimes need information
within the parentheses. You can add changeable parts to the function inside the
parentheses calledparameters.
-
7/27/2019 Python Ch 01 lecture notes
15/18
-
7/27/2019 Python Ch 01 lecture notes
16/18
Save Python code as plain text files called modules or scripts.
Aprogramming environmentis designed to help programmers write programs and
usually includes automatic indenting, highlighting, etc. The default programming
environment for Python is called IDLE.
-
7/27/2019 Python Ch 01 lecture notes
17/18
Comments start with a #. Python skips from # to the end of the line. This is for
humans and ignored by Python. Well save this file as chaos.py.
This code defines a function called main(). This program has only one module so it
could have been written without the main function. Using main is customary.
A variable is used to assign a name to a value so that we can refer to it later. X is an
example of our variable.
The for line tells Python to run a loop, in other words, to repeat the indented lines a
certain number of times, in this case, 10.
x = 3.9 *x*(1-x) is an assignment. The value from the right hand side of the = is
assigned to the variable x.
The last line tells Python to run the code.
-
7/27/2019 Python Ch 01 lecture notes
18/18