Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:07,940 --> 00:00:13,019
In this lesson, we're going to go over some of the basics of writing Python programs.
2
00:00:13,019 --> 00:00:16,530
Part of the reason that I really like Python as a first language is
3
00:00:16,530 --> 00:00:20,460
that it doesn't take much to actually write a simple Python program.
4
00:00:20,460 --> 00:00:22,890
So, when we write a Python program,
5
00:00:22,890 --> 00:00:28,635
we are going to write our Python code in this window,
6
00:00:28,635 --> 00:00:31,850
and the code that we write is going to go into
7
00:00:31,850 --> 00:00:40,850
a Python interpreter which
8
00:00:40,850 --> 00:00:43,550
is a program that's running behind the scenes,
9
00:00:43,550 --> 00:00:45,830
but it takes what we write in
10
00:00:45,830 --> 00:00:51,840
our code window and interprets it and then depending on what write,
11
00:00:51,840 --> 00:00:54,450
it might send something to the output.
12
00:00:54,450 --> 00:00:57,130
So, again this is the source code,
13
00:00:58,910 --> 00:01:08,320
this is the output and the Python Interpreter looks at our source code.
14
00:01:10,850 --> 00:01:15,155
So, we've already seen a Hello World Python program,
15
00:01:15,155 --> 00:01:18,190
but I'm going to write a program that's even simpler.
16
00:01:18,190 --> 00:01:21,175
This is a valid Python program.
17
00:01:21,175 --> 00:01:23,380
So, if I click save and run,
18
00:01:23,380 --> 00:01:26,720
then you aren't going to actually see anything in the output program,
19
00:01:26,720 --> 00:01:31,210
but Python did actually interpret and run this program correctly.
20
00:01:31,210 --> 00:01:35,330
So, remember that there's this hidden python interpreter,
21
00:01:35,330 --> 00:01:37,480
and when we actually ran this code,
22
00:01:37,480 --> 00:01:39,640
the Python interpreter looked at our source code,
23
00:01:39,640 --> 00:01:43,520
ran it and just saw that there was no output for it.
24
00:01:43,770 --> 00:01:46,370
So, when we write code,
25
00:01:46,370 --> 00:01:50,380
we're actually writing what are called expressions.
26
00:01:53,170 --> 00:01:56,450
Learning to program is in part learning
27
00:01:56,450 --> 00:02:01,745
the right expression to write for what we actually want to compute.
28
00:02:01,745 --> 00:02:05,230
So, 100 is an expression,
29
00:02:05,230 --> 00:02:07,305
and when we write expressions,
30
00:02:07,305 --> 00:02:13,430
what the Python interpreter does is it computes the value of those expressions.
31
00:02:13,430 --> 00:02:15,589
So, we have expressions,
32
00:02:15,589 --> 00:02:18,150
every expression has a value.
33
00:02:19,400 --> 00:02:23,900
So, in the case of the expression 100,
34
00:02:23,900 --> 00:02:28,355
the value of the expression 100 is just 100,
35
00:02:28,355 --> 00:02:33,310
and then every value also has what's called a type.
36
00:02:33,310 --> 00:02:36,500
A type is like a category of data.
37
00:02:36,500 --> 00:02:40,680
So, the type of 100 is an integer.
38
00:02:41,140 --> 00:02:45,090
An integer is just a round number.
39
00:02:45,860 --> 00:02:52,090
So, I'm going to add onto this program by writing another expression.
40
00:02:52,090 --> 00:02:56,350
So, here's another expression 3.14.
41
00:02:56,350 --> 00:02:59,365
Again, when I run my program, nothing shows up,
42
00:02:59,365 --> 00:03:02,590
but what's happening is that behind the scenes,
43
00:03:02,590 --> 00:03:07,670
the Python Interpreter is looking at my code and deciding what to output.
44
00:03:09,600 --> 00:03:12,890
So, here we have two expressions,
45
00:03:12,890 --> 00:03:15,675
the first expression is 100,
46
00:03:15,675 --> 00:03:19,780
the second expression is 3.14.
47
00:03:24,300 --> 00:03:27,910
Both of these expressions have a value.
48
00:03:27,910 --> 00:03:34,210
So, the value of 100 is 100.
49
00:03:34,210 --> 00:03:42,740
The value of 3.14 is 3.14 and both of those values have types.
50
00:03:44,070 --> 00:03:49,010
So, the type of 100 is an integer.
51
00:03:50,660 --> 00:03:53,585
The type of 3.14,
52
00:03:53,585 --> 00:03:55,120
because it has a decimal place,
53
00:03:55,120 --> 00:03:56,755
is something called a float,
54
00:03:56,755 --> 00:03:59,540
short for floating point number.
55
00:04:03,170 --> 00:04:07,780
So, we can write expressions that compute all the values that we want.
56
00:04:07,780 --> 00:04:11,765
In Python, we'll do these computations behind the scenes,
57
00:04:11,765 --> 00:04:14,380
again with this Python Interpreter.
58
00:04:14,380 --> 00:04:19,190
But typically, we want our programs to actually give us some feedback.
59
00:04:19,190 --> 00:04:25,990
We want to do something and then give us back some value in the output window.
60
00:04:25,990 --> 00:04:29,030
In other words, we don't just want our source code to go
61
00:04:29,030 --> 00:04:31,745
to the Python Interpreter and get no output,
62
00:04:31,745 --> 00:04:36,815
we usually actually want some output or some feedback from our programs.
63
00:04:36,815 --> 00:04:38,390
In order to do that,
64
00:04:38,390 --> 00:04:43,160
what we need to do is we need to use what are called print statements.
65
00:04:43,160 --> 00:04:47,690
So, a print statement is a special kind
66
00:04:47,690 --> 00:04:52,870
of expression that tells something to show up in our output window.
67
00:04:52,870 --> 00:04:57,260
So, I'm going to modify my source code to say rather than
68
00:04:57,260 --> 00:05:01,295
just computing the value to also print out the value.
69
00:05:01,295 --> 00:05:04,370
The way that I do that is I say, print,
70
00:05:04,370 --> 00:05:08,090
open parentheses and then the value of
71
00:05:08,090 --> 00:05:11,870
whatever expression I want to print out and then close parentheses.
72
00:05:11,870 --> 00:05:17,670
So, if I only say print 100 and I run my program,
73
00:05:18,530 --> 00:05:21,325
then I'll see that in my output window,
74
00:05:21,325 --> 00:05:27,950
I get 100 because I specified that I want to print out the value of this expression.
75
00:05:27,950 --> 00:05:32,105
So, notice here that print 100 prints out 100,
76
00:05:32,105 --> 00:05:35,345
but when I say just the expression 3.14,
77
00:05:35,345 --> 00:05:37,735
then this doesn't lead to any output.
78
00:05:37,735 --> 00:05:40,850
In order to actually print out the value of this expression,
79
00:05:40,850 --> 00:05:44,280
I also would need to add a print statement here.
80
00:05:48,890 --> 00:05:51,360
Now, when I run my code,
81
00:05:51,360 --> 00:05:56,100
then you'll see that the value of both of these expressions is printed.
82
00:05:56,470 --> 00:06:00,455
So far, we have seen two kinds of types.
83
00:06:00,455 --> 00:06:03,380
We've seen integers, which are round numbers like
84
00:06:03,380 --> 00:06:09,785
100 and floats which are decimal point numbers like 3.14.
85
00:06:09,785 --> 00:06:15,200
Another type that we've actually seen in an earlier lecture was a string.
86
00:06:15,200 --> 00:06:20,370
So, when we said, print Hello world,
87
00:06:20,900 --> 00:06:26,475
then this expression Hello world is a string.
88
00:06:26,475 --> 00:06:31,070
A string is a sequence of characters and if I run this program,
89
00:06:31,070 --> 00:06:37,560
then I'll see that Hello world is what actually gets printed out when I run mine too.
90
00:06:38,170 --> 00:06:47,390
So, so far, we've seen the types integers like 100,
91
00:06:47,390 --> 00:07:02,230
strings like Hello world and floats,
92
00:07:02,230 --> 00:07:08,800
it's short for floating point number like 3.14.
93
00:07:13,280 --> 00:07:17,745
In all of these cases, these 100,
94
00:07:17,745 --> 00:07:22,990
the string Hello world or 3.14 are all expressions.
95
00:07:25,820 --> 00:07:29,975
Now, expressions are like the building blocks of programming.
96
00:07:29,975 --> 00:07:34,820
We can combine and reuse expressions to get more and more complicated programs.
97
00:07:34,820 --> 00:07:40,430
So, the expressions that we've written so far are what are called literal expressions,
98
00:07:40,430 --> 00:07:43,550
where the value is the same as the expression itself,
99
00:07:43,550 --> 00:07:46,520
so the value of the expression 100 is 100,
100
00:07:46,520 --> 00:07:50,750
the value of the expression 3.14 is 3.14.
101
00:07:50,750 --> 00:07:53,870
Let's move on to operators to see how we can combine
102
00:07:53,870 --> 00:07:57,840
expressions to get more complicated expressions.8511
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.