1
1

00:00:00,220  -->  00:00:03,470
That's enough for the interactive
shell for now. Let's write our first program.
2

2

00:00:03,470  -->  00:00:06,480
In IDLE, click on File > New File.
3

3

00:00:06,480  -->  00:00:09,820
This makes the file editor window appear.
4

4

00:00:09,820  -->  00:00:13,509
Enter the following code: I'll ex[plain what it  does afterwards.
5

5

00:00:13,509  -->  00:00:16,970
# This program says hello
6

6

00:00:16,970  -->  00:00:20,810
and asks for
7

7

00:00:20,810  -->  00:00:24,800
my name. Add a blank line, followed by
8

8

00:00:24,800  -->  00:00:27,880
print('Hello world!')
9

9

00:00:27,880  -->  00:00:33,719
print('What is your name?')
10

10

00:00:33,719  -->  00:00:39,870
# ask for their name
11

11

00:00:39,870  -->  00:00:44,960
myName = input(), print(
12

12

00:00:44,960  -->  00:00:48,370
'It is good to meet you, ' + myName
13

13

00:00:48,370  -->  00:00:52,680
print('The length of your name is:')
14

14

00:00:52,680  -->  00:00:55,760
print(
15

15

00:00:55,760  -->  00:00:58,770
len(myName))
16

16

00:00:58,770  -->  00:01:02,730
print('What is your age?')
17

17

00:01:02,730  -->  00:01:11,000
# ask for their age
18

18

00:01:11,080  -->  00:01:14,400
myAge = input()
19

19

00:01:14,400  -->  00:01:18,140
print('You will be ' +
20

20

00:01:18,140  -->  00:01:21,950
str(int(myAge))
21

21

00:01:21,950  -->  00:01:26,619
+ 1 +
22

22

00:01:26,619  -->  00:01:38,830
' in a year.' Save this program as hello.py.
23

23

00:01:38,830  -->  00:01:45,600
Python programs are just text files that have the .py
24

24

00:01:45,670  -->  00:01:50,400
file extension. Then click on Run >
25

25

00:01:50,400  -->  00:01:53,850
Run Module or just press F5 to run the
program.
26

26

00:01:53,850  -->  00:01:57,710
"Hello world! What is your name?" Al.
27

27

00:01:57,710  -->  00:02:00,840
"It is good to meet you Al. The length of your name is
28

28

00:02:00,840  -->  00:02:04,729
2. What is your age?" Thirt-
29

29

00:02:04,729  -->  00:02:09,350
26. Yeeeah.
30

30

00:02:09,350  -->  00:02:13,620
I am 26. You will be 27 in a year."
31

31

00:02:13,620  -->  00:02:16,780
Ah, I remember when that was true.
32

32

00:02:16,780  -->  00:02:20,060
Okay, that's the entire program. One thing
to keep in mind
33

33

00:02:20,060  -->  00:02:24,250
is that Python will start at the first
instruction at the top of the program,
34

34

00:02:24,250  -->  00:02:27,600
and then move downwards executing each
instruction entered
35

35

00:02:27,600  -->  00:02:30,959
Think of it as putting your finger on the top
instruction
36

36

00:02:30,959  -->  00:02:34,500
and as each instruction is executed
moving your finger
37

37

00:02:34,500  -->  00:02:37,920
down to the next instruction. This is just
like how you might move your finger
38

38

00:02:37,920  -->  00:02:39,900
along the lines of a book that you're reading.
39

39

00:02:39,900  -->  00:02:44,110
Your finger is called the execution. The
execution
40

40

00:02:44,110  -->  00:02:47,750
in Python starts at the top and moves
down.
41

41

00:02:47,750  -->  00:02:50,790
So the first line with the
42

42

00:02:50,790  -->  00:02:54,950
pound sign is called a "comment". Python ignores comments.
43

43

00:02:54,950  -->  00:02:57,980
You can use them to write notes to yourself or reminders
44

44

00:02:57,980  -->  00:03:02,100
about what the code is trying to do or
what the entire program does.
45

45

00:03:02,100  -->  00:03:06,560
Blank lines are completely ignored by
Python. You can just use these to space
46

46

00:03:06,560  -->  00:03:07,459
out your code.
47

47

00:03:07,459  -->  00:03:11,160
In fact, let's add a couple more blank
line right here
48

48

00:03:11,160  -->  00:03:14,230
just to group together the
49

49

00:03:14,230  -->  00:03:17,640
code that's relevant to asking for your
name, and
50

50

00:03:17,640  -->  00:03:24,420
the code that ask your age. The
51

51

00:03:24,420  -->  00:03:28,329
print() function displays the string value
inside the parentheses
52

52

00:03:28,329  -->  00:03:32,250
on the screen. So the line print('Hello
world!')
53

53

00:03:32,250  -->  00:03:35,780
means print out the text in the string
'Hello World!'.
54

54

00:03:35,780  -->  00:03:39,840
Functions in Python are like mini-programs in your program.
55

55

00:03:39,840  -->  00:03:43,760
They contain code that you various things
and for your convenience Python
56

56

00:03:43,760  -->  00:03:44,989
comes with a lot of them.
57

57

00:03:44,989  -->  00:03:49,340
In code, a function is its name,
58

58

00:03:49,340  -->  00:03:52,400
followed by parentheses and
optionally
59

59

00:03:52,400  -->  00:03:55,640
sometimes there are values passed
60

60

00:03:55,640  -->  00:03:59,340
to the function inside the parentheses.
61

61

00:03:59,340  -->  00:04:02,670
In this context these values are called
62

62

00:04:02,670  -->  00:04:06,329
arguments. But really, values and arguments are the exact same thing.
63

63

00:04:06,329  -->  00:04:10,560
This is called calling the print()
function.
64

64

00:04:10,560  -->  00:04:15,190
We have code like this here. So the next
instruction is also a call
65

65

00:04:15,190  -->  00:04:19,209
the print() function. This time it's
passing the 'What is your name?'
66

66

00:04:19,209  -->  00:04:24,789
string to the print() function. There's
also a comment at the very end of the
67

67

00:04:24,789  -->  00:04:26,419
line. You can always add comments
68

68

00:04:26,419  -->  00:04:29,740
to the end of an instruction since everything after that
69

69

00:04:29,740  -->  00:04:35,020
pound sign will just be ignored by Python.
When the input function
70

70

00:04:35,020  -->  00:04:38,870
is called, Python waits for the user
to type some text on a keyboard and
71

71

00:04:38,870  -->  00:04:39,810
press Enter.
72

72

00:04:39,810  -->  00:04:43,560
Just like a variable will evaluate to
the value it contains,
73

73

00:04:43,560  -->  00:04:47,729
calls to the input() function evaluate to
the string value
74

74

00:04:47,729  -->  00:04:52,000
of what the user typed. So if I typed in
75

75

00:04:52,000  -->  00:04:55,910
"Al" when input() was called, this
76

76

00:04:55,910  -->  00:05:00,889
myName variable assignment statement
would just evaluate to 'Al'.
77

77

00:05:00,889  -->  00:05:07,800
And then the string will get stored
inside myName.
78

78

00:05:07,800  -->  00:05:11,740
Remember, expressions can always evaluate down to a single value.
79

79

00:05:11,740  -->  00:05:15,580
So if 'Al' is stored in myName
80

80

00:05:15,580  -->  00:05:19,419
and this is what that call to the
print() function looks like
81

81

00:05:19,419  -->  00:05:23,100
first my name evaluates to the value
inside it
82

82

00:05:23,100  -->  00:05:26,110
and then, this is just a string
concatenation,
83

83

00:05:26,110  -->  00:05:30,610
so the next step looks just like this:
84

84

00:05:30,610  -->  00:05:35,970
This is the string that gets passed to the
print() function call.
85

85

00:05:35,970  -->  00:05:38,919
This next line introduces a new function
called
86

86

00:05:38,919  -->  00:05:43,150
len(). What len() does is it takes a string
87

87

00:05:43,150  -->  00:05:46,340
argument and then evaluates
88

88

00:05:46,340  -->  00:05:50,050
to the integer value of the length of the string,
89

89

00:05:50,050  -->  00:05:54,360
which is the number characters in a
string. Let's just experiment with this
90

90

00:05:54,360  -->  00:05:57,490
function in the interactive shell for a
little bit.
91

91

00:05:57,490  -->  00:06:00,870
so I can call the len() function
and pass it
92

92

00:06:00,870  -->  00:06:05,259
'Al' and it returns 2.
93

93

00:06:05,259  -->  00:06:08,259
Or 'Albert' to return 6. Or
94

94

00:06:08,259  -->  00:06:11,960
length... the blank string and that returns
0.
95

95

00:06:11,960  -->  00:06:17,110
and then I can just use these function calls to len() anywhere
96

96

00:06:17,110  -->  00:06:20,550
I could using integer. So I could say, you
know,
97

97

00:06:20,550  -->  00:06:25,580
type out my name ten times, it's
length would be 20.
98

98

00:06:25,580  -->  00:06:29,530
So then we have some more print() and
input() calls.
99

99

00:06:29,530  -->  00:06:32,560
This time we're saving whatever the user
types
100

100

00:06:32,560  -->  00:06:35,930
into the variable myAge. And then we
have this line,
101

101

00:06:35,930  -->  00:06:39,569
which introduces the str()and int()
functions.
102

102

00:06:39,569  -->  00:06:44,139
Now this seems really complicated, so we'll just take this one step at a time
103

103

00:06:44,139  -->  00:06:48,750
The str() and int() functions return
string and integer
104

104

00:06:48,750  -->  00:06:53,039
values of whatever you pass them. This is
really handy if you need to convert
105

105

00:06:53,039  -->  00:06:57,199
between data types. So in the interactive shell
106

106

00:06:57,199  -->  00:07:00,699
you can have something like str()
107

107

00:07:00,699  -->  00:07:05,370
of the value 26, and that returns a string
value
108

108

00:07:05,370  -->  00:07:08,880
with 26 in it. And if you have to go the opposite route,
109

109

00:07:08,880  -->  00:07:12,229
you can call the int() function and pass it
110

110

00:07:12,229  -->  00:07:18,919
a string value like '1234'
and it will return in integer value.
111

111

00:07:18,919  -->  00:07:22,599
There's also a float() function that you
can use
112

112

00:07:22,599  -->  00:07:26,870
to convert something to a floating-point
value.
113

113

00:07:26,870  -->  00:07:30,310
So you can pass a string to it, it'll return a float value.
114

114

00:07:30,310  -->  00:07:34,080
Or if you pass an integer value to it, it
will return
115

115

00:07:34,080  -->  00:07:38,409
a floating-point version value of that
116

116

00:07:38,409  -->  00:07:41,990
integer. Notice it has the decimal
point and .0.
117

117

00:07:41,990  -->  00:07:46,240
This may seem pointless but remember
that the input() function always returns a
118

118

00:07:46,240  -->  00:07:49,220
string value, even if the string value something like
119

119

00:07:49,220  -->  00:07:52,590
'26'. We can't do math on a string.
120

120

00:07:52,590  -->  00:07:57,860
That returns an error. First we have
to get in integer value
121

121

00:07:57,860  -->  00:08:01,720
of it. So that's why we passed it to the
122

122

00:08:01,720  -->  00:08:05,880
int()function. And just like you can't do
addition on strings, you can't do string
123

123

00:08:05,880  -->  00:08:12,100
concatenation on integers.
124

124

00:08:12,130  -->  00:08:17,640
So we're going to have to call the str() function
125

125

00:08:17,640  -->  00:08:22,430
to convert that into a string value. This is
What the
126

126

00:08:22,430  -->  00:08:25,570
entire last line
127

127

00:08:25,570  -->  00:08:29,190
looks like when it- when Python evaluates it. First
128

128

00:08:29,190  -->  00:08:32,810
the myAge variable will evaluate to the
129

129

00:08:32,810  -->  00:08:36,289
value that's inside of it. Then that gets
passed to the
130

130

00:08:36,289  -->  00:08:39,310
int() function, which returns an integer
form of it.
131

131

00:08:39,310  -->  00:08:43,770
We add 1 to that integer. We have to convert that
132

132

00:08:43,770  -->  00:08:47,060
back to a string value, because we
want to concatenate
133

133

00:08:47,060  -->  00:08:50,290
the other streets next to it. And then
after that, it's just the string
134

134

00:08:50,290  -->  00:08:51,220
concatenation.
135

135

00:08:51,220  -->  00:08:56,980
And so
136

136

00:08:56,980  -->  00:09:00,600
all this just reduces down
137

137

00:09:00,600  -->  00:09:03,710
to this string, which we pass to the
print() function call,
138

138

00:09:03,710  -->  00:09:08,600
which then gets it displayed on the
screen. That's how the entire program works.
139

139

00:09:08,610  -->  00:09:14,339
Just to recap, the file editor is where
you type in code for a program.
140

140

00:09:14,339  -->  00:09:17,810
The execution starts at the top and then
proceeds
141

141

00:09:17,810  -->  00:09:21,480
down, executing each instruction in turn.
142

142

00:09:21,480  -->  00:09:24,520
Comments begin with a pound sign
143

143

00:09:24,520  -->  00:09:28,200
and are just notes that are ignored by
Python. They're just notes for the
144

144

00:09:28,200  -->  00:09:29,070
programmer.
145

145

00:09:29,070  -->  00:09:34,520
Functions are kind of like mini programs
that your program can run
146

146

00:09:34,520  -->  00:09:38,140
to execute some code that does some
specialized thing. In this case
147

147

00:09:38,140  -->  00:09:43,030
the print() function care has code for
making strings appear on the screen.
148

148

00:09:43,030  -->  00:09:46,120
There's a print() function for
149

149

00:09:46,120  -->  00:09:50,220
making text appear on screen. There's an input()
function for getting text from the
150

150

00:09:50,220  -->  00:09:51,040
keyboard.
151

151

00:09:51,040  -->  00:09:54,430
And there's also a str(), int(),
152

152

00:09:54,430  -->  00:09:57,900
and a float() function that returns an
integer, string,
153

153

00:09:57,900  -->  00:10:00,450
or float version the values that we
passed them.
