1
1

00:00:00,650  -->  00:00:04,089
Welcome to the second lesson. We're going over three topics in this video:
2

2

00:00:04,089  -->  00:00:07,770
expressions, datatypes, and variables. Go
ahead and start
3

3

00:00:07,770  -->  00:00:11,179
IDLE, which is the editor that comes
installed with IDLE.
4

4

00:00:11,179  -->  00:00:14,660
On Windows click on the Start button, type IDLE,
5

5

00:00:14,660  -->  00:00:18,619
and I have multiple versions of Python
installed on my computer,
6

6

00:00:18,619  -->  00:00:23,289
but I'm just going to use IDLE that
comes with Python 3.5. The course notes
7

7

00:00:23,289  -->  00:00:25,859
describe how to start IDLE if you have Mac or Linux.
8

8

00:00:25,859  -->  00:00:29,869
IDLE has two parts: the interactive
shell
9

9

00:00:29,869  -->  00:00:36,020
and, if you click on File, New File, the
file editor
10

10

00:00:36,020  -->  00:00:40,270
will appear. The interactive shell runs Python instructions one at a time and shows
11

11

00:00:40,270  -->  00:00:41,649
you results immediately.
12

12

00:00:41,649  -->  00:00:45,079
It's great for just experimenting and seeing
what instructions do.
13

13

00:00:45,079  -->  00:00:48,989
The file editor lets you enter Python
code for complete programs.
14

14

00:00:48,989  -->  00:00:52,690
There are other editor apps for typing in Python programs.
15

15

00:00:52,690  -->  00:01:01,700
I use one called Sublime Text and
there's another one called PyCharm.
16

16

00:01:01,710  -->  00:01:06,100
But this course will use IDLE because it
comes with Python and there's no
17

17

00:01:06,100  -->  00:01:08,780
additional set up to do, and because it's
the same across
18

18

00:01:08,780  -->  00:01:12,870
Mac, Windows, and Linux. The interactive
shell and file editor windows look
19

19

00:01:12,870  -->  00:01:16,500
really similar so it's easy to get them
confused. We can always tell the difference
20

20

00:01:16,580  -->  00:01:20,720
because the interactive shell will have
the triple angle bracket prompt.
21

21

00:01:20,720  -->  00:01:24,860
In this lesson, we'll be entirely focused
on the interactive show so close the
22

22

00:01:24,860  -->  00:01:30,580
file editor for now. Let's begin by typing a
simple Python instruction.
23

23

00:01:30,580  -->  00:01:34,470
Type 2 + 2 and then press Enter.
24

24

00:01:34,470  -->  00:01:38,540
In Python 2 + 2 is called an
"expression".
25

25

00:01:38,540  -->  00:01:42,100
It's the most basic kind of programming
instruction in the Python language.
26

26

00:01:42,100  -->  00:01:45,840
Expressions consist of values, such as
the 2s,
27

27

00:01:45,840  -->  00:01:49,080
and operators, such as the +.
28

28

00:01:49,080  -->  00:01:52,150
Expressions always evaluate, that is
29

29

00:01:52,150  -->  00:01:55,460
they always reduce down to a single value.
30

30

00:01:55,460  -->  00:01:59,750
So in this example 2 + 2 is
evaluated down to a single value
31

31

00:01:59,750  -->  00:02:03,990
4. You can even think of the single
value by itself as an expression:
32

32

00:02:03,990  -->  00:02:07,610
the expression 2 evaluates to itself,
33

33

00:02:07,610  -->  00:02:10,950
2. There's several other types of operators we can try out.
34

34

00:02:10,950  -->  00:02:15,200
Type the following: 5 - 3, which does
subtraction
35

35

00:02:15,200  -->  00:02:19,050
and evaluates 2. 3 * 7,
36

36

00:02:19,050  -->  00:02:23,850
which does multiplication and evaluates to 21. Note that multiplication is done with
37

37

00:02:23,850  -->  00:02:24,120
the
38

38

00:02:24,120  -->  00:02:29,540
asterisk and not the X. If you try to type
something like
39

39

00:02:29,540  -->  00:02:32,739
3 x 7, Python will just give you an
error.
40

40

00:02:32,739  -->  00:02:35,750
For division we use the forward slash.
41

