Python - BASICS Lab

advertisement
Lecture 1
Introduction
Xiaojuan Cai(蔡小娟)
cxj@sjtu.edu.cn
Fall, 2015
Xiaojuan Cai
Computational Thinking
1
Before we start
• Pre-course questionnaire
• TA:
• 李春淼 lichunmiao@sjtu.edu.cn
• Course website:
• http://basics.sjtu.edu.cn/~xiaojuan/ct15/
Xiaojuan Cai
Computational Thinking
2
Course policy
• Final score =
• Homework (20%) + PA (20%) + Lab (10%)
+ Final exam (50%)
• Practice: Dianxin building 4-313
• Lab sample: percolation
Xiaojuan Cai
Computational Thinking
3
Textbook and References
• 程序设计思想与方法 陆朝俊 高等教育出版社
• References:
• [R1] Python Programming: An Introduction to
Computer Science, John Zelle.
• [R2] How to Think Like a Computer Scientist, Allen
Downey, Jeff Elkner and Chris Meyers
• http://basics.sjtu.edu.cn/~xiaojuan/ct15/files/
Xiaojuan Cai
Computational Thinking
4
Why this course?
• Fundamental part of computer science
• Helps you become a more intelligent user of
computers
• It can be fun!
• Helps the development of problem solving skills
• Programmers are in great demand!
Xiaojuan Cai
Computational Thinking
5
Have fun!
Xiaojuan Cai
Computational Thinking
6
Objective
• To learn what computer scientists study and the
techniques that they use.
• To understand computer programming languages.
• To begin using the Python programming language.
• To learn about chaotic models.
Xiaojuan Cai
Computational Thinking
7
Roadmap
• What is computer science?
• Programming languages
• Introduction to Python
• A chaotic model
Xiaojuan Cai
Computational Thinking
8
Computing and computer
Computing is any goal-oriented activity
requiring, benefiting from, or creating
algorithmic processes.
A computer is a general-purpose device that
can be programmed to carry out a set of
arithmetic or logical operations automatically.
--- Definitions from wikipedia
Xiaojuan Cai
Computational Thinking
9
Computing and computer
•
Computing can be traced back to 9th century.
•
A first general-purpose computing device
(computer) is invented by Charles Babbage, in early
19th century.
•
A first digital programmable computer is Colossus
(around 1943).
•
“Computers are to computer science what telescopes are
to astronomy.”
– E. Dijkstra
Xiaojuan Cai
Computational Thinking
10
Computers
Colossus(1943), the first electronic
digital programmable computing
device.
Picture sourced from Wikipedia.
•
IBM’s Blue Gene/P supercomputer
(2011) runs over 250,000
processors.
Picture sourced from Wikipedia.
In 2013, NUDT Tianhe-2 is ranked as the fastest supercomputer.
Xiaojuan Cai
Computational Thinking
11
Computers
• Software (programs) rule the hardware
(the physical machine).
• The process of creating software is called
programming.
• Before programming, we need to design
how to create (the algorithm).
Xiaojuan Cai
Computational Thinking
12
Hardware basics
• CPU
• the heart of computers
• “simple” instructions, fast speed
• Storage
• primary – also called RAM, fast but volatile
• secondary – permanently, NVRAM, ROM
• I/O device
display,
Xiaojuan Cai • Mouse, keyboard,
Computational
Thinkingtouch screen
13
Computer science
• It is not the study of computers!
It studies “What can be computed?”
• Design – Analysis – Experimentation
• Design = algorithm
• Some problems are unsolvable
• Experimentation: some problems are intractable
Xiaojuan Cai
Computational Thinking
14
Problem solving
• The most important skill
• formulate problems,
• think creatively about solutions, and
• express a solution clearly and accurately
• One level: learn to program
• Another level: learn how to use programs
Xiaojuan Cai
Computational Thinking
16
Roadmap
• What is computer science?
• Programming languages
• Introduction to Python
• A chaotic model
Xiaojuan Cai
Computational Thinking
17
Programming languages
• Natural language has ambiguity
• Programming languages: unambiguous and precise.
• Every structure in a pl has a precise form: syntax
• And it has a precise meaning: semantics.
Xiaojuan Cai
Computational Thinking
18
Languages of computers
• Machine language
• Computer’s language
• Impossible for human beings
• Assembly language
• Possible but still unnatural
• Still useful for designing efficient programs
• High level language
Xiaojuan Cai
Computational Thinking
19
High-level to low-level
• Interpreter
• Compiler
• Hybrid compiler/interpreting process
Xiaojuan Cai
Computational Thinking
20
GCD in pseudo code
function gcd(a, b)
while a ≠ b
if a > b
a := a - b
else
b := b - a
return a
Xiaojuan Cai
Computational Thinking
21
GCD in x86 machine language
Xiaojuan Cai
Computational Thinking
22
GCD in x86 assembly language
Xiaojuan Cai
Computational Thinking
23
GCD in C
Xiaojuan Cai
Computational Thinking
24
GCD in Prolog
Xiaojuan Cai
Computational Thinking
25
GCD in Scheme
Xiaojuan Cai
Computational Thinking
26
GCD in Python
def gcd(a,b):
while a != b:
if a > b:
a = a - b
else:
b = b - a
return a
Xiaojuan Cai
Computational Thinking
27
Roadmap
• What is computer science?
• Programming languages
• Introduction to Python
• A chaotic model
Xiaojuan Cai
Computational Thinking
28
Python
• Designed by Guido van Rossum
• Predecessor:ABC
• Name origin: Guido is a big fan of
Monty Python's Flying Circus
Picture sourced from Wikipedia.
• Here are some interesting slides from one of my
students in SE352 (programming languages).
Xiaojuan Cai
Computational Thinking
29
Python
• Python is an interpreted language
• Command-line mode
• File mode
• “Hello World!” in Python
print “Hello World!”
Xiaojuan Cai
Computational Thinking
30
Program
• A program is a sequence of instructions that
specifies how to perform a computation.
• Instructions include
• input, output, math, conditional execution,
repetition (iteration, recursion), …
Xiaojuan Cai
Computational Thinking
31
What is debugging
• Programming errors are called bugs.
• The process of tracking them down and
correcting them is called debugging.
• Errors
• Syntax error
• Semantics error: Run, but not the expected output
• Runtime error: Divide by zero
Xiaojuan Cai
Computational Thinking
32
• Let’s play around.
Xiaojuan Cai
Computational Thinking
33
Roadmap
• What is computer science?
• Programming languages
• Introduction to Python
• A chaotic model
Xiaojuan Cai
Computational Thinking
34
Chaos and computing
• Two features:
• For any input, returns 10 seemingly random
numbers between 0 and 1. (chaotic)
• Very small differences in initial value can have
large differences in the output. (butterfly effect)
• The function computed by program has the
general form kx(1-x) where k is 3.9
Xiaojuan Cai
Computational Thinking
35
•
•
•
•
•
•
•
•
•
•
•
Input:
0.25
0.73125
0.76644140625
0.698135010439
0.82189581879
0.570894019197
0.955398748364
0.166186721954
0.540417912062
0.9686289303
0.118509010176
Xiaojuan Cai
• Input:
0.26
0.75036
0.73054749456
0.767706625733
0.6954993339
0.825942040734
0.560670965721
0.960644232282
0.147446875935
0.490254549376
0.974629602149
Computational Thinking
36
Lessons from chaos
• This program has import implications.
• Many phenomena in real world exhibit this kind
of chaotic behavior.
• Butterfly effect
• Weather forecast
• Computers may give incorrect answers if there
are very small errors in models or programs.
Xiaojuan Cai
Computational Thinking
37
Summary
• A computer is a universal information-processing
machine.
• Computer science is the study of what can be
computed.
• A computer comprises a CPU, main/secondary
memory, and I/O devices.
• Programs are written using a programming
language.
Xiaojuan Cai
Computational Thinking
38
Summary (cont’d)
• Python is an interpreted language.
• A Python program is a sequence of statements
• A mathematical model is called chaotic if very
small changes in the input lead to large changes
in the random results.
Xiaojuan Cai
Computational Thinking
39
Python
“优雅”、“明确”、“简单”
周丞
上海交通大学
Xiaojuan Cai
Computational Thinking
40
Python娘
简介
• 父亲是吉多·范罗苏姆(Guido van
Rossum)
• 出生于1991年。
• 女大十八变,已经历了
幼女期(1.X版本)和
萝莉期(2.X版本)
目前进入御姐期(3.X版本)
还在健康成长着……
Xiaojuan Cai
Computational Thinking
41
Python娘
培养理念(设计哲♂学)
“优雅”、“明确”、“简单”
因为家长们不喜欢邻家男孩Perl的教育
方式
“There’s More Than One Way
To Do It.”
(不只一种方法来做一件事。)
所以Python娘受到的教育是
“用一种方法,最好是只有一种方法来
做一件事”
Xiaojuan Cai
Computational Thinking
42
Python娘
特点
• 活泼(动态语言)
• 灵活(“脚本语言”、“胶水语言”)
• 抽象思维(面向对象语言,支持函数式
编程)
• 优雅美丽(语法简捷和清晰)
Xiaojuan Cai
Computational Thinking
43
• Python娘很傲骄,御姐状态和萝莉状
态变化很大。
(3.X版本不兼容2.X版本)
• 御姐是未来和希望,但是人们现在更
加喜欢萝莉。
Xiaojuan Cai
Computational Thinking
44
Download