Transcripts
1. 01 To do list introduction: In this Python practice, we will learn how to
create a to do List app. So in our application, they can add task, show task, or remove tasks. And all the tasks will be saved inside a file as a JSON file. So every time that you
run the application, the tasks will be
there and you know what task you should do
today. So let's begin.
2. 02 Tasks list: First thing first, we need
to create a Python project. So for doing that, I will
go to the desktop in here, create a new folder,
and I want to call it to do List, right? Let me put it over here. Now, go to VS code, click in here and open that folder up that is in
or desktop to do List. Head select folder, and in here, create a new file, and I want to call it to
do list dot PY, right? Let me zoom in here so
you can see it better. First thing first in here, I want to say that
this is a to do is application, right? So I use
a print, and I will say, welcome to your to do list. Ah. After that, I want to create a list so we can add
tasks to it, right? So for that, I will say tasks
is equal to a empty list. Now, for adding a
task to this list, we can just say tasks
dot append, right? And with this, for example, let's just say learn
Python basics, right? And know this task will be added to the
tasks array, right? And we can show it. Can
say print tasks, right? Now through a to save note. Let's just run that
terminal in here, PY after that attack, and it will put the rest for us. You can see we have one
tasks in our tasks list. So everything is ready for
creating or to do list.
3. 03 Add new task: No, let's just give the user ability to add
tasks to our tasks list. For doing that, we don't
want to add tasks like this. What we want to do? We want
to create a menu, right? So for that, I will use a
printing here, and I will say, What would you
like to do, right? And just with that, if you run, one problem that you can see is
that when we say, welcome to your to do list app, we don't have any
space after that, and we are saying, What
would you like to do? Of course, I didn't
put K in here. What would you
like to do, right? For adding a space
between these two line, we can say print, right, and add a NTs string. If you run, let's see
what will happen. You can see there is a space
between these two line, but another way to do this, we can use backslash
N. This backslash N, it's like hitting an
inter in a text file. It will go to the new line. This is called New line. So before writing all of these, it will go to the new line
first with this character, and after that, it
will write the rest. So in E control as a save. Now if you run, you can see there is a
space between them. That's cool, right? Now, next
thing that I want to do, I want to give the
user some options. For example, let's
just say print. The first option, I will
call it one dot, at task. And the next option for
no, I will say print. Again, just like before, two dot view tasks, right? And no, if we save and run, you can see it
looks good, right? It will say what you like
to do, and after that, it gives you some option
at tasks or view tasks. So after showing all of these, we need to get the user input to see if they want to
use one or two, right? So in here, we say input. And in there, I want to say, for example, enter your choice. And I can give them a hint like open and close
parenss and in here, I can say one or two. And after that, a column
and a space, right? With that done, let's just run it to see how it looks
like. You can see. It says, What would
you like to do? You have two
options, one or two. We can put one in
there to add tasks, but right now we are not
doing anything with it. So for getting this input and decide what we
want to do in here, we can save it
inside the variable. Let's just call it choice. Is equal to this input. Now, we know what user
decided to do, right? And for that, I want
to use a if statement. I will say I choice was equal to let's just say one.
What should happen? We should get the new tasks that we want to add
from input, right? So in here, again, we need
to use input, and with that, I want to say enter
the new task. Hold on and a space after that and we want to
save it somewhere, I will save it
inside the variable, call it newTask is
equal to this input. And after that, when
they hit Enter, it will go to the next
line and we can say tasks, right dot append this
new task, right? And after that, we
can use a print to say task add it.
That's it, right? Now with that done, let's just
run or application again. If we put one in
there and head inter, it says, enter the new tasks. Let's just say learning
Python, right? Head Inter, it
will add it there, but we are not showing it. For showing it,
we can go down in here and again,
print tasks, right? So you can see it better. In here, if I run again,
we want to add task. So let's just put one
in there, head inter. Now in here, the new task. Creating a to do list,
right? Head inter. No, you can see it
did added two tasks, and you can see it creating
a to do list, right? No, next choice is two
for viewing tasks. You try to do that. We
will do it in next video.
4. 04 View tasks: No, we want to do the
choice to view tasks. Let's see how we can do that. For doing that in here, I want to do it with LF, right, and we want
to check the choice. If it is equal to two, right? If it is equal to two, it means they need to see
the tasks, right? So in here, I will
add the colon, and in here, first,
I will say print. And first thing that I want
to do in here back sN, so it will go to the new line, so it will be more readable. After that, I want to save your tasks and a
colon after that. Next, I want to
show those tasks. I can use four tasks in tasks, right, and we want to show them. I can just say print. I want to use the FS string. I can add the underscore
after that and a space, after that, a curly bracket
and show the task, right? Cool. No, with that done. We don't need this print
because we can show it. But the problem is, right now, there is no tasks there to show, so we can test, right? So what I want to do I want
to take all of these like this control s to cut
and create a wild true. Okay, and put all the
code inside there. You can see we need to
indent all of them. For indenting all of them, one way is to click on
each one of them head tap, so we indent it
like this, right. But the other way to do it, we can just hold out
and shift and drag from top to bottom like
this after the head tap, and now you can see all of
them are indented, right? Now, with that done, let's
just test over code. If here, say add tasks, it will enter the new task. I will say learn Python. Next, it will tell you do you want to view task or at task? Let's just add another task. Go to ML, for example. Next, let's just show it. If you put two and
head in, you can see. In here, it says, You tasks is learn Python and go to Mal. You can see the tasks. After that, again, it will tell you what do you want to do. So that's it. Now we have
choice two for viewing tasks.
5. 5 Add more option: Next thing that I
want to do first, if they put a number that is not a choice,
what should happen? We check I choice
is one, do this. If choice is two,
do this, right? But what if none of this
number happen, right? So in that case, we say else. When none of this
number happened, the else will happen. And in here, we can say print invalid number,
right? And that's it. Let's just test that out,
Control C to get out of the execution of the last
application, run again. And in here, if I put
three, what will happen, I will say invalid
number and it will go until do you want to add
tasks or view tasks, right? Invalid number. No, next
thing that I want to do, I want to add more choices. For example, removing a task. So in here, I can just add
LF in here, L F, right? In this case, we want to get the choice and check if
it is equal to three. But what if three happen? We want to remove a task. So for no, I will say
print, remove task, right. And next, let's just
add another ALIF, L F. And in this case, I want to check the choice
if it is equal to four, let's see what will happen,
what we want to do. We want to quit, right? So we can say print. Good. Bye, right?
And after that, we can just break out of
the loop, this wild loop. But right now, we don't
give them the option. What I want to do, I
want to delete this just like that and add more options. So in here, I can just
copy and paste this, right, and say
three, four, remove. Task. Another one, we
can say print f dot. After that, exit, right? That's it. Now, let's
just test that. If you run, you can see, we can add task,
task, remove task. If you remove task, you can see it will
say just remove task. It won't do anything,
right? For no, of course. But if you put foreign there, it will say goodbye and it will get out of the
application, right? Now, in next video,
what I want to do, I want to organize
everything with creating some function instead of
doing things just like this. So I will see you in next
6. 06 Organize with functions: Know for organizing purposes and make your code
more readable, I want to create some functions. For example, in here, I want to say dev for creating a function, and I want to say
this is show menu, open and close parenss
after that column. No, we can just
cut all of these, control s to cut, and in here, paste it, right? So now instead of putting all
of these inside this iTu, we can just say show
menu, right? That's it. Now, what I want to
show is that how you can add some documentation
to each function. Right now, if I hover
over this, you can see, it's just saying
death, show menu, and it's not returning
anything, right? But I want to add
some documentation. For documentation, you can go here on the first line
of over function, at three, double
column, just like this. And inside it, we can say
what these function do. Display, for example,
menu Options, right? So know with that, if you
hover over show menu, you can see it will say
display menu options. So with this, you can add some documentation
to our functions. And I think that looks awesome. Next, let's just
create a function. Dev on call it view tasks, right, open and close
parentheses, and in here, a colon and no instead of using
this four loop over here, we can cut it and put it
inside this function, right? It has some error
in here because it shouldn't be indented
like this, right? So we can hold out and shift and select all of these spaces
that shouldn't be there. And no it is good. No, I here, choice two, we can save you tasks, right? And again, we can add some
documentation to this as well. Just put three double colon at left side and
right side inside it, you can see whatever you want. For example, show
all tasks, right? That's it. No, I think our
code looks more beautiful. We can just collapse
these ones. You can see. No, you can see how clean it is, and it should work like before. No, let's just run.
We can add task. For example, this one,
learn Python, right? Again, I can add another
task shopping after that, we can view tasks, and you can see learn
Python shopping. And when we say remove
task, it won't do anything. It will just say
remove task for now. But if you put four in there, it will say goodbye
and it will get out of our application. Now, we did organize our
code with some functions.
7. 07 View tasks with number: Know that we organize our
code with some functions, we want to make your
code much better. So for example, for view tasks, I don't want to just show
this line over here. I want to have a number. Each task should have a number, so we can remove it, right, so it will be easier
to work with. But we can add lots more to it. For example, in here, when
they want to view tasks, what I want first I want to
check if the task is empty, tell them it is empty. I can't show empty tasks, right? So I can say, I not tasks. It's like saying,
I tasks are empty, right? Print, no tasks. Yet, that's it. But in case of else, just like this, we
can print the tasks. So we can these ones over
here and indent them. Hold out and shift right
top to bottom, head tap. No, it is indented, right? No, if you run, right now, we don't
have any tasks. If you put two in
the head inter, you can see it
says no tasks yet. But if we add
tasks, for example, learning Python, and after
that view the tasks, you can see it show the tasks. But next thing
that I want to do, I want to change the showing of tasks with some numbers.
How we can do that. For doing that, I
can add a counter. I will call it number. Number is equal to one, right? We want to start from one. Of course, you can
start from zero, but people don't like when
something starts from zero, it will confuse them. So let's just start from one. After that, we can use this
number, and at the end, we need to increase this number by one plus equal to one, right? And now we can use this
number instead of this. We can add a curly
bracket in there. I can say number
right after that, a dot, and that's it. This should work, right? We don't need this space, so let's get rid of that. We are using number correctly. Now with that done, let's
just go here, put four Inder, so get out of the application, save and run again. And in here, let's
just add some tasks. For example, learning, Python, after another task,
let's just say shopping. After that, another tasks, let's just say paying taxes. Now, if you put two in there
head inter, let's just see. It says, You tasks is
one learning Python, two, shopping,
three, paying taxes. If you want to see
everything, you can see it. It's clean and beautiful. When we put two in there, it will show all the tasks, and after that, it will tell you what do you want to do more.
8. 08 Save tasks with json: Know each time that we want
to get to of our application, we want to save all
the tasks, right? So user don't miss any tasks. So for doing that,
I want to use JSON. So we need to first
import the JSON. We say import JSON, right? This is a library
inside the Python. And now I want to create
a function for saving all the tasks
inside a JSON file. So in here, I will
say Dev save tasks, right, and open
and close prances after that colon and no in here. We say WD open, I will explain what it means. Open, we should specify
a name of a file, for example, tasks
dot JSON, right. And after that, we
should tell it what is the open text method
that by default, it is read or R, but we want to change it to
W to be able to write to it. And we want to say to give
this file to me as over file. We could just call it just file, but I wanted to show you that you can call this
whatever you want. So let me explain this
line one more time. With this line, we
are telling Python, open these tasks that JSON file. If it's not there,
created for us and give it to me as over
file so I can use it. So with that, we can save
things inside our file, right? That is called task dot JSON. Now, in here, we
say JSON dot dump. With this dump, we can save whatever we want inside this
file that we create in here. So in here, we say,
save the tasks. Inside over file, right?
This is exactly that. JSON, please save the
tasks inside or file. That is or task dot
JSON file, right? Now with that, let's just
see where we should use it. Wherever client want to get
out of the application, this Choice four will
happen and we say goodbye. After that, we can say
save tasks, right? It is a function or method, so it needs open and close
parenss. Now, let's just run. And now, let's just add a task, for example, learning,
Python, right. Again, another task
shopping Again, another task, paying
taxes, right. And now we can just show it. You can see it learning Python, shopping, paying taxes,
and all of that. Now if you put four in
there and head Inter, there will be a file in here, head Inter in here, and now you can see there is tasks that JSON created for us. And you can see it is a list of some tasks learning Python, shopping and paying
taxes. Cool, right? Know that you learn how you can save a list that is over tasks. Inside a JSON file. Let's see how we can load
it because right now, if we run our code and
tell it to show the tasks, no tasks yet, right? So we need to be able
to load these tasks. So let's see how we
can do that next
9. 09 Load tasks: No, we want to be able
to load the tasks. Actually, it is easy. It's not that hard.
Let's just go here. In here, we can just collapse this and create new function, we'll say Def load tasks, right? It is a function, so it needs
open and closed parents. After that, a colon
and no in here. Again, we can say, open, right? We want to open up
the tasks dot JSON. So we need to specify the
name tasks dot JSON, right? And by default, it
is in read mode, but we can put the
R there R, right? And after that, we
want to tell it to give it to us as loaded
file, for example. You can just call it file again, like I did talk
about it in here. This name is just
a variable name. You can write whatever you
want as this filename. So with this line of code, Python tried to open tasks that JSON in read mode
so we can read from it. And it will save it inside the loaded file variable
that we create in here. No, we can do things with it. We can read through
whatever it is inside it. For example, we
can say JSON, dot, load, and we can pass
the loaded file, right? That's it. What
this will give us, it will give us a
object in return, and we can save it somewhere. And it won't tell you
that it is a list, but if you open that JSON, you can see it is a list. Know what you want to do with this loaded file, we
want to return it. So we say return JSON
dot load loaded file, right? Now with that done. Let's see what it will give us. I will use a print and use the load tasks function inside that print to see
what it will give us, right? Now, let's just run, and you can see it is giving
us a list of all the tasks, and we can save it inside
these tasks, right? So instead of just printing
it, what we can do, we can save tasks
equal to this, right? Control is to save, no in here. Control seed to get out of it. Now, again, if you go here, you can see there
are some tasks. If you run, what will happen
if you show the tasks, you can see all the
tasks are there, right? If you put four and head inter, again, the tasks will be there. And if you run again, and put two in there
to view the tasks. You can see the
tasks will be there. For you to make sure
that it is working, first, I want to get out of
the application by four. After that, I want
to clear the screen. You can see everything is clear. Now I want to run
the to do list. Can see first, it says, welcome to your to do List App. After that, you want to
view the task, head two, and now you can see all the
tasks is loaded successfully.
10. 10 Try except: No, I want to show
you a problem. Let's say we delete
these tasks, right? This file, right? So
right click on it. Head delete. No,
there is no tasks. What if we run or application? You can see it is
giving us an error. It says, No such a file
or directory, right? When we use W, if
there is none there, it will create it for us. But if we use R and the file
is not there, it will crash. So how we can
prevent this crash? For preventing this crash, there is a way that is
called try and catch. It will try something. If it didn't do it successfully and it
will throw an error, we can catch that error and do whatever we
want with it, right? So for example, in here, we are trying to open a file
that doesn't exist, right? And it is giving us an error. We can just say,
try this, right, and it needs to be indented like this hold Alt
and Shift head tab, so it will be indented. Next, this troy needs a catch. That in Python, it's
called except, right? After that, we can give it that we did accept
this error, right? That is file not found error. So we can put it there file. Not found error. In this case, we can say print, for example, file
not found, right? And after that, we can return
empty list. That's it. We are returning an empty list. So what we are doing? We say, try to do this. But if you get a file
not found error, say file not found and
return empty list, right? So this way, it won't
crash over application. So if you run it again,
you can see a top. It says file not found. And know if we add some tasks, for example, learning
Python, right? After that, another
task, for example, shopping and no exit out of it, you can see the
file will be there. And if you run it again
and view the tasks, no, you can see it's
working like before. But if you somehow
delete this file, this try and accept
or try cache, it will prevent or application
to be stopped, right? That's it about try.
11. 11 Remove task: No, we want to be able to remove a task. Let's
see how we can do that. For doing that,
you know that when they use and put three, right, they put three and head inter, we need to remove that task, remove a task, right? So for doing that, like always, we want to create a function. So in here, I will
create a function, and I want to call it
the remove task, right, open and close branches
and after that, the colon and inside
this remove task. First, we want to check if
there is a task or not. So we say if not tasks tasks. If there is no task, we can say print, no task to be removed, right? And after that, we can
return out of this function, return out of this function. But if there is
some tasks, first, I want to use view tasks
function to show all the tasks. And after that,
remove the task zero. So we say task, tasks dot pop, right? And the index, let's just put zero for no. We
will change that. And when we pop
something off of a task, it will give us that
task in return, so we can save it somewhere. I will call it removed task
is equal to this popped one. After that, we can use a print. F string task that was removed is open and
closed curly bracket. And in here, we want to
show the removed task. Make sure that it
is removed task, not removed task, right? ED, it has D in here, right? Don't forget to do that. No, whenever they put
tree and head into, we want to use removed
task function, right? That's it. No licious run. No, you can see if I show
the tasks, head two, you can see we have learning Python and we have
shopping task. If you go to remove task, put three in their
handhad Tinter. Let me make this bigger
so you can see it better. You can see learning
Python and shopping, and the task that was
removed is learning Python. Now, if we show the tasks,
let's see what will happen. There is just one task there
that is the shopping task, and learning Python is deleted. Now, again, if we put three
in there, head inter, no, you can see task that
was removed is shopping. So the shopping won't
be there anymore. And if you put two in there, you can see it says
no tasks yet, right? So we successfully
remove all the tasks. If you put four in there, it will save it inside over task JSON. You can
see it is empty. No, we want to
give the option to the user to decide which
task to get removed. See where we can do that. Next.
12. 12 Get valid task number: Know what we want to do. We want to instead of saying
the pop the index zero, user should decide which task to be popped or removed, right? So for doing that, I want
to create a function to get a number from user, right? I don't want to do it in here because it will
mess up everything. That's why I want to
create a function up here. It should be up here because we want to use it
inside this function. I will call it death, Git
valid task number, right? This is our function, and after that, we
should add a column. Now, let's just
write our function. In here, I want to use a Wil true because if they
didn't put a number, we want to go back and get a
valid number again, right? So that's why I want to
use Wi true after that. I want to get a
number from input. So I will say input. I will say Enter task number
to be removed, right? And a column and a
space after that, and this input is giving
us a string or text. We need to convert it to a int. So we say int with open and close prances
around this input, and we can save it
inside the variable. For example, task number, right? It's equal to this. No, we want to check if this task
number is okay or not. So first, if we have three task, we shouldn't allow user
to put four in there, for example, and
they shouldn't be able to put zero or minus
values there, right? So first, we want to check
if task number that they choose was bigger
or equal than one, and the task number was less than or equal than the
length of tasks, right? In that case, we can return
the task number, right? But because tasks
are starting from zero and we are showing
the task from one, that's why we want to return
the task number minus one. I will explain
this. For example, in here, we have one task that
is called shopping, right? And the shopping actually in tasks list is at
index zero, right? But the user will select one, and one in index one, there is no tasks. So that's why we minus it
by one and we return it. With that done, I want to add
a s statement in here, se. And in here, I want to
tell the user to don't put a value that is minus or bigger than the length
of our tasks, right? So in here, I will use
Fring and I will say, please enter a
value between one, two, the curly bracket, and I want to put the length
of tasks there, right? So it will give us
the length of tasks. Now, with this function done, of course, we are not
finished with it yet. Let's just use it, right? In here, instead of saying
pop Zero, what we want to do. We want to use G
valid task number. This is a function, and
we want to save it inside a variable task to
be removed, right? It's equal to this get
valid task number, and we can copy it and
paste it over here, right? And let me show the task
number with the print as well. F string. I want to
say task number to be removed is task to
be removed, right? Now with that one, let's
just test that out. If we run, and in here, we want to say, first, view the tasks. So
put two in there. We have two tasks in here. So let's just remove. Put Trind I will tell us your task is learning
Python and shopping. If you put two in there, let's see what will happen.
We did put two in there. Task number to be removed
is one, two minus one. We'll give us one. Task that has been removed is
shopping, right? Now, if we view the tasks, what it will be there, you can see the learning
Python will be there, right? Let's just remove that to
see if there is any problem. So put Trinder head inter. No, it will tell us your task is just one learning Python. If you put trinder,
what will happen. It will say, please
enter a value 1-1. So you can just put one in there because there is just
one in there, right? Again, it will ask us
to put a number there. If you put in there, let's see what will happen.
There is a problem. So we need to fix that. If they put non number stuff, how we can use Troy except to fix that.
We'll do it in next.
13. 13 try except on value error: So for fixing this
error, we try except. Let's see how we can do it. I want to go to get valid
task number in here. And inside this Yop what I want to do I want to use try, right? After that, we need to indent
all of this like this, holding Alt and Shift and
drag from top to bottom, head tap, know everything
is good, right. After that, we want to
do the except, except, for example, value error because it is a value error
that we get over here. And in this case, we say print, please enter a number,
not alphabet, right? That's it. Control S to save. No, let's just start it. If we run, view the tasks
to see what task is there, learning Python and shopping. Now if we try to remove,
we can remove them. But if you put N
in their head tab, it says, Please enter a number. If you put one in there, the learning Python
will be removed, right? So with that, if
you view the tasks, you can see the
learning Python will be gone and just
the shopping is. And if you put four
in their head inter, it will be saved in task Jason. You can see the shopping is
there and there is nothing.
14. 14 Prevent empty task: One other thing, other
bug that we should fix right now is
for adding a task. Let me run to show you something in
here, to make this bigger. For example, we want
to add a task, right, and we pass an empty one, right? It will say task added. It is empty. Why it
says task added, right? If we view the tasks, you can see there
is an empty one there, and that's not good. This is a problem. So let's see how
we can fix that. For fixing that, whenever we
are adding a task in here, New task, we want to say
if this task is not known, you can append it, right? So in here, we say
new task, right? If it's not known, if there is something there. With this if task, we are checking if there
is something there. In that case, you can append it, right, like this
and say task added. But if there is nothing
there and it is empty, this else will happen, and we say print task
can't be empty, right? That's it. Control is to save. Now, let's just test that out. In here, I want to
add the task of empty and you can see it says
task can't be empty, and it won't add it there. If you view the tasks, you can see there
is just shopping. No, let's just add a task. For example, learning Python and put two in there as well. So you can see shopping, learning Python, and know
everything is working fine. If you go to task JSON, you
can see we have shopping. We have learning Python,
and know where to do list is finished.
Congratulations.