41

00:02:35,750  -->  00:02:39,690
So 22 / 7 evaluates to
42

42

00:02:39,690  -->  00:02:43,630
3.14 and there are few other less
common math
43

43

00:02:43,630  -->  00:02:48,400
operators so check the course notes for
them. Now, order of operation rules apply here.
44

44

00:02:48,420  -->  00:02:52,890
Multiplication and division are done first,
then addition and subtraction.
45

45

00:02:52,890  -->  00:02:56,799
but you can use parentheses to override the usual precedence if you need to.
46

46

00:02:56,799  -->  00:03:02,000
So let's look at two similar expressions:
2 + 3 * 6,
47

47

00:03:02,000  -->  00:03:03,780
which  evaluates to 20.
48

48

00:03:03,780  -->  00:03:07,580
And then take (2 +3), in parentheses,
49

49

00:03:07,580  -->  00:03:11,010
* 6, which evaluates to 30.
50

50

00:03:11,010  -->  00:03:14,510
So I have a visualization tool so
51

51

00:03:14,510  -->  00:03:18,030
that we can see how Python evaluates
expressions step-by-step. In
52

52

00:03:18,030  -->  00:03:23,159
2 + 3 * 6, the
multiplication is done
53

53

00:03:23,159  -->  00:03:26,739
first. So 3 * 6 evaluates to 18,
and
54

54

00:03:26,739  -->  00:03:31,640
2 + 18 evaluates to 20. While the parentheses
55

55

00:03:31,640  -->  00:03:35,420
overrides the multiplication, so 2 +3 is done
56

56

00:03:35,420  -->  00:03:39,860
first. And a value just in parentheses by
itself just
57

57

00:03:39,860  -->  00:03:43,859
evaluates to the value, and then 5 time 6
evaluates to 30.
58

58

00:03:43,859  -->  00:03:47,030
No matter how big the expression is
Python evaluates it
59

59

00:03:47,030  -->  00:03:51,890
until it becomes a single value. Let's
use this expression as an example:
60

60

00:03:51,890  -->  00:04:06,100
(5 - 1) * ((7 + 1) / (3 - 1))
61

61

00:04:06,129  -->  00:04:09,239
No matter how complicated the expression
is,
62

62

00:04:09,239  -->  00:04:12,359
all expressions are just values and
operators, and it
63

63

00:04:12,359  -->  00:04:16,639
always evaluates down to a single value.
You can see this
64

64

00:04:16,639  -->  00:04:22,990
step-by-step in the visualization tool:
7 +1
65

65

00:04:22,990  -->  00:04:26,570
evaluates to 8. 3 -1 then evaluates
66

66

00:04:26,570  -->  00:04:31,639
and then the operation inside the parnetheses evaluate
67

67

00:04:31,639  -->  00:04:35,280
and then finally it evaluates down to 16.
68

68

00:04:35,280  -->  00:04:39,870
All of these expressions evaluate to the
same thing you can copy and paste
69

69

00:04:39,870  -->  00:04:43,610
any of these and they evaluate
70

70

00:04:43,610  -->  00:04:51,199
to the same value.
71

71

00:04:51,199  -->  00:04:56,010
If you type something that Python can't
understand it will display an error
72

72

00:04:56,010  -->  00:04:58,349
message so let's trigger one of those in IDLE right now.
73

73

00:04:58,349  -->  00:05:01,360
Type 5 + and press Enter.
74

74

00:05:01,360  -->  00:05:05,440
You can always test to see whether
instruction works by typing it in the
75

75

00:05:05,440  -->  00:05:06,419
interactive shell.
76

76

00:05:06,419  -->  00:05:10,700
These errors don't damage the computer. Don't worry. Professionals software developers get
77

77

00:05:10,740  -->  00:05:14,870
error message while writing code all the
time. So don't think that you
78

78

00:05:14,870  -->  00:05:17,690
messed anything up.
79

79

00:05:17,690  -->  00:05:20,130
All values have a data type. A data type is
80

80

00:05:20,130  -->  00:05:23,700
a category of values, and every value
belongs to exactly
81

81

00:05:23,700  -->  00:05:27,280
one data type. So whole number values like
82

82

00:05:27,280  -->  00:05:30,630
-2 and 30, for example, are integers.
83

