Loops |Section 4|Celestial Warrior
1
00:00:00,050 --> 00:00:06,420
Hey, in this lecture you'll learn about loops
and loops are very important.
3
00:00:06,420 --> 00:00:10,590
They are one of the fundamental building
blocks of the Python programming language
5
00:00:10,590 --> 00:00:15,809
and of every other programming language
as well because loops are crucial.
7
00:00:15,809 --> 00:00:21,630
So what are loops? Well loops are basically
the way that allows you to execute the
9
00:00:21,630 --> 00:00:26,789
statement multiple times, so what does
that mean? Let's say you have a bunch of
11
00:00:26,789 --> 00:00:32,040
files, text files and you want to insert
a certain string inside each of these
13
00:00:32,040 --> 00:00:37,140
text files, so you'd have to open those
text files in Python by writing the
15
00:00:37,140 --> 00:00:42,090
Python code that corresponds to opening
a file and then you insert that string
17
00:00:42,090 --> 00:00:46,649
inside the file and close the file and
save it, so there is some code that does
19
00:00:46,649 --> 00:00:52,199
this opening and inserting the text and
closing the file and if you had 100
21
00:00:52,199 --> 00:00:57,570
files you'd have to execute that code
100 times so you'd come up with let's
23
00:00:57,570 --> 00:01:03,480
say 100 lines of code but that doesn't
sound very efficient so that's what loops
25
00:01:03,480 --> 00:01:08,970
are for. So a productive way to solve the
problem that I just mentioned would be
27
00:01:08,970 --> 00:01:13,530
to create a list of these files so
Python lists all of the file names and
29
00:01:13,530 --> 00:01:20,310
then you would use a loop to iterate
through this list of file names and a loop
31
00:01:20,310 --> 00:01:25,439
would do all that in just a few lines of
code and there are two kinds of loops in
33
00:01:25,439 --> 00:01:30,240
Python, there is a for loop and a problem
for example that I just mentioned would
35
00:01:30,240 --> 00:01:36,600
be solved using a for loop and we also
have the while loop. A while loop does
37
00:01:36,600 --> 00:01:41,700
also repeatedly execute a statement
that you give, that you write and it
39
00:01:41,700 --> 00:01:47,280
executes it as long as the given conditional
true, so let's say you have a user that
41
00:01:47,280 --> 00:01:52,770
is trying to login in your web app and
while he keeps entering their own
43
00:01:52,770 --> 00:01:59,130
password, you'd continue executing a
statement that would reject his login to
45
00:01:59,130 --> 00:02:04,049
your system, so while the password is
wrong reject the user, something like that.
47
00:02:04,049 --> 00:02:09,149
So that's about the definition of
the for loop and the while loop. In our
49
00:02:09,149 --> 00:02:14,370
next lecture I'll show you the syntax of
the for loop and by also looking at an example,
51
00:02:14,370 --> 00:02:18,329
so see you in the next lecture.
5
views
Conditionals - Advanced |Section 3|Celestial Warrior
In one of the previous exercises we created the following function:
def string_length(mystring):
return len(mystring)
Calling the function with a string as the value for the argument mystringwill return the length of that string.
However, if an integer is passed as an argument value:
string_length(10)
that wouldgenerate an error since the len() function doesn't work for integers.
Your duty is to modify the function so that if an integer is passed as an input, the function should output a message like "Sorry integers don't have length".
def string_length(mystring):
if type(mystring) == int:
return "Sorry, integers don't have length"
else:
return len(mystring)
The function that we implemented in the previous exercise checks integer datatypes, but not about floats. So, please expand the conditional block so that floats are counted too.
def string_length(mystring):
if type(mystring) == int:
return "Sorry, integers don't have length"
elif type(mystring) == float:
return "Sorry, floats don't have length"
else:
return len(mystring)
In one of the previous exercisesyou created a function that convertedCelsius degrees to Fahrenheit:
def cel_to_fahr(c):
f = c * 9/5 + 32
return f
Now, the lowest possibletemperature that physicalmatter can reach is -273.15C.With that in mind, please improve the function by making itprint out a message in case a number lower than -273.15 is passed as input when calling the function.
def c_to_f(c):
if c< -273.15:
return "That temperature doesn't make sense!"
else:
f=c*9/5+32
return f
print(c_to_f(-273.4))
4
views
Conditionals |Section 3|Celestial Warrior
1
00:00:00,620 --> 00:00:05,720
Hey, hello again. In this lecture you're going to learn
about conditionals. What is a conditional?
3
00:00:07,980 --> 00:00:14,190
Well a conditional is a block of code which
performs different actions depending on
5
00:00:14,190 --> 00:00:19,980
whether a given conditional evaluates to
true or false so let's go ahead and
7
00:00:19,980 --> 00:00:29,340
create a conditional in Python. I'll create the
file conditionals dot py and let me
9
00:00:29,340 --> 00:00:35,280
create a conditional block, and first of
all let me create a variable. Let's say a
11
00:00:35,280 --> 00:00:40,920
equals five and here I'll create a
conditional block. This block, what this
13
00:00:40,920 --> 00:00:48,780
will do is it will perform an action if
a is less than five and another action
15
00:00:48,780 --> 00:00:54,930
if a is equal or greater than five
so let me create that. If this, our
17
00:00:54,930 --> 00:01:04,049
conditional starts if a and let's say
less than five print, let's say less than 5.
19
00:01:04,049 --> 00:01:15,030
Press ENTER and if you want to perform
more actions with this conditional, with
21
00:01:15,030 --> 00:01:19,979
this condition so when this condition is
true this will be executed but if you
23
00:01:19,979 --> 00:01:25,049
want to perform more actions you can add
one there, if not then you can go to the
25
00:01:25,049 --> 00:01:32,159
next check.
So that would be else, so you have a
27
00:01:32,159 --> 00:01:45,060
column here, a column there, so else print
equal or greater than 5. Now let me
29
00:01:45,060 --> 00:01:53,520
execute it. And we get equal or greater
than 5. So why did you get that? Well
31
00:01:53,520 --> 00:01:59,729
because you know if a is less than 5
which is not, so this evaluates to false,
33
00:01:59,729 --> 00:02:05,340
that means with this this will not be
executed so Python will skip this line
35
00:02:05,340 --> 00:02:12,870
and it will go to the next one. This next
line says else, so else means if this
37
00:02:12,870 --> 00:02:18,450
is not true,
this will be executed, so equal or
39
00:02:18,450 --> 00:02:25,769
greater than five, so in this case if a
is not less than five then there's no
41
00:02:25,769 --> 00:02:32,549
other option but a is either equal or
greater than five, so that's an if and else
43
00:02:32,549 --> 00:02:36,959
conditional block. However if you want to
add more conditions there
45
00:02:36,959 --> 00:02:45,569
you want to use elif, so let's do that
the first two lines stay the same. If a
47
00:02:45,569 --> 00:02:57,840
is less than five print less than five
elif a is equal to five print equal to
49
00:02:57,840 --> 00:03:19,019
five else print greater than five. Save
and execute and you get equal to five.
51
00:03:19,019 --> 00:03:21,769
If a was six,
you'd get greater than five and if a was
53
00:03:25,500 --> 00:03:34,500
three you'd get less than five.
That's a conditional block in Python and
55
00:03:34,500 --> 00:03:39,480
yeah, try to get familiar with the syntax
and in the next lecture I'm going to
57
00:03:39,480 --> 00:03:47,569
incorporate a conditional with this
block of code, so see you there.
16
views
Functions and User Input |Section 3|Celestial Warrior
The next lecture will be an exercise and there will be more exercises as you progress through the course.
The exercises are difficult and sometimes tricky and I don't expect students to solve them all. Most students actually fail to solve the exercisesunless they are already familiar with Python.
The whole point of exercises is to force you to think about how to solve a problem independently. The next lecture following each exercise contains the solution where you can compare your approach.
Create a functionthat converts Celsius degrees toFahrenheit. The formula to convert Celsius to Fahrenheit isF= C 9/5 + 32.
In the previous exercises you used the interactive Udemy interface to write your code.For this exercise, please use your computertools (Python, editor, terminal,etc.). This will help you get used with Python in a real environment.
Once you are done check your solution against my solution which islocatedin the next lecture. Don't worry if your solution is not exactly the same as mine. As far as the solutions generate the same output, your solution is most likely correct.
def cel_to_fahr(c):
f = c * 9/5 + 32
return f
And here is an example of calling the function:
print(cel_to_fahr(10))
That would return 50.0 which means 10 degreeCelsius is equal to 50 degreeFahrenheit.
Create a function that takes any string as argument and returns the length of that string.
def string_length(mystring):
return len(mystring)
And here's an example of calling the function:
print(string_length("Hello"))
That would return 5.
2
views
Functions - Advanced Features |Section 3|Celestial Warrior
Tip:
Note that not all functions have one input parameter. There are also simplerfunctions without any parameters at all. Example:
def say_hello():
return "Hello"
And there are also functions with more than one parameter but you will learn that in the next video.
3
views
Functions - The basics |Section 3|Celestial Warrior
1
00:00:00,500 --> 00:00:06,120
Hi again, in this video you'll learn how
to create a function in Python and how
3
00:00:06,120 --> 00:00:13,380
to use that function, so let me go ahead
and create a script. Let me name this my
5
00:00:13,380 --> 00:00:23,730
function.py and let's go ahead and
create a function. Creating a function
7
00:00:23,730 --> 00:00:30,210
will always begin with the DEF keyword
which is, which means define. Now that is
9
00:00:30,210 --> 00:00:33,980
followed by the name of the function, so
you need to give your function a name
11
00:00:33,980 --> 00:00:43,350
let me name this minutes to hours and
as you may guess already what I'm
13
00:00:43,350 --> 00:00:50,910
creating is a function that will convert
minutes to hours, so you create the
15
00:00:50,910 --> 00:00:56,940
procedure on how to convert minutes to
hours and then you can call that
17
00:00:56,940 --> 00:01:02,129
procedure, so you can call that function.
You can give that function any values
19
00:01:02,129 --> 00:01:07,380
when you call it and the function will
produce an output depending on the value
21
00:01:07,380 --> 00:01:12,659
and depending on the formula, on the
procedures that you write in here, so
23
00:01:12,659 --> 00:01:16,860
let's go ahead and do that, minutes
to hours. Now functions normally they have
25
00:01:16,860 --> 00:01:23,549
some input and they produce some output.
The input goes inside these brackets
27
00:01:23,549 --> 00:01:29,130
here. In our case the input could be
minutes so this is just a variable, you
29
00:01:29,130 --> 00:01:32,430
can give it any name that you want, you
can give it any name that you want for
31
00:01:32,430 --> 00:01:38,939
the function name as well, so the input
is minutes and then the output will be
33
00:01:38,939 --> 00:01:43,610
hours, but you don't write the output here.
Here what to do you always choose a
35
00:01:43,610 --> 00:01:52,340
column, press enter and that will be auto
indented, so as you see the functions
37
00:01:52,340 --> 00:01:56,969
everything that you write below the
first line of a function definition is
39
00:01:56,969 --> 00:02:02,850
indented and every line after that, so the
DEF keyword, the name of function, the
41
00:02:02,850 --> 00:02:09,720
input and here we write the body of the
function. What we want to do is let's
43
00:02:09,720 --> 00:02:18,850
say variable house equals to
minutes / 60 that should give us the
45
00:02:18,850 --> 00:02:26,290
hours. Note that if you are on Python 2
you want to do 60 point zero because
47
00:02:26,290 --> 00:02:31,540
if you do that you get an integer as
an output, not a float, so in
49
00:02:31,540 --> 00:02:37,030
Python 2 you have to explicitly pass
a float when you do division to get a
51
00:02:37,030 --> 00:02:42,190
number, to get a float with decimal
point. I am in Python 3, so I don't
53
00:02:42,190 --> 00:02:48,340
need to put 60.0 there. And yeah, this is a
simple function, so this is what the
55
00:02:48,340 --> 00:02:52,450
function will perform and then the last
line of the function is normally a
57
00:02:52,450 --> 00:02:59,790
return keyword, so notice that these are
reserved keywords def and return, and
59
00:02:59,790 --> 00:03:06,430
what I want to return is, well gets what?
The output which is hours, so that's the
61
00:03:06,430 --> 00:03:11,769
idea, we are getting minutes. The user
will input a value for that later when
63
00:03:11,769 --> 00:03:17,230
you call a function and Then minutes
will be divided by 60 and that operation,
65
00:03:17,230 --> 00:03:22,810
that number will be stored in the hours
variable which is a variable that exists
67
00:03:22,810 --> 00:03:27,700
only inside the function so this is
called a local variable. If you call it
69
00:03:27,700 --> 00:03:32,470
outside the function it will not work,
but we'll cover that later. And so then
71
00:03:32,470 --> 00:03:41,130
we return the value of that variable.
Now I'll save this and open the terminal.
73
00:03:45,359 --> 00:03:58,329
Make some space there and let me execute
the script. You get nothing and the
75
00:03:58,329 --> 00:04:04,390
reason for that is because what you did
here is you just define a blueprint, you
77
00:04:04,390 --> 00:04:10,450
are not using the blueprint, so what you
do next is you need to go ahead and use
79
00:04:10,450 --> 00:04:17,530
that blueprint, so which means you
need to call that function, so backspace
81
00:04:17,530 --> 00:04:24,550
to go here to unindent. Indenting
means that you are done with the
83
00:04:24,550 --> 00:04:27,700
function definition so when you start a
new line,
85
00:04:27,700 --> 00:04:32,680
right here that means you are not
writing inside function and so to call a
87
00:04:32,680 --> 00:04:37,840
function what you do is you just call
the name of the function, minutes to hours
89
00:04:37,840 --> 00:04:44,920
and then you open brackets, then inside
brackets you pass a value that you want
91
00:04:44,920 --> 00:04:51,400
to use as input, so let's say you want
70 minutes, you want to convert them to
93
00:04:51,400 --> 00:04:58,510
hours, save the script go and execute
again you don't get any output, but let's see.
95
00:04:58,510 --> 00:05:02,770
As I said you won't get any output,
so why is that?
97
00:05:02,770 --> 00:05:07,630
Well that's because you didn't perform a
print operation there, you didn't call a
99
00:05:07,630 --> 00:05:16,780
print function so what you do is you
print so open brackets close brackets
101
00:05:16,780 --> 00:05:22,630
and inside brackets give the function
code, save your script, go here, execute
103
00:05:22,630 --> 00:05:28,900
and yeah you get the hours, so the
minutes is equal to one point one six
105
00:05:28,900 --> 00:05:37,060
six six hours so it's important to know
that this value here, this is just a
107
00:05:37,060 --> 00:05:44,550
number, so you can see that by doing type.
Execute. Sorry, I didn't print it out.
109
00:05:53,130 --> 00:05:59,530
And so this is a float number, so just a
number. The function gets an input and it
111
00:05:59,530 --> 00:06:04,060
generates an output which is defined by
the hours variable so whatever is
113
00:06:04,060 --> 00:06:10,170
storing the hours variable, that is the
value that the function will output and
115
00:06:10,170 --> 00:06:16,510
yeah, you can do things like you know you
can use this output to do other
117
00:06:16,510 --> 00:06:20,860
operations, let's say you want to add ten
hours there and you want to print that
119
00:06:20,860 --> 00:06:28,210
output, so expect this to be 11.1666.
And so on, and of course
121
00:06:28,210 --> 00:06:34,470
now in your script you can add more
functions if you like, you know like that.
123
00:06:34,470 --> 00:06:40,050
Like seconds to hours. You pass seconds
there
125
00:06:40,050 --> 00:06:47,310
and here seconds.
Return hours and so on and of course this
127
00:06:47,310 --> 00:06:56,639
works separately, so you can say print
seconds to hours and past there actually
129
00:06:56,639 --> 00:07:05,449
we have to change this because this
would be divided by 3600, and let's say
131
00:07:05,449 --> 00:07:14,400
this number of seconds, execute and you get
2.0 hours and you also get the other
133
00:07:14,400 --> 00:07:21,259
output in here and this one here.
Now something important to know is that
135
00:07:21,259 --> 00:07:28,080
these are custom functions and this one
here is also function but this is a
137
00:07:28,080 --> 00:07:33,659
built in function, so constant functions
are functions created by you and built in
139
00:07:33,659 --> 00:07:39,000
functions are functions that are created
by someone else, so in this case print is
141
00:07:39,000 --> 00:07:44,370
created by Python authors and print also
has its own code, but it's more a
143
00:07:44,370 --> 00:07:49,680
low-level code and it's actually
read-only, so you won't be able to see
145
00:07:49,680 --> 00:07:53,669
the code for print. Yeah, this is an
introduction to functions. In the next
147
00:07:53,669 --> 00:07:58,080
lecture I'll show you some more advanced
features of functions, so let's see
149
00:07:58,080 --> 00:08:00,529
you there.
35
views
Section Introduction |Section 3|Celestial Warrior
1
00:00:00,030 --> 00:00:04,589
Hi and welcome to this new section! In
this section you're going to learn about
3
00:00:04,589 --> 00:00:10,250
two very important concepts in
programming. These are functions and
5
00:00:10,250 --> 00:00:17,390
conditionals. Functions, functions are
used to keep your code more organized so
7
00:00:17,390 --> 00:00:24,029
think of functions as small chunks of
code which together build a program that
9
00:00:24,029 --> 00:00:27,240
is what you need to know for now.
Functions are useful for other things as
11
00:00:27,240 --> 00:00:31,800
well but you'll understand it as you
build the real world programs. In the next
13
00:00:31,800 --> 00:00:36,960
video I'll write a function but that is
just a standalone function, so it
15
00:00:36,960 --> 00:00:41,579
doesn't interact with other parts of the
code, it's just a function, but when you
17
00:00:41,579 --> 00:00:45,390
see the large applications later on
you'll see how the function integrates
19
00:00:45,390 --> 00:00:51,149
with other parts of the code, so that's a
good thing of creating reload programs.
21
00:00:51,149 --> 00:00:56,579
That is how you'll learn how to do
programming in real life. That's about
23
00:00:56,579 --> 00:01:00,600
functions and then in this section you'll
also learn conditionals which basically
25
00:01:00,600 --> 00:01:06,869
are used to have your program make
decisions, so if that number is greater
27
00:01:06,869 --> 00:01:12,150
than that do this, if it's not greater
than do this other thing, that's
29
00:01:12,150 --> 00:01:17,340
basically the idea, but you'll see it in
action, so this is about the introduction
31
00:01:17,340 --> 00:01:21,680
of this section. I'll see you in the
other lecture.
2
views
Exception Handling in Python |Section 2|Celestial Warrior
1
00:00:01,129 --> 00:00:06,899
Hi again! Let's have one more lecture
about errors in Python and this
3
00:00:06,899 --> 00:00:11,429
particular lecture will be about
handling errors. Handling errors can be
5
00:00:11,429 --> 00:00:16,590
an advanced concept to digest for now
because you're still going through the
7
00:00:16,590 --> 00:00:22,199
basics of Python and you haven't seen
any real application yet, but later
9
00:00:22,199 --> 00:00:27,990
will use error handling while we are
building real-world applications so
11
00:00:27,990 --> 00:00:32,399
you're going to understand error
handling better later on. For now let's
13
00:00:32,399 --> 00:00:35,670
just focus on the syntax and try to
understand as much as you can
15
00:00:35,670 --> 00:00:42,899
I'll go and do an example here which I
mentioned earlier. Let's say we have a
17
00:00:42,899 --> 00:00:54,090
function that does division between two
given numbers like that. Return a divided
19
00:00:54,090 --> 00:00:59,719
by b and then you call the function here.
Divide 1, 2. In this case the
21
00:01:05,640 --> 00:01:10,619
function will try to divide 1 by 2.
And let me print out the output so that
23
00:01:10,619 --> 00:01:20,159
we can see it on the terminal. Go to the
terminal and execute the script. So you
25
00:01:20,159 --> 00:01:28,460
get you get 0.5. Now that's good.
However if the user enters let's say
27
00:01:28,460 --> 00:01:35,790
zero there like that, execute and your
program will crash so it will show an
29
00:01:35,790 --> 00:01:41,939
error. Now this one here is an example of
a longer error message that I mentioned
31
00:01:41,939 --> 00:01:45,750
previously in the previous videos that
you might have more than one, more
33
00:01:45,750 --> 00:01:54,869
than one error in your traceback, so for
instance this says line 4 at this line
35
00:01:54,869 --> 00:02:01,259
here. So that line is where Python
captured the error at first
37
00:02:01,259 --> 00:02:06,360
so basically Python was trying to
execute this line but it couldn't do it,
39
00:02:06,360 --> 00:02:10,709
it didn't manage to do it because this
line was trying to get the information
41
00:02:10,709 --> 00:02:15,460
from this function
and this function was trying to do this
43
00:02:15,460 --> 00:02:22,280
mathematical operation using zero after
the division so that is not possible and
45
00:02:22,280 --> 00:02:27,950
Python is pointing that out here.
So that's the main error in line two
47
00:02:27,950 --> 00:02:32,510
in this expression so that's where you
should focus, however when you ask
49
00:02:32,510 --> 00:02:36,590
questions you should include the entire
error message in your question
51
00:02:36,590 --> 00:02:43,550
as I mentioned previously.
So back to handling errors now. What I'm
53
00:02:43,550 --> 00:02:49,850
trying to say here is that you know
you have other functions here and other
55
00:02:49,850 --> 00:02:57,320
lines of code that you want to execute,
that you want Python to execute, but if
57
00:02:57,320 --> 00:03:04,720
the user passed this zero number here
the other lines would not be executed
59
00:03:04,720 --> 00:03:09,680
so your program would crash and you
don't want that and the way to avoid
61
00:03:09,680 --> 00:03:17,720
that is by using try and accept so
basically you try to capture the
63
00:03:17,720 --> 00:03:24,170
exception, to handle the exception.
The way to do that is quite easy.
65
00:03:24,170 --> 00:03:33,860
You say try and return a/b, so this has to be
indented like that so try is indented
67
00:03:33,860 --> 00:03:40,160
because it's under the function
definition. And then return is indented
69
00:03:40,160 --> 00:03:49,190
because it's under the try expression.
Then you have except, execute so this
71
00:03:49,190 --> 00:03:54,470
has to be on the same indentation level
with try, they are the same block, so
73
00:03:54,470 --> 00:04:02,090
except if there is an exception. What you
want to do is print out: "You are
75
00:04:02,090 --> 00:04:07,540
dividing by zero", or
"is this meaningless". So let me try
77
00:04:16,299 --> 00:04:26,380
this now and you get: "Zero division is
meaningless" and none, oh sorry I meant
79
00:04:26,380 --> 00:04:34,420
to do here return, not print.
I explained you why you get none there so you
81
00:04:34,420 --> 00:04:42,940
got none as an output because what
the function did was its tried to do
83
00:04:42,940 --> 00:04:48,160
this expression but it got an error
so it ignores this line, it doesn't
85
00:04:48,160 --> 00:04:54,880
execute it and then it goes to the
next line. So this line is executed when
87
00:04:54,880 --> 00:05:01,540
this is not executed and so it printed
out: "Zero division is meaningless", but it
89
00:05:01,540 --> 00:05:06,010
also printed out none because when you
execute a function that doesn't have a
91
00:05:06,010 --> 00:05:10,450
return statement such as in this case we
had the print statement here, the print
93
00:05:10,450 --> 00:05:15,820
function not the return statement, in that
case if the function is not returning
95
00:05:15,820 --> 00:05:23,440
anything it can have a print function or
other function as well, but if it
97
00:05:23,440 --> 00:05:29,229
doesn't have a return statement
then it will print out none, so basically
99
00:05:29,229 --> 00:05:33,520
it executes the other procedures such as
the print statement, it prints out the
101
00:05:33,520 --> 00:05:37,690
statement but if it doesn't have
a return statement it doesn't, it prints
103
00:05:37,690 --> 00:05:43,030
out none. Anyway save this now, execute
again and in this case you get only this
105
00:05:43,030 --> 00:05:48,729
zero division is meaningless printed
out there. So basically if you had other
107
00:05:48,729 --> 00:05:57,280
lines of code here like that, those would
be executed as well and that happens by
109
00:05:57,280 --> 00:06:03,160
using the try and except block who which
make sure that the code flow continues.
111
00:06:03,160 --> 00:06:11,050
So while the important thing to note is
that it is advisable to actually here use
113
00:06:11,050 --> 00:06:18,120
the exact name of the error that you
want to accept, so
115
00:06:19,350 --> 00:06:27,340
Zero Division Error and as you see these
got turned into blue which means that
117
00:06:27,340 --> 00:06:33,370
this is actually a Python keyword, is
recognized by Python and in that case if
119
00:06:33,370 --> 00:06:41,560
you exact you this you get the same
output actually like that or if you pass
121
00:06:41,560 --> 00:06:46,650
a number there that it is possible to
get a division from, you'll get that.
123
00:06:46,650 --> 00:06:53,020
Sorry about the backslash. I typed that
by mistake on my terminal. Execute
125
00:06:53,020 --> 00:06:56,650
again so you get this. And so what I'm
trying to say about the zero division
127
00:06:56,650 --> 00:07:01,090
error is that you know sometimes you
might get other types of errors in your
129
00:07:01,090 --> 00:07:07,930
code, other types of exceptions.
Let's say you have a name error there
131
00:07:07,930 --> 00:07:12,370
and you are saying that except all
errors when you don't pass this you
133
00:07:12,370 --> 00:07:17,919
you're basically including all types of
errors there and that is a bit dangerous for your
135
00:07:17,919 --> 00:07:22,960
code because you know you might have
problems that you don't notice and
137
00:07:22,960 --> 00:07:27,789
basically you are ignoring that, but you
shouldn't. So you should be specific
139
00:07:27,789 --> 00:07:32,830
there, explicit like that. If there are
other types of errors such as a name error,
141
00:07:32,830 --> 00:07:38,349
that name error will crash your
program, so you know what's going on.
143
00:07:38,349 --> 00:07:42,760
You need to be specific.
If you want to handle that error too, then
145
00:07:42,760 --> 00:07:47,380
you can add another exception here.
Except name error for instance. Yeah,
147
00:07:47,380 --> 00:07:52,389
that's about handling errors in Python.
I hope this makes sense! I hope I explained
149
00:07:52,389 --> 00:07:58,330
this well! And yeah, this is about errors.
I'll talk to you in the next lectures.
151
00:07:58,330 --> 00:08:00,000
See you!
45
views
Fixing Difficult Errors |Section 2|Celestial Warrior
1
00:00:00,260 --> 00:00:06,089
Great, welcome again to this new video.
This is the third video about errors. We
3
00:00:06,089 --> 00:00:10,500
talked about syntax errors, we talked
about exceptions, and now we're going to
5
00:00:10,500 --> 00:00:15,179
talk about how to fix an error when you
don't understand the message, so let's
7
00:00:15,179 --> 00:00:23,189
focus on this error, a zero division error.
In case you were good in mathematics you
9
00:00:23,189 --> 00:00:26,789
could easily fix this error so you
you'll be able to understand that you
11
00:00:26,789 --> 00:00:32,820
cannot divide c by, you cannot divide
a number by 0 so you'd fix this by
13
00:00:32,820 --> 00:00:39,090
removing this expression here. However if
you were not that good in mathematics you
15
00:00:39,090 --> 00:00:43,590
may find this difficult to understand.
In that case the fastest way to fix this
17
00:00:43,590 --> 00:00:51,360
error is by copying this line here.
Control C, so select it copy it with ctrl
19
00:00:51,360 --> 00:01:02,510
C or right click and copy there and then
go to Google. Just paste it. Now on Google
21
00:01:02,510 --> 00:01:08,909
this got around a hundred thousand
results so Python is very popular and
23
00:01:08,909 --> 00:01:16,040
you'll get a solution for almost every
problem that you'll get with Python
25
00:01:16,040 --> 00:01:21,810
so before looking at documentation or
asking a question somewhere it's very
27
00:01:21,810 --> 00:01:26,070
likely that someone else has had your
issue before and i have asked a question
29
00:01:26,070 --> 00:01:29,640
about that and there is a solution about
that as well.
31
00:01:29,640 --> 00:01:37,280
And a very good question and answer
website is stackoverflow.com and
33
00:01:37,280 --> 00:01:41,640
almost always when you search for an
error on Google the first results will
35
00:01:41,640 --> 00:01:51,299
be from stackoverflow and so someone
has asked a question with this title. It says
37
00:01:51,299 --> 00:01:55,680
I faced an error when I run the program
using Python. So it's the same things as
39
00:01:55,680 --> 00:02:02,820
error as you like that. How to avoid that
error? And if it also talks about the
41
00:02:02,820 --> 00:02:06,110
desired output.
And there are two answers for this
43
00:02:09,899 --> 00:02:16,170
question. tThe answer with this green mark
here is the accepted answering and it
45
00:02:16,170 --> 00:02:22,800
has and it has the most up votes so 11
and this has 2 down votes some minus 2.
47
00:02:22,800 --> 00:02:27,209
Actually the difference between up votes
and down votes is in the favor of
49
00:02:27,209 --> 00:02:33,420
down votes and so these are two different
answers that you can look at. Usually you
51
00:02:33,420 --> 00:02:38,519
want to look at the accepted answer that
has the solution, however in this case
53
00:02:38,519 --> 00:02:45,209
this also has the explanation it says
you cannot divide by 0. This causes
55
00:02:45,209 --> 00:02:50,549
Python to raise an error. That's single
good answer and I think it didn't
57
00:02:50,549 --> 00:02:57,209
deserve to get to have this
balance of votes, but this one gives a
59
00:02:57,209 --> 00:03:02,910
solution on how to handle an error and
that takes us to another concept which
61
00:03:02,910 --> 00:03:08,489
is handling errors in Python. So let's
say you have a program you have a
63
00:03:08,489 --> 00:03:15,000
function that gets two input values and
then it divides those two input values.
65
00:03:15,000 --> 00:03:21,959
However sometimes the user may pass zero as
as a second input and in that
67
00:03:21,959 --> 00:03:25,170
case if you divide, let's say the first
value would be 2 and the second value
69
00:03:25,170 --> 00:03:34,019
would be 0, your program would crash and
if you want you can use exception
71
00:03:34,019 --> 00:03:38,639
handling to actually avoid that program
from crashing. And you can do that with a
73
00:03:38,639 --> 00:03:44,099
try and accept statements. Anyway we'll
cover that in the next lectures in this
75
00:03:44,099 --> 00:03:51,450
section. So this was about searching
for an error on Google and you can
77
00:03:51,450 --> 00:03:56,069
easily find the solution to that as you
see. These are very clear questions. It's
79
00:03:56,069 --> 00:04:01,319
a big community and everyone can vote up
and down and can comment and so
81
00:04:01,319 --> 00:04:07,380
basically the answers are very accurate.
However you'll not always find an answer on
83
00:04:07,380 --> 00:04:11,060
Google.
In that case it's time to ask your own
85
00:04:11,060 --> 00:04:17,389
question, so the first thing is again you
check if you understand the error and
87
00:04:17,389 --> 00:04:22,220
fix it if you understand it. The second
thing is you do a search on Google and
89
00:04:22,220 --> 00:04:27,229
if the first to fail to solve your
problem, then it's time to ask a question.
91
00:04:27,229 --> 00:04:34,820
And you can ask a question either inside
the course, so you can, while watching a
93
00:04:34,820 --> 00:04:40,669
video you can browse Q&A which stands
for question and answer and you can do a
95
00:04:40,669 --> 00:04:44,840
quick search if you find an existing
answer to your question here and if you
97
00:04:44,840 --> 00:04:48,349
cannot find it then you can ask a
question, or you can ask a question on
99
00:04:48,349 --> 00:04:55,669
stackoverflow. You can use this button
here, ask a question and I'll be happy to
101
00:04:55,669 --> 00:05:00,620
help you here on the course interface
Q&A section.
103
00:05:00,620 --> 00:05:06,020
Or other people can also help you own
stackoverflow, however something is very
105
00:05:06,020 --> 00:05:12,289
important to know. The structure of your
question is very important on getting a
107
00:05:12,289 --> 00:05:18,169
good answer. You know sometimes
there are very bad questions like let's
109
00:05:18,169 --> 00:05:23,750
say my code is not working how do I fix
this and that's all. With that very
111
00:05:23,750 --> 00:05:28,759
little information is very hard to get a
good answer so it's very important to
113
00:05:28,759 --> 00:05:33,919
ask a good question and I'll show you
how to ask a good programming question
115
00:05:33,919 --> 00:05:38,000
in the next lecture. So that's a very
important lecture as well. See you there!
42
views
The Structure of a Good Programming Question
1
00:00:00,030 --> 00:00:04,020
Hey, hi again and welcome to yet another
lecture. In this lecture I'll show you
3
00:00:04,020 --> 00:00:09,090
how to ask a good programming question.
So I mentioned in the previous lecture
5
00:00:09,090 --> 00:00:14,089
asking a good programming question is
very important to get a good answer and
7
00:00:14,089 --> 00:00:21,109
let me first show you what a bad
programming question. For instance
9
00:00:21,109 --> 00:00:28,410
I'm going to click on ask question and
here is the title of a question. A bad
11
00:00:28,410 --> 00:00:33,420
programming question would have a very
generic title like that. Here is the body
13
00:00:33,420 --> 00:00:36,960
of the question where you
have to describe your question in detail.
15
00:00:36,960 --> 00:00:48,239
Some people do they just say: I got an
error when trying to two numbers and they
17
00:00:48,239 --> 00:00:55,350
just post the question. A better question
would be to include the error, the code
19
00:00:55,350 --> 00:01:00,050
there that you executed like that.
However that is also not enough because
21
00:01:04,140 --> 00:01:09,810
you also need to show the error message.
So let me delete this, and a good program
23
00:01:09,810 --> 00:01:14,509
in question would be We're talking
about questions that have an error,
25
00:01:14,509 --> 00:01:20,820
questions that talk about an error that
you got in your code. So this is your
27
00:01:20,820 --> 00:01:27,780
code. A good title would be to include
the error message, so that one would be a
29
00:01:27,780 --> 00:01:32,070
good fit. If it's a very long message
then you can also you can choose to
31
00:01:32,070 --> 00:01:37,829
include only the name of the error without
that and then a good question woulb
33
00:01:37,829 --> 00:01:50,460
be: Hi, I run the following code and
expected to get division in the last print
35
00:01:50,460 --> 00:01:55,520
statement, but I got an error.
Here is my code, so you put the code
37
00:02:01,030 --> 00:02:08,590
there, your entire script. Don't worry
if your question is long. It's better to
39
00:02:08,590 --> 00:02:19,950
be long than to be incomplete and here
is the error. Here make sure to include
41
00:02:19,950 --> 00:02:28,510
your entire errors trace back including
the output as well. So from this
43
00:02:28,510 --> 00:02:34,209
line of the command line up to the next
line so this entire thing goes to your
45
00:02:34,209 --> 00:02:43,750
question. Just copy it. Don't worry if it's
long. Can you help? So that'd be a good
47
00:02:43,750 --> 00:02:48,099
programming question. Now this is still
missing something very important and
49
00:02:48,099 --> 00:02:52,989
that is the highlighting of the code. You
need to select the code and click on
51
00:02:52,989 --> 00:03:07,870
this icon here. The error as well. And let
me pose the question. External links must
53
00:03:07,870 --> 00:03:14,560
provide free educational content, just
click on proceed and here is the question.
55
00:03:14,560 --> 00:03:18,430
Here is how it looks like
on the other end. So this question has
57
00:03:18,430 --> 00:03:21,940
four good elements that make it a good
question.
59
00:03:21,940 --> 00:03:29,590
The first element is that the poster is
mentioning what he or she expected to
61
00:03:29,590 --> 00:03:36,250
get as output from the script that here
they were running so it says I was
63
00:03:36,250 --> 00:03:39,760
expecting to get the division in the
last print statement which is this one
65
00:03:39,760 --> 00:03:44,620
here, but I got an error so the first
thing is you need to include the
67
00:03:44,620 --> 00:03:48,250
expected output, you need to explain
what you were expecting because
69
00:03:48,250 --> 00:03:53,440
otherwise if you don't explain what you
were expecting the other person who
71
00:03:53,440 --> 00:04:01,780
wants to help you doesn't know the goal
of your code or of your questions so it
73
00:04:01,780 --> 00:04:05,890
doesn't know how to help you to reach to
that goal. So expected output is the first
75
00:04:05,890 --> 00:04:08,830
thing. The second thing is you need to
include your entire code
77
00:04:08,830 --> 00:04:15,970
and make sure that you include your code
as text. It's best to use text,
79
00:04:15,970 --> 00:04:22,360
rather than screenshots because text, the
person who wants to help you can copy
81
00:04:22,360 --> 00:04:29,500
your code and run it all their Python
interpreter, but if you includes
83
00:04:29,500 --> 00:04:35,940
screenshots they are images and it's hard
for other people to try out your code
85
00:04:35,940 --> 00:04:40,060
If you ask this on stackoverflow your
87
00:04:40,060 --> 00:04:44,530
question would get closed because if you
ask it or we're using a screenshot
89
00:04:44,530 --> 00:04:49,660
rather than the text version of your
code, the question will get closed
91
00:04:49,660 --> 00:04:55,620
because there are very strong rules in
stackoverflow to have a high-quality
93
00:04:55,620 --> 00:05:01,690
question and answer site. So that's that
second element. The third element is you
95
00:05:01,690 --> 00:05:09,730
need to include your entire screenshot,
entire error traceback, sorry, so from the
97
00:05:09,730 --> 00:05:14,770
beginning up to the end. So that was the
third element, and the last element, the
99
00:05:14,770 --> 00:05:19,870
fourth element is you need to highlight
your code just like that. You see this is
101
00:05:19,870 --> 00:05:27,400
much more visible and easier for others
to troubleshoot. So the course interface
103
00:05:27,400 --> 00:05:32,440
has that function there that you can
highlight your code. On stackoverflow
105
00:05:32,440 --> 00:05:37,169
you also have that capability so just go
to ask question, you write your code here,
107
00:05:37,169 --> 00:05:46,780
highlight it and then use these curly
brackets. Yeah, that's it. If you ask a
109
00:05:46,780 --> 00:05:51,190
good questions the odds are that you're
going to get a good answer as well. So I
111
00:05:51,190 --> 00:05:55,810
hope this makes sense and I hope I showed
you something that you didn't know and
113
00:05:55,810 --> 00:06:00,000
I'll talk to you later. See you!
45
views
Runtime Errors |Section 2|Celestial Warrior
1
00:00:01,429 --> 00:00:06,240
Hi again and welcome to this new lecture!
In the previous lecture we talked about
3
00:00:06,240 --> 00:00:13,320
errors in general and I focused on
syntax errors which is one of the two
5
00:00:13,320 --> 00:00:17,250
types of errors that you have in Python.
So you have syntax errors and you have
7
00:00:17,250 --> 00:00:23,640
exceptions. Now we're going to talk about
exceptions in Python, so every other type
9
00:00:23,640 --> 00:00:29,699
of error that is not a syntax error is
an exception. And often all errors,
11
00:00:29,699 --> 00:00:35,160
syntax errors and exceptions they are
referred to as errors so you're going to
13
00:00:35,160 --> 00:00:41,219
hear this all over the place. Now I have
a new script here that I created for
15
00:00:41,219 --> 00:00:46,050
this lecture.
Try to guess what I'm going to get as
17
00:00:46,050 --> 00:00:53,340
output when I execute this script, so I
have a equal to 1, n equals to characters 2.
19
00:00:53,340 --> 00:01:04,430
Print int 2.5 and print a plus b. So
guess what I'm going to get for a moment.
21
00:01:04,430 --> 00:01:17,369
Okay, now let me run the script Python 3.
Errors.py. File errors.py line 4
23
00:01:17,369 --> 00:01:28,020
which is the last line, this one. We get
a syntax error, invalid syntax. Now this
25
00:01:28,020 --> 00:01:31,979
can be very confusing for you now
because you know you're looking at the
27
00:01:31,979 --> 00:01:40,680
print a plus b expression, but you don't
see any error there. So for a beginner of
29
00:01:40,680 --> 00:01:49,020
this can be quite frustrating but try to
look back on the left of arrow here, so
31
00:01:49,020 --> 00:01:54,509
think of the arrow that the arrow is
pointing at the token and token has not
33
00:01:54,509 --> 00:01:59,670
been written correctly in the script, so
in this case you know the problem
35
00:01:59,670 --> 00:02:07,600
here is that this line here it has an
open bracket, round bracket
37
00:02:07,600 --> 00:02:13,920
and than it has an int function, and then the int
function has its own brackets that is
39
00:02:13,920 --> 00:02:19,360
wrapping inside its input, so these are
the brackets of the int function, but the
41
00:02:19,360 --> 00:02:24,640
print function doesn't have a closing
bracket. What you did instead, what I did
43
00:02:24,640 --> 00:02:30,760
actually, what I did is that instead of
putting a bracket there I actually wrote
45
00:02:30,760 --> 00:02:38,050
another print function so Python was
expecting a closing round
47
00:02:38,050 --> 00:02:43,450
bracket, but I wrote I typed in a print
function. That's why this is saying that
49
00:02:43,450 --> 00:02:49,420
this print function is not in the
correct position. That's the idea.
51
00:02:49,420 --> 00:02:55,030
So always when you see this arrow first
look at this line event look before that
53
00:02:55,030 --> 00:03:03,940
also. Sorry about this! So always when you
see this arrow here, first look at this
55
00:03:03,940 --> 00:03:09,610
line here but also don't forget that the
problem might also be before this
57
00:03:09,610 --> 00:03:14,340
line.
Okay, so that was about the syntax error.
59
00:03:14,340 --> 00:03:19,299
I'll explain why I talked about this
syntax error here let me execute this
61
00:03:19,299 --> 00:03:24,850
again. So the syntax error was fixed now,
but we still have another error. So the
63
00:03:24,850 --> 00:03:29,650
reason I included a syntax error in my
code was to show you that Python
65
00:03:29,650 --> 00:03:36,190
actually first checks for syntax errors
so basically it parses the code, it looks
67
00:03:36,190 --> 00:03:43,060
for syntax errors, it doesn't execute the
code yet. So when I executed Python 3
69
00:03:43,060 --> 00:03:47,170
errors that's why i in here the code was
not executed but the interpreter just
71
00:03:47,170 --> 00:03:53,950
checks for syntax errors, it doesn't
check for exceptions just yet. So first
73
00:03:53,950 --> 00:03:56,890
you need to fix the syntax errors and
that's what we did here. We added
75
00:03:56,890 --> 00:04:02,610
these brackets and we fixed the syntax errors.
Now Python is throwing out an exception and
77
00:04:02,610 --> 00:04:08,620
let's, here is where I executed the code
and see the next line we have number two
79
00:04:08,620 --> 00:04:14,380
there which is coming from the output of
this line here, so from line three we
81
00:04:14,380 --> 00:04:19,239
got the output correctly. So basically
again Python executes the script from top
83
00:04:19,239 --> 00:04:22,810
to bottom
if it doesn't find
85
00:04:22,810 --> 00:04:28,930
syntax errors. If it finds syntax errors
it doesn't execute anything. So 2 is
87
00:04:28,930 --> 00:04:36,360
printed out and then you get this trace
back of the error which starts there and
89
00:04:36,360 --> 00:04:44,680
ends in here that's a block of the error.
Sometimes the errors may have more than
91
00:04:44,680 --> 00:04:50,950
one type of error. So in this case
we have only one type of error which is
93
00:04:50,950 --> 00:04:56,830
a type error, but you may have multiple
blocks here. However the most important
95
00:04:56,830 --> 00:05:02,710
error that you must focus on is the lost
line of the error, so the last block of
97
00:05:02,710 --> 00:05:08,520
of the error. In this case we have only
one block so we're focused on that.
99
00:05:08,520 --> 00:05:16,570
And line 4 which is this one in here. This
is the line. Again the line is printing out
101
00:05:16,570 --> 00:05:21,520
just like in the case of syntax error.
And you have a type of the error here
103
00:05:21,520 --> 00:05:26,920
which is a type error. So what's a type
error? A type error means that there is
105
00:05:26,920 --> 00:05:35,530
something wrong with one of your object
types in your script, and you have the
107
00:05:35,530 --> 00:05:42,220
description here. Unsupported operand
type for plus so this is trying to say
109
00:05:42,220 --> 00:05:48,130
that the plus operator has an
unsupported type so the plus operator in
111
00:05:48,130 --> 00:05:52,690
other words doesn't support one of the
types that you have given to it, so it's
113
00:05:52,690 --> 00:05:59,730
either the type of variable a or the
type of variable b. So it says int and
115
00:05:59,730 --> 00:06:06,730
string, so it doesn't specifically say
whether int or string is an object but
117
00:06:06,730 --> 00:06:12,250
it says that you cannot use a plus
operator with an integer and a string.
119
00:06:12,250 --> 00:06:18,670
And that's logically wrong
because you cannot add a number to some
121
00:06:18,670 --> 00:06:24,150
text. That's what Python does not
understand so it throws a type error.
123
00:06:24,150 --> 00:06:30,500
So exceptions are like logical errors
and you need to use your logic now to
125
00:06:30,500 --> 00:06:35,720
fix the error. And you use your logic by
carefully inspecting the error and
127
00:06:35,720 --> 00:06:40,790
that's what we did.
So what we want to do, we want to fix
129
00:06:40,790 --> 00:06:47,390
this. Now it's up to me whether I
meant to concatenate these two objects
131
00:06:47,390 --> 00:06:54,800
or do a mathematical addition operation.
So let's say if I was intending to
133
00:06:54,800 --> 00:07:01,190
do an addition between these two numbers
then I would have to convert b to a float
135
00:07:01,190 --> 00:07:09,710
or an integer.
Save that, execute, and yeah in this case you
137
00:07:09,710 --> 00:07:14,840
don't get any error. You get 2 printed
out from the second, from the third line
139
00:07:14,840 --> 00:07:22,310
and 3.0 from the last line that we have
just fixed. However if my intention was
141
00:07:22,310 --> 00:07:29,690
to actually print out the concatenation
between these two strings, instead of
143
00:07:29,690 --> 00:07:36,310
converting b to a float, I would have to
convert a to a string.
145
00:07:36,310 --> 00:07:42,980
In this case I would get the text
12. 12, so that would be
147
00:07:42,980 --> 00:07:50,060
a string object, not a number. Even though
here it shows as a number it's just how
149
00:07:50,060 --> 00:07:57,169
the terminal prints it out. So again these
are errors that occur in runtime, so when
151
00:07:57,169 --> 00:08:03,110
the script executes. Syntax errors are
parsing errors so the interpreter tries
153
00:08:03,110 --> 00:08:07,640
to understand whether the script is
syntactically correct, whether
155
00:08:07,640 --> 00:08:13,910
you have followed the Python
syntax rules. So you need a bracket,
157
00:08:13,910 --> 00:08:18,560
a closing bracket for an opening bracket.
You need a closing quote after the opening
159
00:08:18,560 --> 00:08:26,570
quote and so on. Now there are also
other types of exceptions, not only type
161
00:08:26,570 --> 00:08:34,030
error. You know you may have a name error.
Let's say here instead of printing out
163
00:08:34,030 --> 00:08:43,330
that, we print out c.
Save, execute and here is the trace back.
165
00:08:43,330 --> 00:08:50,970
Again 2 was printed out from the third line
and the trace back says line 4 at
167
00:08:50,970 --> 00:09:00,730
print c, name error name c is not
defined. So again this is not a syntax
169
00:09:00,730 --> 00:09:04,750
error because you haven't made any
mistakes with the syntax. You know you have
171
00:09:04,750 --> 00:09:10,120
this variable name which is correct,
you have brackets opening, round brackets
173
00:09:10,120 --> 00:09:14,890
and closing round bracket, so everything
is syntactically correct both this c
175
00:09:14,890 --> 00:09:20,170
object Python doesn't know this. You
haven't defined this c variable so
177
00:09:20,170 --> 00:09:23,920
Python doesn't know what to print out.
You know python is able to print out a
179
00:09:23,920 --> 00:09:30,940
because it knows that a refers to the
1 to integer 1 and so it prints out
181
00:09:30,940 --> 00:09:36,790
integer 1, but c doesn't have anything
so you get this name error and whenever
183
00:09:36,790 --> 00:09:42,520
you get a name error you know that this
name here has not been defined by you so
185
00:09:42,520 --> 00:09:48,490
to fix this you may want to define c
like that and execute and you don't get
187
00:09:48,490 --> 00:09:54,160
any error. You may also have other
types of errors such as c divided by 0.
189
00:09:54,160 --> 00:10:02,080
See what you get. Zero division
error division by 0. That's the
191
00:10:02,080 --> 00:10:07,300
description of this error so division by
0 is not mathematically possible or
193
00:10:07,300 --> 00:10:13,330
meaningful and since python is based on
mathematics it throws an error in
195
00:10:13,330 --> 00:10:17,200
and you need to fix that. You need to
remove that expression that divides by 0.
197
00:10:17,200 --> 00:10:25,720
Yeah, that's about errors in Python.
I hope you understood a great deal of this
199
00:10:25,720 --> 00:10:30,820
lecture and the previous lecture as well.
So it's relatively easy to fix errors,
201
00:10:30,820 --> 00:10:35,800
however sometimes
there are errors that you might have
203
00:10:35,800 --> 00:10:41,620
difficulties understanding and fixing
them so later for example we're going to
205
00:10:41,620 --> 00:10:45,220
work on libraries and sometimes
libraries they have different kinds of
207
00:10:45,220 --> 00:10:49,900
errors, however nothing to worry about
because there are other things that you
209
00:10:49,900 --> 00:10:53,050
can do
for an arrow that you don't understand.
211
00:10:53,050 --> 00:10:58,000
And I'll talk about that in the next
lecture, so I'll see you there!
66
views
Syntax Errors |Section 2|Celestial Warrior
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!
56
views
Dictionaries |Section 1|Celestial Warrior
1
00:00:00,709 --> 00:00:08,580
Hey, hello! Let's look at dictionaries in
this lecture. This far we've looked at strings
3
00:00:08,580 --> 00:00:15,150
we've looked at lists and tuples and now we
have yet another data type and these are
5
00:00:15,150 --> 00:00:23,430
dictionaries. Let me open the python shell.
And let me create a list first I'm just
7
00:00:23,430 --> 00:00:28,140
creating this for reference, so to
compare lists with dictionaries, but you
9
00:00:28,140 --> 00:00:32,940
don't need to create a list for creating
a dictionary. So to create a dictionary
11
00:00:32,940 --> 00:00:40,140
you need curly brackets, so open and
closing curly brackets and then inside
13
00:00:40,140 --> 00:00:45,719
that you'd pass the items of the dictionary.
Now the difference here with
15
00:00:45,719 --> 00:00:52,230
dictionaries is that instead of letting
Python add default indexes for your
17
00:00:52,230 --> 00:01:00,390
items you want to create those indexes
yourself exclusively so therefore for
19
00:01:00,390 --> 00:01:09,960
dictionaries you do something like this.
Name, a column and then the item. Let me
21
00:01:09,960 --> 00:01:20,159
write another one, surname column and
then the other item and yeah that's it.
23
00:01:20,159 --> 00:01:25,950
That's the dictionary. So what we have
here is when I was talking about
25
00:01:25,950 --> 00:01:31,229
indexing I was referring to these things.
Actually these two are referred to as
27
00:01:31,229 --> 00:01:38,189
keys, not indexes so for dictionaries you
can create custom indexes so to say and
29
00:01:38,189 --> 00:01:45,329
so these are keys so we have key and
value, key and value. This is good because
31
00:01:45,329 --> 00:01:51,329
now if you want to access one of the
values of the dictionary again you'd use
33
00:01:51,329 --> 00:01:56,939
square brackets just as you did with
strings lists and tuples and then there you
35
00:01:56,939 --> 00:02:02,909
pass the key which value you want to
extract, so if you want to extract John,
37
00:02:02,909 --> 00:02:09,300
you want to pass the key of John and
that will extract the John value. Something
39
00:02:09,300 --> 00:02:13,470
to note is that dictionaries are
an unordered colecton
41
00:02:13,470 --> 00:02:20,250
of items and so sometimes you may
notice that the order of the key and
43
00:02:20,250 --> 00:02:26,550
values is not maintained so you may see
that surname has gone to the first place, so
45
00:02:26,550 --> 00:02:32,160
surname Smith and the pair has gone the
first one and name John is the second
47
00:02:32,160 --> 00:02:37,110
one and so on. So this is different from
lists, strings and tuples. The reason why
49
00:02:37,110 --> 00:02:42,210
Python doesn't keep the order is that
you know the way you access dictionaries
51
00:02:42,210 --> 00:02:49,890
is you exclusively pass the key of the
dictionary when you access it and so you
53
00:02:49,890 --> 00:02:54,209
don't need to know the order of the dictionary.
And whenever you want to access
55
00:02:54,209 --> 00:02:57,510
something you say the surname for
instance and that will the return Smith
57
00:02:57,510 --> 00:03:02,340
no matter where Smith is, so whether it
is the first one, the second one or over
59
00:03:02,340 --> 00:03:10,500
and so on. And interestingly in
dictionaries you can also pass, you can
61
00:03:10,500 --> 00:03:17,940
have like lists as values or even tuples,
so let me write a circle there or let me
63
00:03:17,940 --> 00:03:27,000
keep it like that and let me add another
key and pair here. Let's say cities. Let's say
65
00:03:27,000 --> 00:03:42,750
Joe Smith lives in three cities, Porto
and San Diego and Bali. You close
67
00:03:42,750 --> 00:03:49,769
bracket there so as you see we have the
key here, we have a column and then you
69
00:03:49,769 --> 00:03:54,030
have the value which can be anything so
it can be string, it can be a number, it
71
00:03:54,030 --> 00:04:01,100
can be at list, a tuple. In this case
that's a tuple so if you access now
73
00:04:01,100 --> 00:04:10,070
cities like that, you'd get… Sorry, I'll add
two bracket there, so you'd get the tuple.
75
00:04:10,070 --> 00:04:15,959
And then you can also do tricks like you
know if you want to access Bali you'd
77
00:04:15,959 --> 00:04:23,789
have other brackets there and pass 2.
And that would return Bali, so why is that?
79
00:04:23,789 --> 00:04:27,710
That's because you know up to here you
have a tuple,
81
00:04:27,710 --> 00:04:35,849
but that's a triple object and
then from that tuple you use again the
83
00:04:35,849 --> 00:04:40,560
square brackets notation to access an
item from that tuple, so you want item
85
00:04:40,560 --> 00:04:47,759
with index two and that is Bali and yeah
that's about dictionaries. Try them out
87
00:04:47,759 --> 00:04:55,110
some more, play around and you will be able
to comfortably create and access items
89
00:04:55,110 --> 00:05:01,099
from a dictionary. So I hope you enjoyed
this and talk to you later.
14
views
Tuples |Section 1|Celestial Warrior
1
00:00:00,350 --> 00:00:05,279
Let's now look at tuples. In the
previous lecture we looked at lists and
3
00:00:05,279 --> 00:00:10,790
tuples are very similar to lists, the
only difference is that tuples are not
5
00:00:10,790 --> 00:00:17,520
mutable, so you cannot change a tuple.
Lists are used much more than tuples
7
00:00:17,520 --> 00:00:24,269
so tuples are only using very specific
scenarios, so we will see some scenarios
9
00:00:24,269 --> 00:00:28,109
throughout the course when we build the
real-world applications, so you don't
11
00:00:28,109 --> 00:00:32,430
have to worry about understanding when
to use a tuple in programming for now.
13
00:00:32,430 --> 00:00:41,640
Anyway I want to show you the syntax, so
you can create a tuple like that.
15
00:00:41,640 --> 00:00:48,800
So tuples just like lists, they contain
they may contain different data types.
17
00:00:48,800 --> 00:00:56,340
Let's say 3 4 and so on.
Oh sorry, I was writing in the
19
00:00:56,340 --> 00:01:04,700
terminal, let me open the Python shell so
again
21
00:01:04,700 --> 00:01:11,850
we have strings numbers and floats if you like.
Whatever type that you want you can pass
23
00:01:11,850 --> 00:01:17,150
in a tuple and in a list and of course
you can use indexing for tuples as well,
25
00:01:17,150 --> 00:01:25,799
so let's access the lost item and that
will give 4.6. The difference is that you
27
00:01:25,799 --> 00:01:32,520
know tuples they have some slight
different methods than lists, so if you
29
00:01:32,520 --> 00:01:39,600
dir list, you see that lists have
much more methods available than tuples,
31
00:01:39,600 --> 00:01:46,890
so for instance you can append items to
a list, you can clear the list and copy
33
00:01:46,890 --> 00:01:54,149
and count, count is available for tuples
because count doesn't change the data type,
35
00:01:54,149 --> 00:01:58,829
so it doesn't change the tuple, it just
counts how many items are there in the tuple.
37
00:01:58,829 --> 00:02:04,259
As I said, tuples are immutable, so you
cannot change them that's why these
39
00:02:04,259 --> 00:02:09,959
methods that change the data type like
inserting new values and popping out
41
00:02:09,959 --> 00:02:14,280
values, removing
values, reversing and sorting so these
43
00:02:14,280 --> 00:02:19,590
are not available and yeah this is more or
less about tuples. In case you need them
45
00:02:19,590 --> 00:02:26,840
later they are there for you. Hope you're
enjoying the course and I'll talk to you later.
3
views
Lists |Section 1| Celestial Warrior
1
00:00:01,550 --> 00:00:08,189
Great, now let's look at lists, so in the
previous lecture we were looking at
3
00:00:08,189 --> 00:00:14,250
strings and indexing and splitting
strings and I will be working with lists
5
00:00:14,250 --> 00:00:20,210
which are very similar to strings. They
are similar because they are both
7
00:00:20,210 --> 00:00:25,949
referred to as sequences, so there are
sequences of items. The difference is
9
00:00:25,949 --> 00:00:35,910
that, let me open Python, and so the
difference is that the string is made of
11
00:00:35,910 --> 00:00:41,730
other strings so it can contain only
strings of characters or strings.
13
00:00:41,730 --> 00:00:49,079
whatever you like to call it, however
lists can contain various other data
15
00:00:49,079 --> 00:01:00,809
types, let's say H a letter, a number, and
also a string. So this is a string the
17
00:01:00,809 --> 00:01:05,970
first one, this is a number, an integer.
This is also string with multiple
19
00:01:05,970 --> 00:01:14,460
characters and that is a list, so for
this you want to use square brackets to
21
00:01:14,460 --> 00:01:21,450
create a list. Execute and yeah you can
call it and so this is a list of three
23
00:01:21,450 --> 00:01:26,460
items and you can also store other
objects in a list but we'll cover other
25
00:01:26,460 --> 00:01:33,320
objects in later sections, you know we
have table of objects with lots of data.
27
00:01:33,320 --> 00:01:39,180
You may store files in a list you may
store widgets of a graphical user
29
00:01:39,180 --> 00:01:45,570
interface that you are building and so
on, so these are very useful and lists also
31
00:01:45,570 --> 00:01:51,180
have indexing and you can split lists,
you can extract items and so on. So the
33
00:01:51,180 --> 00:01:56,790
good news is that lists have the
same indexing system as strings and so
35
00:01:56,790 --> 00:01:59,790
you've learned that in the previous
lecture and you can go ahead and apply
37
00:01:59,790 --> 00:02:06,979
that system not, so let's extract number 2.
That would have an index of one.
39
00:02:06,979 --> 00:02:14,720
Execute that and yeah you get 2 there. If you
check the type you'll see that's an integer.
41
00:02:14,720 --> 00:02:20,760
So every item of a list has its own
data type, even also access more than
43
00:02:20,760 --> 00:02:28,970
white one item of a list. Let's say
you want the first two items. You do 0 1 2, so
45
00:02:28,970 --> 00:02:38,010
like that or 0 to 2 whatever you like.
That will produce another list so the
47
00:02:38,010 --> 00:02:49,459
type of this split is a list. The type of
the split of a string is a string, so S
49
00:02:49,459 --> 00:02:57,390
for the string here, C for the list.
Lists now have methods associated with
51
00:02:57,390 --> 00:03:02,130
them just like strings do and so you can
check what methods you have available
53
00:03:02,130 --> 00:03:08,220
there by just doing a dir list.
So this a the blueprint you can use
55
00:03:08,220 --> 00:03:13,200
lists, you can pass empty square brackets
in there, it's the same thing and then
57
00:03:13,200 --> 00:03:20,510
you can use help to look at each of
these methods, so these are the methods.
59
00:03:20,510 --> 00:03:31,260
Without the underscore and let's look at
the append method, so to apply methode
61
00:03:31,260 --> 00:03:39,690
you do c the dot notation append and here
on that 3. What that does, it adds
63
00:03:39,690 --> 00:03:44,549
another item to the end of the list,
So it appends an item to the
65
00:03:44,549 --> 00:03:49,850
list and you can explore the other
methods if you like so let's say help.
67
00:03:49,850 --> 00:03:59,160
Remove like that and you'll see how to
use that remove method, so you do
69
00:03:59,160 --> 00:04:03,750
c.remove and then you pass the value of
the item that you want to remove, so
71
00:04:03,750 --> 00:04:10,440
not the index but the value. Let's say we
want to remove 2 and you see that 2
73
00:04:10,440 --> 00:04:16,979
is now gone from the list so look
at the other methods of the list and try
75
00:04:16,979 --> 00:04:21,720
to experiment with this and play around
with them access items and split them
77
00:04:21,720 --> 00:04:27,470
and you'll be good to go then.
Thanks, I'll talk to you later.
11
views
String Indexing and Splitting |Section 1|Celestial warrior
1
00:00:00,030 --> 00:00:04,290
Hi again, in the previous lecture we
talked about strings and in this lecture
3
00:00:04,290 --> 00:00:10,500
again we are going deeper into learning
strings and specifically we're going to add
5
00:00:10,500 --> 00:00:16,260
indexing. Indexing is a very important
concept not only with strings but
7
00:00:16,260 --> 00:00:21,150
with all the data type that we'll be
looking at later such as lists,
9
00:00:21,150 --> 00:00:26,880
tuples and dictionaries, so please follow
this lecture. Yeah, let's talk about
11
00:00:26,880 --> 00:00:35,070
indexing. Let me open the Python
interactive session. Let me create a
13
00:00:35,070 --> 00:00:44,070
string, "Hi there!"
exclamation mark and close quote.
15
00:00:44,070 --> 00:00:48,989
Now when you create a string, every string
that you create Python under the hood
17
00:00:48,989 --> 00:00:57,930
what it does is it assigns a number to
each or the items of your string so it
19
00:00:57,930 --> 00:01:06,780
starts from 0 so H will be assigned an
index of 0 and then a 1 for I, 2 for the
21
00:01:06,780 --> 00:01:12,799
space, 3 for T, five, six
seven, eight.
23
00:01:12,799 --> 00:01:18,990
So the exclamation mark character will
will have an index of eight, so you would
25
00:01:18,990 --> 00:01:26,360
ask why is this useful? Well this allows
you to extract certain parts from a
27
00:01:26,360 --> 00:01:33,600
string and there is a certain notation to
do that you'd want to access the string.
29
00:01:33,600 --> 00:01:39,270
So in this case that's variable C
holding the string, then you want to use
31
00:01:39,270 --> 00:01:44,579
square brackets so an opening square
bracket and then a closing square
33
00:01:44,579 --> 00:01:49,350
bracket and inside those square brackets you
pass the index of the item that you want
35
00:01:49,350 --> 00:01:58,170
to extract. Let's say I want the T
character so that would be 0 1 2 3.
37
00:01:58,170 --> 00:02:05,490
I pass number 3 there as an index and that
will extract for me the letter T which is
39
00:02:05,490 --> 00:02:10,800
actually a string.
So you can check the type of the
41
00:02:10,800 --> 00:02:15,810
output and you'll see that that's a
string, so a string is made of strings.
43
00:02:15,810 --> 00:02:22,950
You can say so. If you wanted H you'd
past zero and if you wanted a lost
45
00:02:22,950 --> 00:02:28,260
character well you might have to count
so one two three four five six seven
47
00:02:28,260 --> 00:02:33,330
eight.
And so, and sorry including type there.
49
00:02:33,330 --> 00:02:38,880
Sorry about that!
so c8 would give you the exclamation
51
00:02:38,880 --> 00:02:45,720
mark, but there's also another indexing
system there that Python uses so in this
53
00:02:45,720 --> 00:02:50,519
case we had to count from the beginning
to the end of the string but wouldn't it
55
00:02:50,519 --> 00:02:56,370
be useful if you had another system that
lets you count from the end, so from
57
00:02:56,370 --> 00:03:00,780
right to left?
Well, such a system exists and it
59
00:03:00,780 --> 00:03:08,400
starts from minus 1, so the exclamation
mark would be minus 1 now that you can
61
00:03:08,400 --> 00:03:18,989
extract that with that. Minus 2 would be
E, R would be minus 3 and so on up to H, so we
63
00:03:18,989 --> 00:03:25,019
have two indexing systems there which
allow you to extract items depending on
65
00:03:25,019 --> 00:03:30,090
whether the items are in the beginning
or near the end. Now what if you want to
67
00:03:30,090 --> 00:03:36,959
extract more than one item from a string?
Let's say you want to extract Hi.
69
00:03:36,959 --> 00:03:47,090
Well there is a notation for that too, so
Hi is expanding from index 0 to index 1.
71
00:03:47,090 --> 00:03:57,380
So up to here. So let's try 0 to 1.
That will not work a very good, but let's see.
73
00:03:57,380 --> 00:04:05,130
So as I said this will return only H,
so it doesn't return item with index 1.
75
00:04:05,130 --> 00:04:11,250
That's because splitting in Python, so we
we're splitting a string here
77
00:04:11,250 --> 00:04:17,609
splitting in Python is upper bound
exclusive, which means that the upper
79
00:04:17,609 --> 00:04:24,730
bounds of the split here is not included
in the output and so if you want to
81
00:04:24,730 --> 00:04:33,070
include I you want to pass the index
after I which is two so this is the string
83
00:04:33,070 --> 00:04:40,420
just for reference, so we extracted item
with index 0 and item with index 1.
85
00:04:40,420 --> 00:04:46,770
And then the split starts right there at
index 2 so it doesn't include the space.
87
00:04:46,770 --> 00:04:54,430
Just hi. If you pass 3 there, that would
include the wide space as well and
89
00:04:54,430 --> 00:05:00,310
similarly you can pass like one.
That will include the I and the space, there
91
00:05:00,310 --> 00:05:06,970
are also shortcuts, so if you pass zero
like nothing to 3 but that is the same
93
00:05:06,970 --> 00:05:18,120
as doing 0 to 3 so it will include 0 1 &
2 and similarly if you want to extract
95
00:05:18,120 --> 00:05:24,760
there with the exclamation mark
well what you'd want to do is T would
97
00:05:24,760 --> 00:05:32,919
have an index of 0 1 2 3 so 3 and then
you can just pass a column there and
99
00:05:32,919 --> 00:05:38,830
execute it and that will give you There
with the exclamation mark. Let's now try
101
00:05:38,830 --> 00:05:46,240
to extract R and E using negative
indexing, so something to know is that
103
00:05:46,240 --> 00:05:50,200
even though we are using a negative
indexing which starts from right to left
105
00:05:50,200 --> 00:05:56,530
the splitting will work from left to
right which means the first index you
107
00:05:56,530 --> 00:06:05,140
want to pass is that of the first item.
So R we pass minus 3 for R because
109
00:06:05,140 --> 00:06:15,300
you know we have minus 1, minus 2 and minus 3.
So minus 3 for R and then we want E
111
00:06:15,300 --> 00:06:21,450
which is minus 2,
but since splitting is upper bounce
113
00:06:21,450 --> 00:06:30,430
exclusive we want to pass minus one there.
So let's try that, minus one, execute and yeah
115
00:06:30,430 --> 00:06:35,360
we get the correct output.
That's the idea. If you didn't pass
117
00:06:35,360 --> 00:06:40,640
anything here you'd get the exclamation
mark as well. So it will take a while
119
00:06:40,640 --> 00:06:45,050
until we get used to this, but with some
practice you'll be able to remember
121
00:06:45,050 --> 00:06:52,010
these notations. Yeah, that's what I wanted
to cover in this lecture, so this was
123
00:06:52,010 --> 00:06:58,100
about string indexing and splitting,
so you split using the index that Python
125
00:06:58,100 --> 00:07:02,660
uses on the background. Yeah, I hope
you enjoyed this and I'll talk to you
127
00:07:02,660 --> 00:07:05,470
later, thanks!
38
views
Strings |Section 1| Celestial warrior
1
00:00:00,030 --> 00:00:05,240
Hi, welcome again. In the previous lecture
we were talking about numbers.
3
00:00:05,240 --> 00:00:11,370
Specifically we looked at integers and
float data types so these are referred
5
00:00:11,370 --> 00:00:15,809
to as data types in Python, so integers
and floats. In this lecture we're going
7
00:00:15,809 --> 00:00:23,970
to look at strings. I already mentioned
strings in the previous lecture.
9
00:00:23,970 --> 00:00:30,410
So a string is everything that goes
inside quotes. "Hi there". That is a string.
11
00:00:30,410 --> 00:00:40,170
And of course strings can be stored in
variables and you can print those
13
00:00:40,170 --> 00:00:44,579
variables, so we're using the interactive
shell now, but of course you can use a
15
00:00:44,579 --> 00:00:51,899
part of script as we did previously.
So numbers were used to do math operations,
17
00:00:51,899 --> 00:00:58,070
but strings, strings are used for
displaying information for the user.
19
00:00:58,070 --> 00:01:04,320
So let's say you have a web application, and
you want to generate some text
21
00:01:04,320 --> 00:01:10,140
dynamically on the web page, so you'd use
would use Python on the backend and
23
00:01:10,140 --> 00:01:18,509
Python would send strings to the HTML
code and HTML would render the
25
00:01:18,509 --> 00:01:24,689
code on the web page basically that's
the idea or if you want to create a
27
00:01:24,689 --> 00:01:31,229
notepad, an editor with Python such as
Atom let's say, all these things you see
29
00:01:31,229 --> 00:01:37,170
here so my program.py selection file,
so all this would be written as
31
00:01:37,170 --> 00:01:43,710
strings inside Python and then you
display them on the interface using
33
00:01:43,710 --> 00:01:48,780
various functions, for instance if you
want to display a string on the terminal
35
00:01:48,780 --> 00:01:57,030
you'd use the print function. Now strings
have methods associated with them. What I
37
00:01:57,030 --> 00:02:04,079
mean with methods is let me go ahead
and try out a method. Methods, for
39
00:02:04,079 --> 00:02:10,860
methods you need to use the dot notation.
So you'd refer to the string which is
41
00:02:10,860 --> 00:02:15,760
string c in our case actually
variable c, but the variable is holding
43
00:02:15,760 --> 00:02:24,750
the strings so it's the same as doing
here dot, so let's do c dot, replace and
45
00:02:24,750 --> 00:02:33,910
what I want to replace is e with i.
So I'm replacing, for string here I'm
47
00:02:33,910 --> 00:02:42,310
replacing letter e with later i and what
that will do it will replace all
49
00:02:42,310 --> 00:02:50,620
the letters, all the occurrences of this
letter in the string, so you'd ask where
51
00:02:50,620 --> 00:02:55,739
can we see these method? Well that's
very easy actually. You can do dir and
53
00:02:55,739 --> 00:03:01,540
then pass the string there, any string.
It could be c, it could be an empty string
55
00:03:01,540 --> 00:03:07,930
or whatever and that will print out all
the available methods that you can use
57
00:03:07,930 --> 00:03:16,630
with strings, so let's say c dot upper.
That will cover the strings to
59
00:03:16,630 --> 00:03:22,630
upper case letters. Now if you want to
know what these methods are about you
61
00:03:22,630 --> 00:03:29,380
could ask for help.
Just do help and then c, so the
63
00:03:29,380 --> 00:03:40,810
string or just empty string dot
65
00:03:44,320 --> 00:03:52,450
here replace method of the string
instance, so string is like the blueprint
67
00:03:52,450 --> 00:03:58,560
that creates strings objects or you can
say the data type that represents texts.
69
00:03:58,560 --> 00:04:08,950
Look here. Here is the syntax, so what you
do is, you use a string like we did.
71
00:04:08,950 --> 00:04:16,840
So let's say here replace. Then you open
brackets. Here is the old item you want
73
00:04:16,840 --> 00:04:24,490
to remove from the string and then the new
item we want to replace it with i and
75
00:04:24,490 --> 00:04:30,140
then what you see here in square brackets,
that means this parameter is
77
00:04:30,140 --> 00:04:35,330
optional so you can either pass it or
not and you know in the previous case we
79
00:04:35,330 --> 00:04:42,100
didn't pass it so what Python did was
it replaced all the occurrences of the i
81
00:04:42,100 --> 00:04:48,070
item but if you want to replace only the
first occurrence you do one there and
83
00:04:48,070 --> 00:04:54,770
that will replace all the first i over
the first e with i. That's how it works
85
00:04:54,770 --> 00:05:02,720
So strings, they support functions,
so print is a function. Here. And they support
87
00:05:02,720 --> 00:05:08,530
methods like this one here and they also
support operators so you get "Here"
89
00:05:08,530 --> 00:05:19,190
plus "There" and you get "HereThere" or you
can make it more readable like that so
91
00:05:19,190 --> 00:05:23,930
an empty space is also a string and yeah
that's about strings for now and I'll
93
00:05:23,930 --> 00:05:26,380
talk to you later.
38
views
Numbers and Operators |Section 1| Celestial Warrior
1
00:00:00,269 --> 00:00:04,680
Hi again, in this lecture I'll show you
how to do math operations with numbers.
3
00:00:04,680 --> 00:00:10,200
So we're going to look at math operators.
Let's go straight away and open a Python
5
00:00:10,200 --> 00:00:16,880
section and let me write a math
expression there. 20 minus 10 divided by
7
00:00:16,880 --> 00:00:31,019
5 times 3 in the power of 2 and what we
get is 2.0. So why did I write this a
9
00:00:31,019 --> 00:00:36,239
long expression? Well because I want to
explain to you the order of operators so
11
00:00:36,239 --> 00:00:41,760
when you have multiple operators in an
expression the one to be executed
13
00:00:41,760 --> 00:00:50,910
first is the exponent, so we have 3 in
power of 2 here that will give us 9.
15
00:00:50,910 --> 00:00:56,120
So power is the first to be executed and
also that is division and multiplication.
17
00:00:56,120 --> 00:01:00,210
Actually division and
multiplication are on the same level.
19
00:01:00,210 --> 00:01:06,330
We're talking about order but the one
that is first will be executed first so in
21
00:01:06,330 --> 00:01:11,640
this case division is the first one so
what will happen here is Python will
23
00:01:11,640 --> 00:01:16,500
ignore minus for now,
so it will divide 10 by 5 and it will
25
00:01:16,500 --> 00:01:30,180
give 2 then it will do 2 times 9.
So again 10 divided by 5, 2 times 9, 19.
27
00:01:30,180 --> 00:01:35,220
And then we have the rest of the operators
which could be a plus or minus so in
29
00:01:35,220 --> 00:01:45,299
this case we have minus so 20 minus 18.
We have 2.0. You'd ask why we have 2.0?
31
00:01:45,299 --> 00:01:54,509
Because 10 divided by 5 gives 2.0.
So division outputs a float in Python 3.
33
00:01:54,509 --> 00:02:00,540
If you are on Python 2, it would output
an integer. Now to make your expressions
35
00:02:00,540 --> 00:02:10,590
look better, you'd want to remove the
spaces for the power operator and also
37
00:02:10,590 --> 00:02:15,569
you do like that somehow you can read it
better,
39
00:02:15,569 --> 00:02:23,550
so 2 times 9 and then 20 minus 18 and so on.
You get the idea.
41
00:02:23,550 --> 00:02:33,599
You get 2.0 again and now if you want to
execute the minus operator first
43
00:02:33,599 --> 00:02:38,569
you'd want to put that inside brackets.
So why did we get 18? Well because 20
45
00:02:43,890 --> 00:02:52,940
minus 10 gives 10 and then 10 divided by
5 gives 2 and then 2 times 9 gives 18.
47
00:02:52,940 --> 00:03:02,459
That's the idea. And then you could also do
this, put these in brackets and what
49
00:03:02,459 --> 00:03:13,890
would happen here is you know 10 here,
15 here in the power of 2, so 15 in the
51
00:03:13,890 --> 00:03:20,459
power of 2, and then 10, so 10 divided by
15 in the power of 2. Let's see what we get.
53
00:03:20,459 --> 00:03:25,920
Yeah we got that, so let's see if I was
55
00:03:25,920 --> 00:03:31,260
Right.
15 in the power of 2 we get 225.
57
00:03:31,260 --> 00:03:42,510
And then we got 10 on the other side, so 10
divided by 225. We get that number.
59
00:03:42,510 --> 00:03:46,349
So whenever you want to change the
order of operators, you should use
61
00:03:46,349 --> 00:03:53,549
brackets and so I hope you get the idea
and please try this out, so try to do a
63
00:03:53,549 --> 00:03:57,569
few operations with numbers to get used to it.
And yeah, I'll talk to you later.
65
00:03:57,569 --> 00:04:00,379
Thanks.
19
views
Numbers |Section 1| Celestial Warrior
1
00:00:00,469 --> 00:00:07,770
Great, now let's learn numbers. Let me
open an interactive shell here to
3
00:00:07,770 --> 00:00:13,380
explain this faster and so numbers, you
know what numbers are. To create a number
5
00:00:13,380 --> 00:00:19,640
just type the number in python and
that's it and you also have operators
7
00:00:19,640 --> 00:00:25,350
like this is the plus operator and you
can also store numbers in variables
9
00:00:25,350 --> 00:00:33,020
let's say a equals to 2, b equals to 3
and then if you do a plus b you get 5.
11
00:00:33,020 --> 00:00:41,550
Something to note is that you know if
you do that in here, a equals to 2 b
13
00:00:41,550 --> 00:00:48,170
equals to 3, and then if you just do a
plus B and then you execute the script,
15
00:00:48,170 --> 00:00:58,739
let me open another terminal, you will
not get any output in there. The reason
17
00:00:58,739 --> 00:01:04,559
is that you are not printing out
anything in your program, so this is
19
00:01:04,559 --> 00:01:10,229
different from an interactive session.
Here if you simply do an operation or if
21
00:01:10,229 --> 00:01:16,650
you simply call a variable like that, you'll
get the output promptly, so this is just
23
00:01:16,650 --> 00:01:21,840
for simplicity when you when you try
things out, so you don't have to pass
25
00:01:21,840 --> 00:01:27,600
print explicitly, but Python will
understand that you met print. But here
27
00:01:27,600 --> 00:01:35,130
things are more strict, so you if you
want to print out the output, you want
29
00:01:35,130 --> 00:01:43,680
to say print a plus b and then execute
and you get the output and yeah basically
31
00:01:43,680 --> 00:01:48,960
that's the only difference between
executing a program from a Python file
33
00:01:48,960 --> 00:01:56,130
and running the code in the interactive
shell, so these are numbers and I can
35
00:01:56,130 --> 00:02:03,439
prove that if I do type 2 for instance.
You'll see that this is an INT
37
00:02:03,439 --> 00:02:10,289
type which means is an integer, so
basically we have integers and we have
39
00:02:10,289 --> 00:02:13,799
floats. These are the two main types
of numbers.
41
00:02:13,799 --> 00:02:22,379
A float is, let's say 2.4 it's a float
and I can prove that 2.4,
43
00:02:22,379 --> 00:02:30,359
type 2.4. You'll see that this
is a float, however if you do type 2 in
45
00:02:30,359 --> 00:02:35,609
brackets you'll see that this is a
string, it's not a number and therefore
47
00:02:35,609 --> 00:02:42,689
you cannot make operations using a
string and a number so Python will give
49
00:02:42,689 --> 00:02:47,430
you a type error and this is saying
cannot convert integer to string
51
00:02:47,430 --> 00:02:54,569
implicitly so when you do a string plus
something else Python expects that you
53
00:02:54,569 --> 00:03:00,000
were adding another string after a plus
sign, so that's why it says I cannot
55
00:03:00,000 --> 00:03:05,549
convert integer object, so it cannot
convert this to a string implicitly
57
00:03:05,549 --> 00:03:14,040
because if you were to do two plus three
you'd get 23 because Python treats these
59
00:03:14,040 --> 00:03:17,189
two as text. So to understand it
better
61
00:03:17,189 --> 00:03:26,729
let's add some text here b so you get
2a3b, so it's a concatenation operation.
63
00:03:26,729 --> 00:03:31,199
When the plus operator is used with
strings it is referred to as
65
00:03:31,199 --> 00:03:37,010
concatenation. Now let's go back to our
sample program that we used earlier
67
00:03:37,010 --> 00:03:44,549
which was this one in here, so now what
if instead of getting a greeting from
69
00:03:44,549 --> 00:03:53,220
the user you ask their for their age?
Enter your age, so this is just a message
71
00:03:53,220 --> 00:03:56,879
you can write anything you want there,
but just try to have a message that
73
00:03:56,879 --> 00:04:04,680
makes some sense to the user and so also
I'm going to change the variable name to
75
00:04:04,680 --> 00:04:11,400
something more meaningful, age and then
what I want to do is let's say new age,
77
00:04:11,400 --> 00:04:18,790
I want to calculate the age plus 50.
Why not?
79
00:04:18,790 --> 00:04:28,540
I'll print out the new age. Save the
script and let's go and execute this, we are
81
00:04:28,540 --> 00:04:33,130
going to get an error here I warn you.
But it's good that you see the errors
83
00:04:33,130 --> 00:04:36,030
that you get because that's how you
learn better.
85
00:04:36,030 --> 00:04:45,580
So Python my program, execute.
For now it's working fine because what
87
00:04:45,580 --> 00:04:51,280
happens is Python executes a script
line by line so it goes to the first
89
00:04:51,280 --> 00:04:56,980
line and if there is no error it
executes that line and in this case we have
91
00:04:56,980 --> 00:05:02,830
an input function so the program is on
standby mode so it expects from the user
93
00:05:02,830 --> 00:05:10,780
to enter the input, so let's enter the
input here. Let's say 28 and here is the
95
00:05:10,780 --> 00:05:16,420
error that we get. So again we saw this
error before. This is saying type error
97
00:05:16,420 --> 00:05:21,250
cannot convert int object to string
implicitly and you also have the line
99
00:05:21,250 --> 00:05:30,630
here that the error occurred. So these are
important messages and line two in here.
101
00:05:30,630 --> 00:05:37,270
So what's happening is that age is a
string it's not a number therefore
103
00:05:37,270 --> 00:05:43,180
python cannot add up a string with a
number and so why is age a string?
105
00:05:43,180 --> 00:05:48,520
Because that's what the input function
produces, so the input function get the
107
00:05:48,520 --> 00:05:55,000
user input and converts it to a string
so even if you pass a number there these
109
00:05:55,000 --> 00:05:59,140
will always convert everything to a
string.
111
00:05:59,140 --> 00:06:07,570
Therefore Python you know cannot
concatenate 28 plus 50 so what can you
113
00:06:07,570 --> 00:06:13,480
do in that case? Well you want to convert
that string into an integer and for
115
00:06:13,480 --> 00:06:19,360
that you want to use the int function,
so it's a Python function that does the work.
117
00:06:19,360 --> 00:06:27,570
As you can see it outputs now
the correct output so 28 plus 50 equals
119
00:06:27,570 --> 00:06:32,410
78 and similarly you can convert
numbers to strings
121
00:06:32,410 --> 00:06:42,280
and let's say str 50 and I think we'll
get an error here. Yeah, it says type error.
123
00:06:42,280 --> 00:06:48,580
Unsupported operand type four plus.
So you're adding up an integer with a
125
00:06:48,580 --> 00:06:57,850
string, so integer with a string. If you
like to convert this to a float, let me
127
00:06:57,850 --> 00:07:07,270
remove that, save, execute. 28. You get
78 point zero because what
129
00:07:07,270 --> 00:07:19,330
Python does when you use float is you
know float 28, it converts it to 28.0.
131
00:07:19,330 --> 00:07:24,160
So floats can be useful in certain scenarios.
And you'll have a lot of scenarios in the
133
00:07:24,160 --> 00:07:28,690
course, so don't worry about that now
because we have real world
135
00:07:28,690 --> 00:07:33,490
applications later on where you see the
real benefit of everything that you are
137
00:07:33,490 --> 00:07:39,040
explaining here in this early sections
that cover the basics. So this is about
139
00:07:39,040 --> 00:07:43,540
numbers. Try to practice them a little
bit and I'll talk to you in the next
141
00:07:43,540 --> 00:07:45,690
lecture.
19
views
Variables |Section 1| Celestial Warrior
1
00:00:00,210 --> 00:00:05,400
Hi, in this lecture you're going to learn
about variables. So what is a variable?
3
00:00:05,400 --> 00:00:10,980
Well a variable is like a container
where you can store various data types
5
00:00:10,980 --> 00:00:18,539
in Python for instance this program now
this doesn't have any variable in it for
7
00:00:18,539 --> 00:00:23,699
now, so let me try and execute it and
you'll see that this prints out of the
9
00:00:23,699 --> 00:00:29,609
hello string. String is a data type, but
we'll cover that later. Now what is the
11
00:00:29,609 --> 00:00:38,790
variable? Well and variables will keep
your code more organized. Let me create a
13
00:00:38,790 --> 00:00:45,149
variable here, greeting so you can call
your variables whatever you like you can
15
00:00:45,149 --> 00:00:50,070
give them any name however there are
some naming rules which I'll mention in
17
00:00:50,070 --> 00:00:55,739
just a bit, so let me create this
variable for now. I'll store in this
19
00:00:55,739 --> 00:01:01,980
variable, I said variables are like
containers so I'll store this text in the
21
00:01:01,980 --> 00:01:07,799
variable greeting and then what I do is
instead of doing print hello I'll do
23
00:01:07,799 --> 00:01:15,570
print greeting and save the script ctrl
s and then go through the command line,
25
00:01:15,570 --> 00:01:22,229
the upper arrow key, enter to execute and
you get the same output. So this is how
27
00:01:22,229 --> 00:01:26,670
you create a variable you don't use any
quotes because when you use quotes you
29
00:01:26,670 --> 00:01:34,229
are creating a string data type not a
variable and you can also use a dash
31
00:01:34,229 --> 00:01:43,439
before the variable that would also be
fine so you get the output correctly
33
00:01:43,439 --> 00:01:50,549
but you cannot use numbers before the
variable ctrl s to save, execute and get
35
00:01:50,549 --> 00:01:56,820
an invalid syntax, so this syntax is not
valid in Python, but you can use numbers
37
00:01:56,820 --> 00:02:09,119
after or between the variable name let's
say here greeting2. So that would
39
00:02:09,119 --> 00:02:13,860
be fine to use so that's the condition
about naming
41
00:02:13,860 --> 00:02:19,470
your variables, you can start them with a
letter or a, or an underscore. Now if
43
00:02:19,470 --> 00:02:24,720
you're not experienced with programming
you may still be confused on why do we
45
00:02:24,720 --> 00:02:31,370
use these variables? Well this is a very
simple program, actually very useless, but
47
00:02:31,370 --> 00:02:37,140
normally your programs will interact
with your users. That is what we call a
49
00:02:37,140 --> 00:02:43,140
real program. You create a program to
give it to people so they can interact
51
00:02:43,140 --> 00:02:48,810
with the program they can generate all
different outputs and so on, so let's go
53
00:02:48,810 --> 00:02:57,060
Ahead and make this program more dynamic, so
what I'll do here is I'll get the text
55
00:02:57,060 --> 00:03:03,720
from the user interactively. To do that
you need to use the input function, this
57
00:03:03,720 --> 00:03:09,959
is also a Python function and inside
the function you can enter a message to the
59
00:03:09,959 --> 00:03:16,640
user so let's say write a greeting.
A column and make some space there and then
61
00:03:20,519 --> 00:03:28,290
you print greeting2, save the script.
Execute and now the program is asking
63
00:03:28,290 --> 00:03:36,060
us to enter some input so let's go ahead
and say hi this time and we get the output
65
00:03:36,060 --> 00:03:42,150
Printed out in here, hi, so what
happened here is we created a variable
67
00:03:42,150 --> 00:03:49,320
and that variable will store the value
that the user submits in the command line
69
00:03:49,320 --> 00:03:55,590
here, in the terminal so the user
submitted the hi text and this text will be
71
00:03:55,590 --> 00:04:00,989
stored in this variable so as I said
variables are like containers and then can
73
00:04:00,989 --> 00:04:06,390
use this text in various ways, but in
this scenario I'm just passing it to the
75
00:04:06,390 --> 00:04:12,030
print function, so I want to print it out
on the terminal here and the print
77
00:04:12,030 --> 00:04:16,709
function will get the value that is
stored in the greeting2 variable and
79
00:04:16,709 --> 00:04:22,650
print it out on the terminal. That's it.
And yeah, I'd like to close this lecture
81
00:04:22,650 --> 00:04:27,110
now and I'll see you in the next lecture.
4
views
Setting up Atom on Mac | Celestial Warrior
External Link:~ https://www.windowscentral.com/how-add-open-command-prompt-window-here-back-context-menu-windows-10
Frequently Asked Questions: file:///C:/Users/Taha%20Umar%20Farooq/Videos/FOLDER/[Tutsgalaxy.com]%20-%20The%20Python%20Mega%20Course%20Build%2010%20Real%20World%20Applications/01%20Getting%20Started/009%20Installation%20FAQs.html
1
00:00:00,000 --> 00:00:06,870
Hello again. This is yet another lecture
for Mac users and in this s lecture I'm going
3
00:00:06,870 --> 00:00:12,389
to show you how to set up for the Atom
editor on a Mac computer so if you're a
5
00:00:12,389 --> 00:00:19,439
Windows user please skip this lecture.
Atom is quite an advanced editor but if
7
00:00:19,439 --> 00:00:25,230
you have your favorite editor by now you
can go ahead and use that editor so
9
00:00:25,230 --> 00:00:32,009
that's perfectly fine. If you choose to
Look, to have a look at the Atom editor
11
00:00:32,009 --> 00:00:38,100
then please follow this lecture.
First of all you're going to have to
13
00:00:38,100 --> 00:00:50,879
download Atom. Simply search for Atom
editor and then go to its website which
15
00:00:50,879 --> 00:01:01,500
Atom.io, download for Mac and
so while you wait for the download this
17
00:01:01,500 --> 00:01:09,360
is how the Atom editor will look like so
like that and so the download has finished
19
00:01:09,360 --> 00:01:25,680
on my end and you can go and click Atom.
Click open and basically Atom
21
00:01:25,680 --> 00:01:31,680
has been downloaded and has been
installed so you've got a welcome guide
23
00:01:31,680 --> 00:01:41,280
in here, you can close that. This is
an empty file. I'm going to close it as
25
00:01:41,280 --> 00:01:51,090
well and let me close Atom and try to
show you how to actually open it from
27
00:01:51,090 --> 00:01:56,700
Scratch, so how to start Adam from the
beginning and so let's see how to create
29
00:01:56,700 --> 00:02:03,390
a program and execute a program with
Atom and now you want to launch Atom
31
00:02:03,390 --> 00:02:10,369
and then go to view,
developer open in dev mode and that
33
00:02:15,319 --> 00:02:20,209
will ask you for the directory that you
want to open, so go to the desktop, go to
35
00:02:20,209 --> 00:02:24,620
the folder where you have your programs
or the folder where you want to create
37
00:02:24,620 --> 00:02:35,209
your programs if you haven't created any
program yet and click open.
39
00:02:35,209 --> 00:02:44,510
And so you get this tree view in here and close that
and double-clicking a file will open the
41
00:02:44,510 --> 00:02:51,109
file here in the editor and so you can
go ahead and add more code there.
43
00:02:51,109 --> 00:02:59,030
Three plus four, you can also
create other files, so you right click on
45
00:02:59,030 --> 00:03:11,750
the main directory, go to new file,
Myprogram2.py. Let's say print
47
00:03:11,750 --> 00:03:19,849
five this time and ctrl s to save and so you
have two programs now in here and you
49
00:03:19,849 --> 00:03:26,510
can switch through different files by
clicking the tabs in here. So how to
51
00:03:26,510 --> 00:03:31,669
execute this programs now? Well you can
use your Mac terminal just as you did in
53
00:03:31,669 --> 00:03:36,470
the previous lecture or even
better you can go ahead and install a
55
00:03:36,470 --> 00:03:49,890
package, so go two packages settings view,
open, go to install.
57
00:03:49,890 --> 00:04:10,860
Type platformio ide, so dash ide dash
terminal. Enter to search and then go
59
00:04:10,860 --> 00:04:17,459
ahead and install the platformio ide
61
00:04:20,760 --> 00:04:25,200
platformio ide terminal has been
installed successfully, so you can go ahead
63
00:04:25,200 --> 00:04:32,370
and close settings and you'll see a plus
icon in here. If you click that
65
00:04:32,370 --> 00:04:43,230
an embedded terminal will open in Atom
editor and you can drag it down and up
67
00:04:43,230 --> 00:04:52,650
to resize it. If you see closely you'll
notice that the terminal is open in the
69
00:04:52,650 --> 00:05:00,150
current folder, see so untitled folder
this one in here and that means you can go
71
00:05:00,150 --> 00:05:07,290
ahead and use Python now, Python 3 and
execute a program, my program, my temp
73
00:05:07,290 --> 00:05:17,550
program.py and that will print
hello. Actually it was supposed to print
75
00:05:17,550 --> 00:05:24,120
hello and then 7 but maybe I didn't
save the script, so click on the editor
77
00:05:24,120 --> 00:05:31,890
here, ctrl s to save and then click on
the terminal again press the upper arrow
79
00:05:31,890 --> 00:05:38,250
key to call a previously executed
command and execute and you get hello
81
00:05:38,250 --> 00:05:43,740
and 7 and yeah this is how to use
the Atom editor. Now this is good
83
00:05:43,740 --> 00:05:48,360
because you have both, you have the
directory tree here with your files, with
85
00:05:48,360 --> 00:05:52,530
your Python files and you have the
editor here and you also have an
87
00:05:52,530 --> 00:05:57,539
embedded terminal, so I hope you like
Atom and from now on I'll be using
89
00:05:57,539 --> 00:06:03,680
Windows, but things will be exactly the
same because Python is a cross-platform
91
00:06:03,680 --> 00:06:09,620
so which means the code is exactly the
same on Mac, and Windows, and Linux as
93
00:06:09,620 --> 00:06:15,020
well so just follow me and I hope you
succeed with Python. I'll talk to you
95
00:06:15,020 --> 00:06:17,169
in the next lecture.
17
views
Setting up Atom on Windows | Celestial Warrior
External Links:~ https://www.windowscentral.com/how-add-open-command-prompt-window-here-back-context-menu-windows-10
Frequently Asked Questions: file:///C:/Users/Taha%20Umar%20Farooq/Videos/FOLDER/[Tutsgalaxy.com]%20-%20The%20Python%20Mega%20Course%20Build%2010%20Real%20World%20Applications/01%20Getting%20Started/009%20Installation%20FAQs.html
1
00:00:00,000 --> 00:00:05,190
Hey hello again in this lecture I'll
show you how to download and install the
3
00:00:05,190 --> 00:00:11,309
Atom editor. Atom is a powerful and
very efficient editor for writing and
5
00:00:11,309 --> 00:00:18,510
executing Python code. This lecture is
specifically for windows, so for Mac
7
00:00:18,510 --> 00:00:24,869
users please go to the next lecture.
So before what we did was we installed
9
00:00:24,869 --> 00:00:30,660
Python and we tried a python interactive
session in the command prompt and we
11
00:00:30,660 --> 00:00:36,059
also created a python program using a
simple editor such as Notepad or
13
00:00:36,059 --> 00:00:40,860
whatever editor you had in on your
Windows, so now we're going to use
15
00:00:40,860 --> 00:00:45,570
another editor which is Atom and you
need to go and install it from and
17
00:00:45,570 --> 00:00:54,480
download it from the Atom official web
Page, just click the button there.
19
00:00:54,480 --> 00:01:01,800
Say file and it will take a while until it
downloads. Great, the download process
21
00:01:01,800 --> 00:01:08,100
has finished now so I'm going to click
on Atom, click on run to run
23
00:01:08,100 --> 00:01:16,380
the installation and yeah you have to wait a
while until Atom is installed.
25
00:01:16,380 --> 00:01:23,189
And yeah, the installation was quick and now Atom has
been opened on my Windows, however I would
27
00:01:23,189 --> 00:01:29,909
like to close it here and show you the
standard way of how to open Atom, so the
29
00:01:29,909 --> 00:01:35,670
way that you'd normally open Atom
when you start programming so one way to
31
00:01:35,670 --> 00:01:41,430
open Atom is to browse through your
programs and open Atom by clicking.
33
00:01:41,430 --> 00:01:48,630
However that we open atom in a random
directory so the most efficient way to
35
00:01:48,630 --> 00:01:53,790
open Atom is to go to the directory
where you have your programs so the
37
00:01:53,790 --> 00:01:58,920
program that we have is myprogram.py.
And then outside in here right click
39
00:01:58,920 --> 00:02:04,649
and go to open with Atom, but if you
don't see open with Atom like I do. so
41
00:02:04,649 --> 00:02:09,250
I don't have open with Atom for now,
then go and open Atom
43
00:02:09,250 --> 00:02:22,120
for your programs. Go to file, settings
and then check these two options so this
45
00:02:22,120 --> 00:02:27,250
options were under system, under the
system tab of the settings menu and I
47
00:02:27,250 --> 00:02:35,050
close Atom and I right click and now
you should see this menu in here. Open it
49
00:02:35,050 --> 00:02:41,620
and you should now see this view but if
you see, if you don't see the tree
51
00:02:41,620 --> 00:02:50,260
view in here, the directory tree view, go to
view and then toggle tree view so you can
53
00:02:50,260 --> 00:02:59,950
toggle it off and on. You can close this
and then to open your program, so to edit
55
00:02:59,950 --> 00:03:04,989
your program you simply double-click
your file your Python file and yeah, you
57
00:03:04,989 --> 00:03:13,209
start writing. Print. Start adding more
code and always press ctrl s to save and
59
00:03:13,209 --> 00:03:21,010
now to execute this code you can either
go here and open your command window
61
00:03:21,010 --> 00:03:26,830
here with Shift right click or there is a
more efficient way to do it. You need
63
00:03:26,830 --> 00:03:34,870
to go to file settings, packages, sorry
install, you want to install a new
65
00:03:34,870 --> 00:03:48,610
package and search for that platformio
67
00:03:51,340 --> 00:03:58,299
click install and so this is the package
that we will install or will integrate a
69
00:03:58,299 --> 00:04:04,510
terminal, a command line inside Atom so
you don't have to use external windows
71
00:04:04,510 --> 00:04:10,180
and native windows command line and you
can run your programs from within Atom.
73
00:04:10,180 --> 00:04:15,610
All right, platform ide terminal was
installed successfully, you can close the
75
00:04:15,610 --> 00:04:22,510
settings tab and so now you should see
this plus icon in here, click on the icon.
77
00:04:22,510 --> 00:04:26,870
Will open a new terminal so now this is
79
00:04:26,870 --> 00:04:32,030
opening the current directory which
reflects the directory of your Atom
81
00:04:32,030 --> 00:04:37,400
directory, so of your programs. Now you
can go ahead and type in commands you to
83
00:04:37,400 --> 00:04:43,880
run your Python program, so my tab for
auto completion and that will
85
00:04:43,880 --> 00:04:48,260
auto complete like that, don't worry about this.
This just means the current
87
00:04:48,260 --> 00:04:55,520
directory, so when you do Tab, Tab will
add this. Ignore it and press Enter and
89
00:04:55,520 --> 00:05:02,690
you see the program output in the
terminal. You can clear the terminal from
91
00:05:02,690 --> 00:05:07,970
the codes from previously executed
commands and of course you can call
93
00:05:07,970 --> 00:05:13,730
previously executed commands with the
upper arrow key. Execute again and don't
95
00:05:13,730 --> 00:05:21,380
confuse executing a program with opening
a Python interactive session, so here you
97
00:05:21,380 --> 00:05:28,880
write Python code like print 7 for
example interactively but if you exit
99
00:05:28,880 --> 00:05:36,229
this because I see some students will go
ahead and run Python code in here but
101
00:05:36,229 --> 00:05:43,820
this is wrong, this is not Python,
this is windows, the windows command line, so
103
00:05:43,820 --> 00:05:49,820
don't do that instead write Python code
inside an interactive session or inside
105
00:05:49,820 --> 00:05:54,020
the Python file. That's the idea and
this is Atom. I hope you like it
107
00:05:54,020 --> 00:05:57,160
and I'll talk to you later.
52
views
Creating and Executing a Python Program on Mac | Celestial Warrior
External Link:~ https://www.windowscentral.com/how-add-open-command-prompt-window-here-back-context-menu-windows-10
Frequently Asked Questions: file:///C:/Users/Taha%20Umar%20Farooq/Videos/FOLDER/[Tutsgalaxy.com]%20-%20The%20Python%20Mega%20Course%20Build%2010%20Real%20World%20Applications/01%20Getting%20Started/009%20Installation%20FAQs.html
1
00:00:00,000 --> 00:00:04,859
Hey welcome to this new lecture and in
this lecture I'm going to show you how
3
00:00:04,859 --> 00:00:12,059
to create and execute a Python program
on a Mac computer, so if you are a
5
00:00:12,059 --> 00:00:17,520
Windows user please skip this lecture.
And so in the previous lecture I showed
7
00:00:17,520 --> 00:00:25,080
you how to install Python 3 and also how
to launch a Python 3 interactive
9
00:00:25,080 --> 00:00:33,809
Session, so which is good for executing
and testing out code so like that line.
11
00:00:33,809 --> 00:00:43,410
Like my line, but if you quit the terminal
you're going to lose that code, so you
13
00:00:43,410 --> 00:00:49,800
want to find out a way to save that code
in a file so that you can develop your
15
00:00:49,800 --> 00:00:56,010
program step by step, so you can go back
to your code, add the code, modify it, and
17
00:00:56,010 --> 00:01:01,379
most importantly you can execute
multiple lines at once instead of
19
00:01:01,379 --> 00:01:06,659
executing line by line interactively.
So let's go ahead and do that.
21
00:01:06,659 --> 00:01:11,700
Normally you need to figure out,
to create a directory where you want to
23
00:01:11,700 --> 00:01:17,850
save your program, so I'm going to create
this folder in here and this is empty
25
00:01:17,850 --> 00:01:25,590
for now, so I'm going to create a
file there. There may be different ways
27
00:01:25,590 --> 00:01:32,189
of creating a Python file all but I prefer
to do it via the terminal.
29
00:01:32,189 --> 00:01:37,560
So currently you do PWD, that will print
out the current directory of the terminal,
31
00:01:37,560 --> 00:01:43,049
so the terminal is an app and it is
opened in the current directory, in a certain
33
00:01:43,049 --> 00:01:50,399
directory which is this on for the
moment. If you do ls you will see the
35
00:01:50,399 --> 00:01:56,939
directories and files or the current
Directory, so users admin has this
37
00:01:56,939 --> 00:02:03,090
directors, has these folders so if you
want to go to the desktop folder,
39
00:02:03,090 --> 00:02:08,080
you'd want to do
desktop or you can use the Tab for auto
41
00:02:08,080 --> 00:02:16,360
Completion, so the tab key and then Un Tab.
That will take you to the
43
00:02:16,360 --> 00:02:24,250
untitled folder. Execute and I'll see
that your current folder is the untitled
45
00:02:24,250 --> 00:02:32,020
folder and that means you can go ahead
and create a file now. Let's say touch my
47
00:02:32,020 --> 00:02:42,070
program dot py, so you can give it
any name that you want. Execute and that
49
00:02:42,070 --> 00:02:49,420
will create an empty file which for now
you can go ahead and edit it with text
51
00:02:49,420 --> 00:02:54,160
edit for example. Later on we're going
to install Atom editor which is much
53
00:02:54,160 --> 00:02:59,709
more advanced than this simple editor
but for now this should be enough to
55
00:02:59,709 --> 00:03:06,580
just get you started to get you the
idea on how to execute a Python program
57
00:03:06,580 --> 00:03:12,340
so let create code for the program.
This program will simply
59
00:03:12,340 --> 00:03:20,230
print out the hello text so you need, you
use this print keyword and then you use
61
00:03:20,230 --> 00:03:28,600
double quotes and then the text hello and
then another double quote to close the
63
00:03:28,600 --> 00:03:32,760
text and then a closing bracket.
65
00:03:46,470 --> 00:03:56,520
CTRL S to save, exit text edit and then
go back to the terminal and what you do
67
00:03:56,520 --> 00:04:00,930
here now to execute your program, the
program that you just created, you use the
69
00:04:00,930 --> 00:04:07,200
Python 3 command and then my, you can
type my program dot py or you can use
71
00:04:07,200 --> 00:04:14,760
tab to autocomplete the name, enter to
execute and you get the output of the program.
73
00:04:14,760 --> 00:04:20,040
Print it out in the terminal so in this
case the output is hello and yeah that's how
75
00:04:20,040 --> 00:04:24,570
you create and execute a program on Mac
and similarly you can go ahead and
77
00:04:24,570 --> 00:04:31,050
create other Python programs in here just
as we did with my program dot py. I can also
79
00:04:31,050 --> 00:04:35,730
show you a quick way to actually open
the terminal in this current directory
81
00:04:35,730 --> 00:04:42,690
without having to do CD and try the
path name, so let's do that.
83
00:04:42,690 --> 00:04:50,460
You go here, system preferences, so this is a
one time configuration. Go to keyboard.
85
00:04:50,460 --> 00:05:03,150
Shortcuts and then in the shortcuts tab
you go to services, scroll down and see
87
00:05:03,150 --> 00:05:08,070
you probably have this option
unchecked, so you want to make sure you
89
00:05:08,070 --> 00:05:13,590
have checked the new terminal at folder
option just like I have it here, so make
91
00:05:13,590 --> 00:05:20,490
sure make sure that is checked. Close
this and then what you do is to open the
93
00:05:20,490 --> 00:05:25,350
terminal in a current folder, in a
specific folder you right click on
95
00:05:25,350 --> 00:05:34,110
folder and then go to New Terminal at
Folder and now what you do is Python 3
97
00:05:34,110 --> 00:05:39,390
Myprogram.py, execute and you get the
Output, so that's a quick way to open
99
00:05:39,390 --> 00:05:44,100
the terminal in a specific folder and
yeah that's about this lecture.
101
00:05:44,100 --> 00:05:50,210
I hope everything went smoothly and I'll
talk to you later. Thanks.
52
views
Creating and Executing a Python Program on Windows | Celestial Warrior
External Link:~ https://www.windowscentral.com/how-add-open-command-prompt-window-here-back-context-menu-windows-10
Frequently Asked Questions: file:///C:/Users/Taha%20Umar%20Farooq/Videos/FOLDER/[Tutsgalaxy.com]%20-%20The%20Python%20Mega%20Course%20Build%2010%20Real%20World%20Applications/01%20Getting%20Started/009%20Installation%20FAQs.html
0
00:00.240 --> 00:08.010
H, again. In this video I'll show you how to create and how to run a Python program. In the previous lecture
1
00:08.060 --> 00:13.070
we set up Python and we ran a Python interactive session.
2
00:13.260 --> 00:15.960
So that was like a running code line by line.
3
00:16.170 --> 00:22.020
But what you want to do instead, we want to write code that we are able to save it in a file and we're
4
00:22.020 --> 00:25.150
able to run all the code at once.
5
00:25.200 --> 00:27.220
So that's what we're going to do in this lecture.
6
00:27.460 --> 00:30.720
And this is targeted to Windows users.
7
00:30.720 --> 00:32.940
So if you have a Windows computer please watch this.
8
00:32.970 --> 00:38.550
If you have a Mac computer or even a Linux computer please skip this video and go to the next video
9
00:39.240 --> 00:44.070
where I show you the same process but on a Mac computer. So Windows users,
10
00:44.220 --> 00:52.230
let's create a Python program and we're going to use very basic tools for now.
11
00:52.250 --> 00:59.760
But then in the next lecture we're going to download and use an advanced editor. On Windows before creating
12
00:59.760 --> 01:00.850
the program
13
01:00.890 --> 01:06.840
there is something you need to make sure of. You go to organize and you go to folder search options
14
01:07.590 --> 01:12.990
you go to View and then look here for this option.
15
01:12.990 --> 01:15.510
Hide extensions for known file types.
16
01:15.600 --> 01:23.380
You want to make sure this option is unchecked just as I have it here. Don't check it.
17
01:24.230 --> 01:34.170
Cancel that and with the option unchecked you go had and right click and create create a text document.
18
01:34.710 --> 01:36.180
And put a name.
19
01:36.180 --> 01:41.670
Let's say my program and you want to remove the txt extension.
20
01:42.120 --> 01:43.660
And want to say .py.
21
01:47.800 --> 01:48.290
OK.
22
01:48.320 --> 01:49.160
No problem.
23
01:49.190 --> 01:51.100
I want to change the name.
24
01:51.470 --> 01:54.970
Now this is a file where you're going to write your Python code.
25
01:55.070 --> 01:59.750
So just go ahead and edit, edit it with an editor that you have.
26
01:59.760 --> 02:02.760
Maybe Notepad.
27
02:02.900 --> 02:05.030
We're going to use Atom later. For now
28
02:05.030 --> 02:05.790
this is good.
29
02:06.070 --> 02:12.080
And this program, what this will do, it will print out some text.
30
02:12.320 --> 02:14.690
Let's say the 'Hello' text.
31
02:14.750 --> 02:20.300
So that's what you do, you use the Python print function, and the print function
32
02:20.300 --> 02:21.760
has this syntax.
33
02:22.070 --> 02:26.320
So it expects to have some brackets opening and closing brackets.
34
02:26.590 --> 02:31.690
And inside these brackets you want to pass the thing that you want to print out.
35
02:31.700 --> 02:36.020
In this case this is called a string. We'll cover strings later.
36
02:36.140 --> 02:38.520
Or you can say text.
37
02:38.660 --> 02:42.690
So we want to print out some text and text goes inside quotes always.
38
02:42.940 --> 02:53.930
So strings go, strings go inside quotes. Make sure you want to save control s, exit the script and then click
39
02:54.030 --> 03:01.610
outside in here and hold shift and then right click and then you want to go to
40
03:07.910 --> 03:12.330
Open command window here that you open the command prompt.
41
03:12.440 --> 03:16.560
Look carefully and you'll see that the command prompt was open.
42
03:16.560 --> 03:25.370
In this current directory which reflects which is actually the directory of your python file.
43
03:25.430 --> 03:28.960
So this one in here. This one in here is in here.
44
03:29.360 --> 03:36.050
So before you run the program you need to have these two directories the same because if you open
45
03:36.050 --> 03:44.250
the command prompt from here as we did in the previous lecture that will go to another default directory.
46
03:45.240 --> 03:48.870
So the best way to do it as I did it here.
47
03:48.990 --> 03:56.520
Click and go to open command window here and then what you do is you type python and then the name of your
48
03:56.520 --> 04:07.080
program, my program.py. So what you're doing is you're using Python, the python program to execute
49
04:07.200 --> 04:14.690
this file and you get the output in the command line which is hello, the text Hello.
50
04:15.960 --> 04:25.780
Similarly I could execute it with my Python 3.6, dot py and you get the same output again.
51
04:25.890 --> 04:29.180
So 3.6, 3.7 doesn't make any big difference.
52
04:29.190 --> 04:31.560
The differences are very very slight.
53
04:31.590 --> 04:33.670
So you don't have to worry about that.
54
04:33.690 --> 04:36.320
Let me execute it one more time, Python.
55
04:36.540 --> 04:44.480
If you type now my and if you press the Tab key, that will autocomplete the name of your file.
56
04:44.480 --> 04:50.130
So it will find the name of your file because it knows that you are in the current directory so use Tab
57
04:50.130 --> 04:54.670
for auto completion and yeah, again you execute it and you get the output.
58
04:54.930 --> 05:02.130
Of course you can go ahead and add other lines of code there. Let's say print 7, a number.
59
05:02.130 --> 05:10.710
Numbers don't go inside quotes, control s to save and close and go to the command line again and press
60
05:10.890 --> 05:15.780
the upper arrow key to execute, to call a previously executed statement.
61
05:15.840 --> 05:19.580
So upper arrow key will call Python my program.py. Execute and you get
62
05:19.590 --> 05:19.990
Hello
63
05:20.020 --> 05:22.120
and 7. That's the idea.
64
05:22.140 --> 05:24.890
Hope you enjoyed this and I'll talk to you in the next lecture.
17
views