Syntax Errors |Section 2|Celestial Warrior

1 year ago
67

1

00:00:00,319 --> 00:00:06,270

Hey welcome to this new lecture! This is

the most important lecture you have had

3

00:00:06,270 --> 00:00:12,240

so far in this course, so please try to

make the most of it and this is actually

5

00:00:12,240 --> 00:00:15,780

a new video that I am adding to the

course. I didn't have this lecture a few

7

00:00:15,780 --> 00:00:22,320

times ago so I had this quite a while

after I published the course and until

9

00:00:22,320 --> 00:00:26,550

now I had explained this concept, so the

errors little by little in other

11

00:00:26,550 --> 00:00:32,610

lectures but I felt that was not enough

and I was seeing that students were

13

00:00:32,610 --> 00:00:37,140

still having difficulties in

understanding and dealing with errors in

15

00:00:37,140 --> 00:00:44,070

Python so I decided to make a dedicated

lecture for errors in Python. So here we

17

00:00:44,070 --> 00:00:48,629

are and I said this is the most

important lecture because you know we

19

00:00:48,629 --> 00:00:54,510

we learnt about functions and strings

and numbers until now in the previous

21

00:00:54,510 --> 00:01:00,480

videos and these are individual concepts

that you know you'll learn, now or later you'll

23

00:01:00,480 --> 00:01:05,760

learn functions and strings and etc, but

if you don't know how to read an error

25

00:01:05,760 --> 00:01:11,220

how to to understand an error and deal

with it if you don't know that you will

27

00:01:11,220 --> 00:01:15,930

have trouble dealing with everything

every other object like functions and

29

00:01:15,930 --> 00:01:22,560

streams and numbers etc. So understanding

and error and knowing how to deal with

31

00:01:22,560 --> 00:01:28,350

it, how to fix it is very important and

even the most experienced programmers

33

00:01:28,350 --> 00:01:34,140

they make errors in their programs. The

difference is that they know how to read

35

00:01:34,140 --> 00:01:39,420

these errors. How to understand and

fix them, but everyone makes errors. So if

37

00:01:39,420 --> 00:01:44,460

an error shows in your program don't

panic just for all the instructions that

39

00:01:44,460 --> 00:01:48,840

I'll give you in this video on how to

fix the error so that's what this

41

00:01:48,840 --> 00:01:54,930

lecture is about. So what is an error

anyway? An error in a program is a bug

43

00:01:54,930 --> 00:02:00,509

that causes the program to function

incorrectly. Now in Python we have

45

00:02:00,509 --> 00:02:06,229

basically two types of errors. We have

syntax errors and we have exceptions.

47

00:02:06,229 --> 00:02:10,950

Let's first focus on syntax errors and

let's forget about exceptions. We'll go

49

00:02:10,950 --> 00:02:16,840

to exceptions after we explain

syntax errors. I have this code here and

51

00:02:16,840 --> 00:02:23,019

actually this is a Python file. This is

the icon showing on a Mac so currently

53

00:02:23,019 --> 00:02:27,909

I am on a Mac computer but that doesn't

make the slightest difference. No matter

55

00:02:27,909 --> 00:02:33,280

where you are on Linux Windows or Mac

everything is the same. So I have the

57

00:02:33,280 --> 00:02:37,750

terminal here and let me go ahead and

execute this program. This has a few

59

00:02:37,750 --> 00:02:44,890

errors so let me show them. On Mac you

can call Python 3 with the python3

61

00:02:44,890 --> 00:02:49,870

command, on Windows you can just call

python or whatever command you are

63

00:02:49,870 --> 00:02:56,049

using. You can also use any editor that

you like and so errors.py is the

65

00:02:56,049 --> 00:03:07,290

name I gave to the script, execute.

So this is now an example of a syntax error

67

00:03:07,290 --> 00:03:14,230

and this is the entire body of the error

message and this is very important, so

69

00:03:14,230 --> 00:03:19,450

whenever you get an error the first

advice I give you is don't panic just

71

00:03:19,450 --> 00:03:23,730

focus on the error. Read it line by

line just like you're reading a poem or

73

00:03:23,730 --> 00:03:32,739

whatever so the first line of the error

points you to the name of the file that

75

00:03:32,739 --> 00:03:37,840

has the error. In this case is errors.py.

Then you have a comma and after

77

00:03:37,840 --> 00:03:40,620

the comma you have the line where the

error occurred.

79

00:03:40,620 --> 00:03:47,169

So it's line 3 and you can see that line

3 is this one here but you also have for

81

00:03:47,169 --> 00:03:53,379

your convenience Python prints out the

line in the terminal so int 9. Here is

83

00:03:53,379 --> 00:03:59,199

where the error is and just after that

you have the type of the error so it's a

85