83

00:05:30,630  -->  00:05:34,490
Numbers with a decimal points such as
3.14 are called
84

84

00:05:34,490  -->  00:05:38,700
floating point numbers. We also call integer and floating point values
85

85

00:05:38,700  -->  00:05:41,820
ints and floats for short.
86

86

00:05:41,820  -->  00:05:44,940
Note that even though 42 is an integer,
87

87

00:05:44,940  -->  00:05:51,430
42.0 is a float. So there's also data type
for text values. These are called strings.
88

88

00:05:51,430  -->  00:05:55,980
When typing strings begin and end them
with a single quote so Python knows
89

89

00:05:55,980  -->  00:05:57,590
where the text begins and ends.
90

90

00:05:57,590  -->  00:06:01,490
For example type 'Hello world'.
91

91

00:06:01,490  -->  00:06:06,300
When the plus operator is used on
strings
92

92

00:06:06,300  -->  00:06:09,580
it's the "string concatenation" operator.
Concatenation
93

93

00:06:09,580  -->  00:06:12,680
means, "joins the strings together". For
example let us type
94

94

00:06:12,680  -->  00:06:15,780
'Alice' + 'Bob'
95

95

00:06:15,780  -->  00:06:20,290
And this expression evaluates down to be
single string
96

96

00:06:20,290  -->  00:06:23,990
'AliceBob'. Less common is the
multiplication
97

97

00:06:23,990  -->  00:06:28,600
operator for the... for string replication
which operates on an integer and a string.
98

98

00:06:28,610  -->  00:06:32,430
This is handy if you have to make a string
repeat itself. You can type in
99

99

00:06:32,430  -->  00:06:36,760
a string 'Alice' * 3 to create
100

100

00:06:36,760  -->  00:06:40,890
the string 'AliceAliceAlice'. Or try this
expression which
101

101

00:06:40,890  -->  00:06:44,700
uses string concatenation and
replication. A string 'Hello'
102

102

00:06:44,700  -->  00:06:48,620
+ '!'
103

103

00:06:48,620  -->  00:06:53,730
* 10. And that will evaluate to a string value that has
104

104

00:06:53,730  -->  00:06:59,660
'Hello' with 10 exclamation marks after it.
Your programs can save values to the
105

105

00:06:59,660  -->  00:07:01,470
computer's memory with variables.
106

106

00:07:01,470  -->  00:07:06,700
A variable is kind of like a box that can
store a single value.
107

107

00:07:06,700  -->  00:07:10,430
You can do this with an assignment statement. In the interactive shell
108

108

00:07:10,430  -->  00:07:14,090
enter spam = 42.
109

109

00:07:14,090  -->  00:07:17,670
This creates a new variable named spam
110

110

00:07:17,670  -->  00:07:21,950
and stores the value 42 inside of it.
111

111

00:07:21,950  -->  00:07:26,070
You can think of it as a box with the
label "spam"
112

112

00:07:26,070  -->  00:07:30,500
and it has the value 42 in it. And normally
you would give variables names that describe
113

113

00:07:30,500  -->  00:07:34,440
the data they contain. Imagine if you were
moving and you labeled all of your boxes
114

114

00:07:34,440  -->  00:07:40,200
"stuff". That would be too helpful. But for
these lessons we just need a generic name so
115

115

00:07:40,240  -->  00:07:44,520
the words spam, eggs, bacon, and ham are
often used in Python tutorials
116

116

00:07:44,520  -->  00:07:48,100
because of the spam comedy sketch from
Monty Python.
117

117

00:07:48,100  -->  00:07:52,360
"Egg, bacon, sausage, and spam. Spam, bacon, sausage, and spam. Spam, egg, spam, spam, bacon, and spam. Spam, spam, spam, spam, egg, & spam. Spam-"
118

118

00:07:52,360  -->  00:08:02,250
You can use variables and expressions
119

119

00:08:02,250  -->  00:08:05,790
anywhere you would normally use values.
A variable just evaluates
120

120

00:08:05,790  -->  00:08:09,130
to the value it contains. So when I type spam,
121

121

00:08:09,130  -->  00:08:12,290
that evaluates to 42. So in the interactive
shell
122

122

