-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture_1
More file actions
72 lines (48 loc) · 2.32 KB
/
Lecture_1
File metadata and controls
72 lines (48 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# the first lecutre skims the power of python interpreter. We went over some basic terms and ideas. I will write down
# the codes I learned from lecture 1.
# at first, let's try to retrieve Shakespeare's work and do something with it.
# import a new function from urllib.request, called urlopen
from urllib.request import urlopen
# associates the name shakespeare with a online file that contains Shakespeare's work.
shakespeare = urlopen('http://composingprograms.com/shakespeare.txt')
# associates the name words to the set of all unique words that appear in Shakespeare's work.
# actually, 'set' is a object to build an unordered collection of unique elements.
# we read the data from the opened URL, then decoe the data into text, and finally split the
# text into words.
words = set(shakespeare.read().(decode().split())
{w for w in words if len(w) == 6 and w[::-1] in words}
# this command evaluates to the set of all Shakespearian words that are simultaneously
# a word spelled in reverse.
# ok, that is the first part of the lecture 1. Now we turn to try some basic commands.
# find the maximal value
max(4, 5)
# find the value of 100 to the power of 2
pow(100,2)
# importing library functions: some simple examples
# from math module we import function 'square root of ...'
sqrt(256)
# from operator module we import function addition, multiplication, substraction
from operator import add, sub, mul
add(14, 28)
sub(100, mul(7, add(8,4)))
# from math module import the value of pi
from math import pi
pi * 2
# assign multiple values to multiple names in a single statement:
a, b = 3, 4
# at last we went over the pure vs. non-pure function.
# the most obvious non-pure function is print()
# we will dive into non-pure functions in Ch2 and pure functions in Ch4.
#########################
#lab0/1:
# terminal commands:
# pwd : show current directory
# cd : change to following directory
# cd ~/Documents : ~/ represents /Users/callforsky, that is, the default computer mother directory
# mkdir : create a file in current directory. ex: mkdir hw1
# ls : list all file in current directory
# mv [file1's directory] [file 2's directory] : move file 1 from directory 1 to another directory.
# ex: mv ~/Downloads/hw0.py ~/cs61a/hw/hw0
# reset : to clear the terminal's window and won't see previous code.
# clear: same as before
# Lec 1 conlcuded