00:03:59,199 --> 00:04:03,849

syntax error, you also have other types

of errors such as name error, type error,

87

00:04:03,849 --> 00:04:07,780

but those are exceptions and I'll

explain later why we call them

89

00:04:07,780 --> 00:04:12,519

exceptions, so this is one type of error

a syntax error and the others are

91

00:04:12,519 --> 00:04:18,900

exceptions. So this is the type and then

you have the description after the colon.

93

00:04:18,900 --> 00:04:24,990

Sometimes the description is more

specific more detailed. This time is just

95

00:04:24,990 --> 00:04:29,870

invalid syntax.

So you have to figure out where

97

00:04:29,870 --> 00:04:36,259

you did some error where you

missed some syntax of your program.

99

00:04:36,259 --> 00:04:45,080

And you also have this arrow here pointing

upwards and the arrow points you even at the

101

00:04:45,080 --> 00:04:51,770

token that the error is occurring or at

the end of the token, so a token is you

103

00:04:51,770 --> 00:04:58,190

know, this is the token in this case 9.

So it can be a number, it can be string,

105

00:04:58,190 --> 00:05:04,400

anything, so at this point this is

pointing at this, at the token but

107

00:05:04,400 --> 00:05:11,780

it could also be like that.

999 save the script, execute. In this

109

00:05:11,780 --> 00:05:16,220

case you see that it's pointing you at

the end of the token. So here's, around

111

00:05:16,220 --> 00:05:21,729

here you have an error now and I know

that int is actually a function and

113

00:05:21,729 --> 00:05:29,780

functions important they need to have

brackets so you need to pass 999 inside

115

00:05:29,780 --> 00:05:42,650

brackets like that and you leave that.

Save and execute again. All right, you've

117

00:05:42,650 --> 00:05:49,729

got another error but don't panic again.

This is my advice always, instead

119

00:05:49,729 --> 00:05:54,979

read the error it says file errors.py

the file line 5, oh line 5 this time.

121

00:05:54,979 --> 00:06:02,210

So it's not line 3 anymore which means

that line 3 was fixed so what Python is

123

00:06:02,210 --> 00:06:06,949

doing is it's going through all the

lines one by one from top to bottom, it

125

00:06:06,949 --> 00:06:11,330

checks the first line it says: "Oh it's

fine, no error there". Goes to the next

127

00:06:11,330 --> 00:06:16,130

line no error, goes through the third

line, no error this time, fourth line is

129

00:06:16,130 --> 00:06:21,860

also fine, and fifth line has an error so

again it's a syntax error and this one

131

00:06:21,860 --> 00:06:28,630

is more specifically described so

missing parentheses in call to print.

133

00:06:28,630 --> 00:06:33,560

Note that you get this error only if you

are on Python 3. If you are on Python 2

135

00:06:33,560 --> 00:06:40,810

this won't be an error because in Python

2 print was a statement and was not

137

00:06:40,810 --> 00:06:46,210

function so with statements like

return or print in Python - you didn't

139

00:06:46,210 --> 00:06:49,150

have to pass brackets. This syntax would

be correct.

141

00:06:49,150 --> 00:06:56,190

So again this arrow here is pointing you

and the token so we just add

143

00:06:56,190 --> 00:07:04,810

brackets there, ctrl s to save the script

and execute again. This time we don't get

145

00:07:04,810 --> 00:07:09,280

any error so the script executed fine

and print it out the output here.

147

00:07:09,280 --> 00:07:15,400

1 2 3 so we had 3 print functions

printing out output. This doesn't return

149

00:07:15,400 --> 00:07:21,340

any output because we're not printing

anything so this just gets the function

151

00:07:21,340 --> 00:07:26,410

input and produces some output but

doesn't print anything. If you want to

153

00:07:26,410 --> 00:07:30,550

print it, you want to pass the print

statement function there. There could

155

00:07:30,550 --> 00:07:36,940

also be other types of syntax errors and

you know such as you want to define a

157

00:07:36,940 --> 00:07:44,800

least 1, 2, 3, but instead of

closing it with a square brackets, you use

159

00:07:44,800 --> 00:07:50,260

a round bracket instead like that. Save

the script and execute and you see that

161

00:07:50,260 --> 00:07:58,030

you got a syntax error, invalid syntax at

line 5 which is this one in here. Again

163

00:07:58,030 --> 00:08:04,780

the arrow points you at the token so you

need to figure out how to fix this round

165

00:08:04,780 --> 00:08:11,020

bracket here and you know that. You need

to close it with a square bracket so

167

00:08:11,020 --> 00:08:15,760

that should fix the issue. So these are

Ao that's about syntax errors. They are

169

00:08:15,760 --> 00:08:22,000

very easy to fix. In the next lecture I

will explain exceptions so see you there!

Loading comments...