00:08:12,290  -->  00:08:18,700
type spam =  'Hello'. That will
store the string Hello' in the variable spam.
123

123

00:08:18,710  -->  00:08:22,140
Now when we have the expression spam + 'world'...
124

124

00:08:22,140  -->  00:08:26,820
oh, whoops, well Python does exactly
what we tell it to
125

125

00:08:26,820  -->  00:08:30,410
which is why it doesn't have a space in
between these words. Let's try that again.
126

126

00:08:30,410  -->  00:08:34,680
spam + ' world' with space in
front.
127

127

00:08:34,680  -->  00:08:38,390
There we go. So this is just
128

128

00:08:38,390  -->  00:08:43,140
regular string concatenation because
spam evaluates down to the
129

129

00:08:43,140  -->  00:08:46,300
string inside of it, and then that gets
130

130

00:08:46,300  -->  00:08:49,610
concatenated with the ' world' string.
Additional assignment
131

131

00:08:49,610  -->  00:08:53,700
statements can change the value that's
inside the variable. The old value is forgotten.
132

132

00:08:53,700  -->  00:08:58,760
This is called "overwriting" the variable.
So I could set span to a new vari-
133

133

00:08:58,760  -->  00:09:02,510
uh, value such as 'Goodbye'.
134

134

00:09:02,510  -->  00:09:06,570
This same spam + ' world' expression
135

135

00:09:06,570  -->  00:09:10,780
evaluates to a different value. So you can think of
136

136

00:09:10,780  -->  00:09:14,430
the old value as just being tossed
out and forgotten and the new value
137

137

00:09:14,430  -->  00:09:17,620
being assigned every time we have an
assignment statement.
138

138

00:09:17,620  -->  00:09:22,070
Expressions are one kind of instruction
in Python. The other kind are statements.
139

139

00:09:22,070  -->  00:09:25,810
Unlike expressions, statements don't
evaluate to single value.
140

140

00:09:25,810  -->  00:09:29,850
Although an assignment statement can
have an expression as part of itself.
141

141

00:09:29,850  -->  00:09:33,450
Let's try typing
142

142

00:09:33,450  -->  00:09:38,070
spam = 2 + 2. The 2 + 2
expression
143

143

00:09:38,070  -->  00:09:41,640
evaluates to 4 and so 4 is the value
that gets
144

144

00:09:41,640  -->  00:09:45,030
assigned to spam. We can even use the
variable itself
145

145

00:09:45,030  -->  00:09:48,260
to set the variable's new value. Try
typing
146

146

00:09:48,260  -->  00:09:54,000
spam = 10 and then spam = spam + 1.
147

147

00:09:54,000  -->  00:09:56,260
This assigns spam a new value.
148

148

00:09:56,260  -->  00:09:59,570
The new value for spam is equal to the
current value of spam,
149

149

00:09:59,570  -->  00:10:03,170
plus 1. Spam, spam, spam, variable, spam.
150

150

00:10:03,170  -->  00:10:07,930
So if a Python instruction
evaluates to a single value,
151

151

00:10:07,930  -->  00:10:12,510
it's an expression. Otherwise it's a
statement. But mostly we just generically
152

152

00:10:12,510  -->  00:10:14,270
called them instructions or code.
153

153

00:10:14,270  -->  00:10:17,860
To recap: IDLE is the editor program that
you type code into.
154

154

00:10:17,860  -->  00:10:21,300
It has two parts: the interactive shell
which runs code
155

155

00:10:21,300  -->  00:10:24,510
one instruction at a time, and the file
editor
156

156

00:10:24,510  -->  00:10:30,220
where you'll take complete programs into.
Expressions are made up of values and
157

157

00:10:30,220  -->  00:10:33,280
operators and they always evaluate down
to one value.
158

158

00:10:33,280  -->  00:10:36,580
And every value belongs to exactly one
data type.
159

159

00:10:36,580  -->  00:10:41,000
The data types we've seen so far are
integers, floating point numbers, and strings.
160

160

00:10:41,090  -->  00:10:45,740
And strings contain text, and they start
and end with a single quote character.
161

161

00:10:45,740  -->  00:10:49,890
And values can be stored in variables,
which can then be used in expressions
162

162

00:10:49,890  -->  00:10:51,440
anywhere values are used.
