Berkeley CS 61A Lecture 1

http://inst.eecs.berkeley.edu/~cs61a/sp18/

CS 61A LECTURE 1

What is Computer Science?

The study of :

  1. What problems can be solved using computation,

  2. How to solve those problems, and

  3. 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?

  1. A course about managing complexity
    • Mastering abstraction
    • Programming paradigms
    • Not all about 0’ s and 1’ s (in programming languages–python)
  2. An introduction to Python
    • Full understanding of language fundamentals
    • Learning through implementation
    • How computers interpret programming languages
  3. 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:

  1. Evaluate the operator and then the operand subexpressions
  2. 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 directory
  • cd: change into the specified directory
  • mkdir: make a new directory with the given name
  • mv: 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 edited
  • ok: our testing program
  • lab00.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 just python or py. 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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值