http://inst.eecs.berkeley.edu/~cs61a/sp18/
CS 61A LECTURE 1
What is Computer Science?
The study of :
-
What problems can be solved using computation,
-
How to solve those problems, and
-
What techniques lead to effective solutions.
-
Systems
-
Artificial Intelligence : Decision Making, Robotics, Natural Language Processing ( Translation, Answering Questions …)
-
Graphics
-
Security
-
Networking
-
Programming Languages
-
Theory
-
Scientific Computing
-
…
What is This Course About?
- A course about managing complexity
- Mastering abstraction
- Programming paradigms
- Not all about 0’ s and 1’ s (in programming languages–python)
- An introduction to Python
- Full understanding of language fundamentals
- Learning through implementation
- How computers interpret programming languages
- A challenge course that will demand a lot of you
Course Policies
- Learning
- Community
- Course Staff
- Details…(http://cs61a.org/about.html)
Types of expressions
An expression describes a computation and evaluates to a value
All expression can use function call notation
- 2000 + 15
- call function: max(2, 4), min(-2, 50000)
- …
In [1]:2015
Out[1]:2015
In [2]:2000 + 15
Out[2]:2015
In [3]:from operator import add, mul
add(2, 3)
Out[3]:5
In [4]:mul(2, 3)
Out[4]:6
In [5]:max(1, 2, 3, 4, 5)
Out[5]:5
In [6]:mul(add(2, mul(4, 6)), add(3, 5))
Out[6]:208
Operators and Operands are also expressions
So they evaluate to values
Evaluation procedure for call expression:
- Evaluate the operator and then the operand subexpressions
- Apply the function that is the value of the operator subexpression to the arguments that are the values of the operand subexpression
Expression Tree
Evaluating Nested Expressions
Functions, Objects, and Interpreters
project
Download Sublime Text 3, Git Bash, lab00.zip
ls
: lists all files in the current directorycd
: change into the specified directorymkdir
: make a new directory with the given namemv
: move the file at the given source to the given destination
Doing the assignment
Open up lab00.py
in your text editor. You should see something like this:
def twenty_eighteen():
"""Come up with the most creative expression that evaluates to 2018,
using only numbers and the +, *, and - operators.
>>> twenty_eighteen()
2018
"""
return ______
The lines in the triple-quotes """
are called a docstring, which is a description of what the function is supposed to do. When writing code in 61A, you should always read the docstring!
The lines that begin with >>>
are called doctests. Recall that when using the Python interpreter, you write Python expressions next to >>>
and the output is printed below that line. Doctests explain what the function does by showing actual Python code: “if we input this Python code, what should the expected output be?”
In twenty_eighteen
,
- The docstring tells you to “come up with the most creative expression that evaluates to 2018,” but that you can only use numbers and arithmetic operators
+
(add),*
(multiply), and-
(subtract). - The doctest checks that the function call
twenty_eighteen()
should return the number 2018.
You generally will not need to modify the docstring, unless you want to add your own tests! The only part of your assignments that you’ll need to edit is the code.
Writing code
Once you understand what the question is asking, you’re ready to start writing code! You should replace the underscores in return ______
with an expression that evaluates to 2018. What’s the most creative expression you can come up with?
Don’t forget to save your assignment after you edit it! You can save by navigating to File > Save or by pressing Command-S on Mac-OS or Ctrl-S on Windows.
Running tests
In CS 61A, we will use a program called ok
to test our code. ok
will be included in every assignment in this class.
Back to the terminal! Make sure you are in the lab00
directory we created earlier (remember, the cd
command lets you change directories).
In that directory, you can type ls
to verify that there are the following three files:
lab00.py
: the starter file you just editedok
: our testing programlab00.ok
: a configuration file for Ok
Now, let’s test our code to make sure it works. You can run ok
with this command:
python ok
The first time you run Ok, you will be prompted for your bCourses email. Please follow these directions. We use this information to associate your code with you when grading.
Remember, if you are using Windows and the
python3
command doesn’t work, try using justpython
orpy
. See the the install Python 3 section for more info and ask for help if you get stuck!
If you wrote your code correctly, you should see a successful test:
=====================================================================
Assignment: Lab 0
Ok, version v1.11.1
=====================================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
1 test cases passed! No cases failed.
If you didn’t pass the tests, ok
will instead show you something like this:
---------------------------------------------------------------------
Doctests for twenty_eighteen
>>> from lab00 import *
>>> twenty_eighteen()
2013
# Error: expected
# 2018
# but got
# 2013
---------------------------------------------------------------------
Test summary
0 test cases passed before encountering first failed test case
Fix your code in your text editor until the test passes.
LECTURE 1 OVER
附:
我在Windows下terminal的界面如下:
text editor 采用的是 Sublime Text 3