- 4 days ago
apna college
sigma 4.0 apna college
sigma 5.0 apna college
sigma 6.0 apna college
sigma 7.0 apna college
sigma 8.0 apna college
shradha apna college
sigma 3.0 apna college
sigma batch apna college
alpha batch apna college
delta batch apna college
sigma 9.0 apna college
delta 6.0 apna college
sigma apna college
apna college ai ml course
apna college ai course
apna college app
apna college alpha
apna college alpha batch
apna college app download
apna college alpha 6.0
apna college alpha course
apna college aiml
apna college alpha course free download
alpha apna college
alpha course apna college
alpha 5.0 apna college
alpha plus apna college
alpha 3.0 apna college
alpha 4.0 apna college
alpha 6.0 apna college
alpha batch apna college free download
alpha 2.0 apna college
apna college batches
apna college backend course
apna college batch login
apna college batch review
apna college batch fees
apna college beta course
apna college bca course
apna college by shradha khapra
apna college bootstrap
apna college free batch
bootstrap apna college
binary search apna college
bubble sort apna college
bca apna college
bootstrap tutorial apna college
apna college blockchain
binary tree apna college
backend apna college
best laptop apna college
binary search tree apna college
apna college courses
apna college controversy
apna college c++
apna college contact number
apna college c language
apna college courses free download
apna college courses fees
apna college c programming
apna college courses review
apna college certificate
css apna college
c++ apna college
c language apna college
c programming apna college
c++ dsa apna college
css notes apna college
css tutorial apna college
code with harry vs apna college
c++ dsa course apna college
css full course by apna college
apna college dsa sheet
apna college dsa
apna college dsa course
apna college delta
apna college delta course free download
apna college data science course
apna college delta course
apna college delta 5.0 free download
apna college delta 6.0
apna college delta 7.0
delta apna college
delta 5.0 apna college
dsa apna college
dsa sheet apna college
delta 3.0 apna college
delta 2.0 apna college
delta course apna college
delta 7.0 apna college
apna college express js
apna college express
apna college email id
apna college email
apna college enrollment number kaise nikale
apna college excel course
apna college earnings
apna college education
apna college emi
exception handling in java apna college
express js apna college
excel apna college
email of apna college
early bird offer apna college
college me apna introduction kaise de in english
apna college enquiry number
shradha apna college education
apna college founder
apna college full stack developer course
apna college free courses
apna college founder name
apna college fees
apna college free course telegram
apna college
sigma 4.0 apna college
sigma 5.0 apna college
sigma 6.0 apna college
sigma 7.0 apna college
sigma 8.0 apna college
shradha apna college
sigma 3.0 apna college
sigma batch apna college
alpha batch apna college
delta batch apna college
sigma 9.0 apna college
delta 6.0 apna college
sigma apna college
apna college ai ml course
apna college ai course
apna college app
apna college alpha
apna college alpha batch
apna college app download
apna college alpha 6.0
apna college alpha course
apna college aiml
apna college alpha course free download
alpha apna college
alpha course apna college
alpha 5.0 apna college
alpha plus apna college
alpha 3.0 apna college
alpha 4.0 apna college
alpha 6.0 apna college
alpha batch apna college free download
alpha 2.0 apna college
apna college batches
apna college backend course
apna college batch login
apna college batch review
apna college batch fees
apna college beta course
apna college bca course
apna college by shradha khapra
apna college bootstrap
apna college free batch
bootstrap apna college
binary search apna college
bubble sort apna college
bca apna college
bootstrap tutorial apna college
apna college blockchain
binary tree apna college
backend apna college
best laptop apna college
binary search tree apna college
apna college courses
apna college controversy
apna college c++
apna college contact number
apna college c language
apna college courses free download
apna college courses fees
apna college c programming
apna college courses review
apna college certificate
css apna college
c++ apna college
c language apna college
c programming apna college
c++ dsa apna college
css notes apna college
css tutorial apna college
code with harry vs apna college
c++ dsa course apna college
css full course by apna college
apna college dsa sheet
apna college dsa
apna college dsa course
apna college delta
apna college delta course free download
apna college data science course
apna college delta course
apna college delta 5.0 free download
apna college delta 6.0
apna college delta 7.0
delta apna college
delta 5.0 apna college
dsa apna college
dsa sheet apna college
delta 3.0 apna college
delta 2.0 apna college
delta course apna college
delta 7.0 apna college
apna college express js
apna college express
apna college email id
apna college email
apna college enrollment number kaise nikale
apna college excel course
apna college earnings
apna college education
apna college emi
exception handling in java apna college
express js apna college
excel apna college
email of apna college
early bird offer apna college
college me apna introduction kaise de in english
apna college enquiry number
shradha apna college education
apna college founder
apna college full stack developer course
apna college free courses
apna college founder name
apna college fees
apna college free course telegram
apna college
Category
😹
FunTranscript
00:00Next, we are going to complete a small activity which is called word search. Basically, we have the Python code file and we have this file called Sample.txt.
00:10In Sample.txt, we have some data available in this way. Inside this file, we have all the operations that have come to the file.
00:19We want to search for a particular word. Let's suppose we want to search for the word Python.
00:24We have to search for Python in this file. We have to search if Python exists or does not. And if it exists, which line number exists?
00:34This is line 1, this is line 2, this is line 3. So definitely Python exists and we want to print the line number which is going to be line number 3.
00:42So we will do this in which way? With the existing operations. Here you have to pause the lecture and you have to try to solve this activity on your own.
00:50As long as we have used operations, we have learned everything in Python, we will use all the same logic.
00:55But logically, we are going to think how we can reach the answer. If we talk about the solution, basically, we know that this is our text available.
01:03And we want to figure out in which line this particular word exists.
01:07So since we are talking about lines, we will try to process our data by line.
01:13So first of all, we will process the first line, then the second line, then the third line, then the fourth line.
01:18So we know that we need to read our file line by line. Now, do we need to write something in this file?
01:23As such, we don't need to write anything in this file. So we can figure out that we want to open our file in the read mode.
01:29In our mode, we want to open our file because we don't really need to make any changes.
01:34And then we want to read it line by line. So we will use the read line function.
01:39For now, for all the lines, we can print all the lines.
01:43So for that, we can use with open sample.txt. We will open the file in read mode as f.
01:51And what we will do? We will use read line.
01:54So we have one of our data, which we will do f.read line.
01:59And after that, we can also print the data.
02:02Now, when we read our file in this way, we will get the first line.
02:05What do we need to do for the second line?
02:07We need to repeat this process.
02:09For the third line, we need to repeat this process.
02:11So for different lines, we need to do the same work every time.
02:15Now, logically, we know that we need to repeat the same work every time.
02:20In this particular way.
02:22So, or we can help it with a loop.
02:24Because this is going to happen for a finite number of times.
02:27We know exactly when this loop will go.
02:30This loop will go until we have some data.
02:33With our read line function, there is some valid data.
02:36If this valid data will be finished.
02:38For example, if I do read line five times.
02:41If we run the code, we are going to get all of the data.
02:44Now, what do we need to do?
02:45We can convert this entire logic into a loop.
02:47We have these two operations that we have to perform.
02:50And we have to perform those operations until we have to perform.
02:53Until we have to perform these operations.
02:54Until our data is valid value.
02:56Meaning, until we have some valid value in our data.
03:00Until we have to perform these operations.
03:03Now, since we want to form a valid value in the first time.
03:05we want to create a valid value in the f.readline
03:08so we need to create this data variable
03:11and we need to initialize the data variable
03:14which will give us true in the first time
03:17so that we can read the first timeline
03:20after that, when we go over the while loop
03:23we are going to have a valid value
03:25so here we can initialize the data with a true value
03:28so what will happen, we will get all of the content line by line
03:31so this will print all of the content in our
03:33each line will be printed
03:35so this is how we access the data
03:37entire data in a file line by line
03:39where we don't know exactly how many lines are
03:41we have not defined how many lines are
03:43but with the help of the data variable
03:45we can access the data
03:47now we have to check that in any particular line
03:49what is this data, it is basically our line data
03:51in any particular line
03:53what is our python keyword
03:55python word exists or does not
03:57so as we have the line data
03:59we can check
04:01python
04:03python
04:05in data
04:07so we can use the in keyword
04:09if python is in this particular line of data
04:11then we can print
04:13word found
04:15right
04:17and we can simply break from here
04:19after this
04:21the other operations
04:23do not need to perform
04:25so let's clear this
04:27what we have done
04:29let's try to understand this
04:31first we have read the line
04:33so we have read the first line
04:35which is this line
04:37now because it is not python
04:39we simply print it
04:41so we have printed the first line
04:43in this line
04:45we have not python
04:47so we have printed it here
04:49then we have read the third line
04:51which is this line
04:53now because in the third line
04:55python word exists
04:57so in this condition
04:59we have printed the word found
05:01so third line is where we found the word
05:03and how do we track line number
05:05how do we print the line number
05:07we can take another variable
05:09let's call it line
05:11which we can initialize with one
05:13and word found
05:15here we can print
05:17word found at line
05:19and we can print the line number here
05:21and every time we can update the line number here
05:23and every time we can update our line number
05:25like we have printed data
05:27we will update the line number by one
05:29so word found at line 3
05:31basically we need to print data as such
05:33we need to print data as such
05:35we need to simply print our word
05:37that it is printed on which line
05:39so the word python is found at line 3
05:41in fact here we can replace it
05:43with the word that we are trying to find
05:45a word we can take here
05:47which is going to be python
05:49and we can also write the word variable here
05:53so python was found at line 3
05:55we can also write the word variable here
05:57so python was found at line 3
05:59we can also search any word
06:01let's suppose we want to search for this word called store
06:03so we have to change store
06:05so store is found at line 2
06:07definitely in our file
06:09this is at line 2
06:11so this is how we can perform simple
06:13or even complex operations on different files
06:15using file.io
06:17so we have three keywords
06:19we have one more keyword which is the finally keyword
06:21if in our python program we want to write
06:23some block of code
06:25which always needs to be executed
06:27irrespective of if our exception is thrown
06:29or not
06:31then we can write that code finally
06:33for example, if we want to print here
06:35that our program is finished
06:37whether it is an exception throw
06:39or not
06:41we want to print in both cases
06:43if we want to execute
06:45if we want to execute
06:47or not
06:49so this is how we do
06:51exception handling
06:53in python
06:55so this is how we do
06:57exception handling in python
06:59next concept that we are going to learn about
07:01is called exception handling
07:03first of all
07:05first of all
07:07first of all
07:09first of all
07:11first of all
07:13let's try to understand what exactly is an exception
07:15basically
07:17basically
07:19when we write
07:21python code
07:22it will show
07:23something like this
07:24now
07:25inside python code
07:26we know
07:27that if we have another error
07:29then
07:30because of the errors
07:31our program
07:33execution flow
07:35it will disrupt
07:36if there is an error here
07:37then our program
07:38will directly
07:39throw an error
07:40and we will never execute
07:42we have already seen an example
07:43now
07:44in our code
07:45there are some errors
07:46that we can predict
07:47or manage
07:49and
07:50in this way
07:51the manageable errors
07:52we can write
07:53some cases
07:54that
07:55if these errors
07:56come
07:57then
07:58the flow
07:59disturb
08:00we call exceptions
08:01and
08:03such exceptions
08:04we call exception handling
08:05and
08:06we call exception handling
08:08exception handling
08:09exception handling
08:10we basically try to
08:11predict
08:12what are the possible
08:13errors that can occur
08:14in the program
08:15and we try to manage them
08:16we try to handle
08:17these errors
08:18take a very simple example
08:19let's suppose
08:20our program
08:21we are trying to input
08:23a number
08:24which is x
08:25now
08:26we can get any valid
08:27integer value
08:28and
08:29we are trying to calculate
08:30an answer
08:31which is
08:3210 divided by x
08:33now
08:34if user input
08:35positive value
08:36plus 1
08:37plus 3
08:38or minus 1
08:39minus 2
08:40minus 3
08:41then
08:42we will have an answer
08:43but
08:44if x's value
08:45becomes equal to 0
08:46then
08:4710 divided by 0
08:48is going to be undefined
08:49this is something
08:50that our python program
08:51cannot calculate
08:52now
08:53this is a value
08:54this is a possible scenario
08:55which we can predict
08:56before
08:57that
08:58if we are taking x
08:59input from user
09:00then
09:01any value
09:02input
09:03in fact
09:04user can be
09:05not be a number
09:06at all
09:07so hello
09:08we can't convert into
09:09integer
09:10so
09:11whenever
09:12we take
09:13user input
09:14or
09:15program
09:16in
09:17program
09:18there are many
09:19scenarios
09:20and cases
09:21there are many
09:22possible things
09:23here
09:24we can predict
09:25those exceptions
09:26first keyword
09:27is the try keyword
09:28our code
09:29is the part
09:30that might cause
09:31an error
09:32and because of
09:33our execution flow
09:34disturbed
09:35we put it in our
09:36try block
09:37for example
09:38in our code
09:39if we know
09:40that if we take
09:41any input x
09:42which we can divide
09:43from 10
09:44then we can write
09:45this whole part
09:46this is how
09:47we create a try block
09:48we will convert into
09:49integer
09:50enter x
09:51and this is how
09:52we will enter x
09:53and after that
09:54we will try to calculate
09:55our answer
09:56we will divide
09:5710
09:58or any number
09:59with x
10:00so in which part
10:01there is an exception
10:02we can try
10:03this code
10:04we can try
10:05if there is no error
10:06in this code
10:07it may be
10:08it may be
10:09perfectly fine
10:10work
10:11so in this way
10:12we create the try block
10:13now we don't create the try block
10:14we don't create the try block
10:15with try
10:16we use our accept block
10:18what we will write
10:19first of our flow
10:20first of our flow
10:21we will create the try block
10:22in which there is no
10:23possible exception
10:24we will write the code
10:25and that is followed
10:27by the accept block
10:29in the accept block
10:30we will check for particular
10:31exceptions
10:32for example
10:33divide by 0
10:34a particular exception
10:35a built-in exception
10:36which in python
10:37already exists
10:38and this error
10:39is called
10:400 division error
10:41so we can check
10:43so we can check
10:44that whatever exception
10:45has come
10:46means that
10:47each exception
10:48has a particular name
10:49so we can check
10:50for already built-in
10:51exceptions
10:52or built-in errors
10:53or we can make
10:54custom exceptions
10:55now we don't need
10:56as such
10:57custom exceptions
10:58because we are trying
10:59to get an idea
11:00of what exactly
11:01exception handling is
11:02so we have already
11:03an error exists
11:04which is 0 division error
11:05so basically
11:06if that error
11:07has come
11:08we will automatically
11:09detect Python
11:10what error has come
11:11here
11:12so this error
11:13will encounter
11:14Python
11:15and as this error
11:16will come
11:17to us
11:18what code
11:19should run
11:20what work
11:21should be
11:22how to handle
11:23it
11:24that work
11:25we can create
11:26multiple
11:27accept blocks
11:28depending upon
11:29what are the
11:30different types
11:31of errors
11:32that can happen
11:33after that
11:34finally
11:35we will create
11:36our else block
11:37for example
11:38let's copy this
11:39and remove it
11:40let's suppose
11:41this is our normal code
11:42which we have
11:43written
11:44and we want to print
11:45the answer
11:46let's run the code
11:47and we want to print
11:48and we want to print
11:49the answer
11:50let's run the code
11:51in the code
11:52we have entered
11:535
11:54so 10 divided by 5
11:55we have to print
11:562.2
11:57and we want to print
11:58and we want to print
11:59the answer
12:00let's run the code
12:01in the code
12:02we have entered
12:035
12:04so 10 divided by 5
12:05we have to print
12:062.2
12:07and we want to print
12:080
12:09so what happened
12:10in that case
12:11we have directly
12:12we have thrown
12:13an error
12:14which is the 0
12:15division error
12:16which is division by 0
12:17which is in line number 2
12:18so python has
12:19automatically detect
12:20what error
12:21and after that
12:22whatever code
12:23its execution flow
12:24will stop
12:25so to prevent this
12:26we use
12:27exception handling
12:29in exception handling
12:30whatever lines
12:31can throw exception
12:32those lines
12:33we will write
12:34inside our
12:35try block
12:36except
12:37except
12:38we have the exception
12:390
12:40division
12:41error
12:42and
12:43as we can print
12:440
12:45division
12:46error
12:47and
12:48we can print
12:49here
12:50divide
12:51by 0
12:52is not allowed
12:54so we can print
12:55this statement
12:56and
12:57else
12:58block
12:59we can write
13:00our final answer
13:01so if
13:02exception
13:03throw
13:04then we will print
13:05in that case
13:06we will print
13:07let's save it
13:08and run our code
13:09if we have entered 5
13:10then normally
13:11answer will print
13:12and if we enter 0
13:13then we will print
13:14and divide by 0
13:15is not allowed
13:16so this is how we do
13:17exception handling
13:18in fact we can check
13:19multiple exceptions
13:20for example
13:21here
13:22if someone has
13:23x
13:24passed
13:25if they passed
13:26a string
13:27like hello
13:28python
13:29hello world
13:30a value
13:31which can't be converted
13:32into an integer value
13:33here we can have
13:34another exception
13:35which is
13:36value error exception
13:37here we can handle
13:38another exception
13:39with the
13:40except keyword
13:41which is
13:42value error exception
13:43here we can print
13:44f
13:45we can say
13:47invalid
13:48input
13:49let's save it
13:50and run our code
13:51let's save it
13:52and run our code
13:53now if we have
13:54hello
13:55entered here
13:56then we will print
13:57invalid input
13:58so this is how we use
13:59the different keywords
14:00to handle our exceptions
14:01now we want to see
14:02what are the other
14:03built-in exceptions
14:04in python
14:05for that we can visit
14:06this link
14:07which is for w3 schools
14:08in w3 schools
14:09we can check python
14:10built-in exceptions
14:11we have many exceptions
14:12like we can have
14:14lookup error
14:15we can have
14:16os error
14:17we can have
14:18overflow error
14:19error
14:21so
14:22in this case
14:23we can get
14:24an
14:25overflow error
14:26in that case
14:27we can get
14:28an
14:29overflow error
14:30we can get
14:31basic syntax errors
14:32if we issue
14:33in
14:34this way
14:35we have
14:36value error
14:37zero division error
14:38and a lot of
14:39different errors
14:40we can also have
14:41indentation
14:42error
14:43we can have
14:44lot of
14:45exceptions
14:46next
14:47we are going to learn
14:48to list which is called List Comprehensions. List Comprehensions already existing thing
14:54is a simple and concise way. Let's take example, for example, we have to create a list
15:00in which with 0 or with 1, let's suppose with 5, we have to store all numbers of squares
15:06in which we would have value 1, 0, 1, we will have 4, we will have no one, 16 and 25.
15:14So, by this way, we want to store all numbers of squares in a list in a form.
15:19Now, what are available tools for this work? We have the most simple available tool that is a loop
15:26that we can create a loop and create this list. How do we do? We can create a list of let's suppose numbers
15:33or in fact, let's call this list squares. We can initialize the squares like an empty list.
15:38Now, we can create a loop, i in range 0 to 6 and there we can append in the list some values.
15:47What values append? i square which is i into i append and after that we can print our squares.
15:56Let's save it and run. So, in this way, we will have all numbers of squares.
16:00Now, for this work, a simple, concise and easy way is our list comprehensions.
16:06In the list comprehensions, we have a simple fixed type format which we can use in the loops
16:12where we can do all the work in a single line. That basic format is that first of all,
16:17we define our output that we need exactly what output in our list.
16:21Now, this output we define in the form of a variable. For example, in our loop,
16:27we have a variable i used here. So, we can write here that what we need output?
16:31We need output i square which is i into i. But, where will we get this output? Basically,
16:36our looping part, we define next. So, whatever is our iterable,
16:41whatever is our collection, wherever we take this variable and calculate values,
16:45we will define it here. So, here we will write for i in our previous range 6.
16:52So, what we have written in the loop here, meaning,
16:55which iterable, which loop, which range we will take value.
16:59That is the same thing we define here. After that, we can write any condition.
17:04Now, the condition part is optional. If we want to work on a specific condition,
17:09then we define that condition here. Now, we will see an example here.
17:13But, in this example, we don't have any condition in this example.
17:16We want to know that from 0 to 5, we can get all the numbers of all the numbers.
17:20So, we can store this square in our list, whatever list it is.
17:25And, we format this whole thing with the help of square brackets.
17:28So, here we have square brackets here. So, this will be another simple way to do this.
17:33We can write square is equal to i into i which is i square for i in range 6.
17:40And, we will print our square value. And, we are going to get the same output.
17:44Let's execute the code. And, in this case,
17:47we use the same values. And, in this case, we use our list comprehension.
17:57Now, let's suppose, we want to create squares.
18:00But, only the odd numbers, we want to add the squares in our list.
18:05So, if we have numbers 0, 1, 2, 3, 4, and 5.
18:09So, only the odd values, which are 1, 3, and 5.
18:12We want to make the list of these three squares.
18:15Which is going to be 1, 9, and 25.
18:19Now, for this, generally, if we use this loop,
18:22Then, in the loop, we would have to write an if condition.
18:24That, if i modulo 2 is not equal to 0.
18:27In that case, we store the value in our list.
18:30Now, the conditions of this way,
18:32we can write it in our comprehension.
18:34How do we write it?
18:35We can directly define our condition here.
18:39That, if i modulo 2,
18:42that means, we will refer whatever variable.
18:44Here, if we store i2,
18:45then, we can refer i to this way.
18:47If i modulo 2 is not equal to 0.
18:50In that case,
18:51we store i2 in our list.
18:53So, this is how we add an additional condition at the end.
18:57Here, we can add condition.
18:59If i modulo 2 is not equal to 0.
19:03If i2 is not equal to 0.
19:04If i2 is not equal to 0.
19:06Let's save it and run.
19:07So, this time, we will store our list in our list.
19:11So, this is how we use list comprehensions.
19:14In fact, we can take another simple example.
19:17We want to write list comprehensions.
19:19We have multiple ways.
19:20We want to use directly if else.
19:22If else, it means,
19:24Let's take an example,
19:25that we already have a list that exists.
19:27We have a list in which
19:29there are some negative numbers,
19:30some positive numbers.
19:31We have 3,
19:32we have 5,
19:33we have 2,
19:34we have minus 1.
19:35We want to replace all the negative numbers in this list.
19:38So, finally,
19:40finally,
19:41we have a list.
19:42This is 003,
19:435,
19:442 and 0.
19:45In this way,
19:46our list will convert.
19:47So, how will we do this work?
19:48In our list comprehensions,
19:50we can,
19:51basically,
19:52we need to traverse this list.
19:54So, let's call this list numbers.
19:56Let's call it numbers.
19:58We want,
19:59for n in numbers,
20:01n can be our value,
20:02which is available on our numbers.
20:05In fact,
20:06we can call this value.
20:07For value in numbers,
20:09we want 0,
20:10if our value is less than 0.
20:12So,
20:13if this value is minus 2,
20:14if the value variable is minus 2,
20:15if the value variable is minus 2,
20:16in that case,
20:17we want to store 0.
20:18How will we write this?
20:19We will write this,
20:20we will write 0,
20:21if value is less than 0,
20:23but if value is greater than or equal to 0,
20:26in that case,
20:27in that case,
20:28in that case,
20:29in that case,
20:30we need 3,
20:31in that case,
20:32in that case,
20:33we need 5.
20:34So,
20:35we can write else value.
20:36Basically,
20:37value is less than 0,
20:38in that case,
20:39we want to 0,
20:40but in that case,
20:41we want the value itself.
20:42So, in that case,
20:43we can create a condition
20:44in the form of list comprehension,
20:45in that case,
20:46we can replace all negative values
20:48with 0.
20:49Let's look at the practical implementation.
20:52Let's suppose,
20:53we have a list of numbers,
20:54in which we have some negative numbers,
20:56then,
20:57we have some positive numbers,
20:59and then again,
21:00some negative numbers,
21:01and in that case,
21:02we have a lot of numbers.
21:03Now,
21:04we can overwrite the numbers here,
21:060,
21:08if the value is less than 0,
21:11else,
21:12we want the value itself,
21:13for all the values,
21:15in numbers.
21:16So, in that case,
21:17we can write conditions in this case,
21:18and if I print numbers again,
21:21all the negative values,
21:22are going to get overwritten.
21:24In that case,
21:25we will overwrite all the values,
21:26and we will convert them to 0.
21:28In that case,
21:29we want directly to our output,
21:31we have a conditional output here,
21:33but directly to our output,
21:34we can apply some particular functions.
21:37Let's suppose,
21:38we have some words,
21:39that we have stored in a list,
21:42we have hello,
21:43we have python,
21:45we have our college,
21:47so,
21:48in that case,
21:49we have a lot of words.
21:50Now,
21:51we want these words,
21:52which are in the upper case,
21:53convert,
21:54so, in that case,
21:55we have a function,
21:56which is called .upper,
21:57so,
21:58we have words of 0.upper,
22:00if we do,
22:01and if I print the value,
22:03so,
22:04we have values in the upper case,
22:05so,
22:06in that case,
22:07we get upper case values,
22:08in that case,
22:09we also have the lower function,
22:10so,
22:11what can we do,
22:12we can use this function,
22:13in our output,
22:14how do we do that,
22:15to override our words,
22:17we can use,
22:18value.upper,
22:21for,
22:22value in,
22:24words,
22:25and then we can,
22:26print the,
22:27words list,
22:28let's run this code,
22:30and in that case,
22:31our values,
22:32will override,
22:33so,
22:34string comprehensions,
22:35help us a lot,
22:36in the form of a for loop,
22:38so,
22:39if we have a lot of simple conditions,
22:40to apply our particular iterable,
22:42in this case,
22:44mostly list,
22:45so,
22:46in those cases,
22:47we can use our list comprehensions,
22:48next,
22:49we are going to learn about,
22:50the json module,
22:51now,
22:52what exactly,
22:53is json,
22:54json basically,
22:55stands for,
22:56javascript object notation,
22:59now,
23:00here,
23:01we can ask,
23:02javascript is Python,
23:03another programming language,
23:04so,
23:05exactly,
23:06studying about,
23:07javascript object notation,
23:08json,
23:09we have a format,
23:11this is one of the most popular formats,
23:13in which,
23:14web developers,
23:15they store their data,
23:16whenever,
23:17we build websites,
23:18or,
23:19in fact,
23:20when we build apps,
23:21generally,
23:22back-end development,
23:23or front-end development,
23:24when we store data,
23:25in the form of files,
23:26so,
23:27in that case,
23:28we use a lot of json format,
23:29so,
23:30a lot of standard format,
23:31in the industry,
23:32for storing data,
23:33like,
23:34we have .csv format,
23:35like,
23:36we have .txt format,
23:37like,
23:38we have json format,
23:39which can store data,
23:40now,
23:41json format,
23:42which is,
23:43which is,
23:44quite similar,
23:45with python dictionaries,
23:46like,
23:47in python,
23:48in dictionaries,
23:49we store data,
23:50in some way,
23:51in curly braces,
23:52like,
23:53we store JSON data,
23:54in JSON data,
23:55we have key value pairs,
23:57in the dictionary,
23:59we have key value pairs,
24:01like,
24:02if we want to store data,
24:03name,
24:04so,
24:05in this way,
24:06we can write name,
24:07this is json format,
24:08python dictionaries,
24:09also,
24:10like,
24:11if i want to store subjects,
24:12then,
24:13we can store them,
24:14in the form of an array,
24:15so,
24:16in the array,
24:17we can have python,
24:19we can have data science,
24:21we can have a lot of things,
24:23now,
24:24we have heard first time words,
24:26array,
24:28in JSON,
24:29the array,
24:30we call Python,
24:31we call list,
24:32or,
24:33the list of Python,
24:34in JSON format,
24:35we call arrays,
24:36in Python,
24:37the dictionary,
24:38we call object,
24:39or,
24:40in Python,
24:41the string,
24:42we call string,
24:43in JSON,
24:44we call string,
24:45so,
24:46these are strings,
24:47or,
24:48in fact,
24:49in Python,
24:50which we call none data type,
24:51in JSON,
24:52we call null,
24:54so,
24:55in JSON format,
24:56which we see values,
24:57we look familiar,
24:58but,
24:59actually,
25:00in JSON,
25:01in JavaScript,
25:02different names,
25:03now,
25:04why exactly,
25:05are we dealing with JSON format,
25:06why are we talking about it,
25:07basically,
25:08when we create our models,
25:09we can create our models,
25:10in Python,
25:11or,
25:12when we deal with our models,
25:14then,
25:15definitely,
25:16what will be our majority code,
25:17that will be related to Python,
25:18but,
25:19when we use those models,
25:20in a practical application,
25:21it will be used in a website,
25:23or in a application,
25:25for example,
25:26for example,
25:27if we make an AI chatbot,
25:28now,
25:29this AI chatbot,
25:30is a larger application,
25:31or a website,
25:32and,
25:33when we share data,
25:34then,
25:35we will be required to export our data,
25:37and we will be required to import our data,
25:39and,
25:40generally,
25:41when we export this data,
25:42or import this data,
25:43we can import it,
25:44in this particular format,
25:45so,
25:46whenever,
25:47there is a website,
25:48which is a web developer,
25:49and,
25:50let's suppose,
25:51as a data scientist,
25:52we are working in that company,
25:53and,
25:54in such cases,
25:55our development team,
25:56can give us data,
25:57in the form of JSON format,
25:58so,
25:59we should deal with JSON data,
26:00we should understand the basics,
26:02of how to deal with JSON,
26:04in Python,
26:05so,
26:06we have a special module,
26:07which is the JSON module,
26:08we can import the JSON module,
26:10as we normally import our OS module,
26:12or import another module,
26:14and,
26:15in this case,
26:16we have some special functions,
26:17which help us,
26:18to convert the JSON format,
26:19to Python code,
26:20and,
26:21Python format,
26:22to JSON,
26:23so,
26:24we can deal with JSON data,
26:26so,
26:27when we have to create JSON data,
26:28in Python objects,
26:29or Python dictionaries,
26:30or strings,
26:31we have to create our JSON module,
26:32there,
26:33we use JSON module,
26:34now,
26:35let's look at an example,
26:36of what does JSON data look like,
26:38let's create a file,
26:39which is data.json,
26:41and,
26:42in this file,
26:43we store a JSON data,
26:45now,
26:46we can store a JSON data,
26:47in the form of this object,
26:49so,
26:50basically,
26:51we create JavaScript objects,
26:52in JSON,
26:53and that is why,
26:54it is called,
26:55JavaScript object notation,
26:56and,
26:57in this file,
26:58we can store our normal strings,
26:59key value pairs,
27:00in this way,
27:01in fact,
27:02we can also create,
27:04some variables,
27:05boolean variables,
27:06like,
27:07this teacher name,
27:08can be a variable,
27:09which,
27:10we store true value,
27:11in this file,
27:12because,
27:13we write with small,
27:14or lowercase t,
27:15but,
27:16in Python,
27:17we write it with capital T,
27:18so,
27:19in JSON format,
27:20we have to write JavaScript notations,
27:21now,
27:22we don't need to learn JavaScript format,
27:23JavaScript syntax,
27:24this is just basic information,
27:26that I am sharing with you,
27:27we have to deal with majority,
27:28with Python,
27:29we have to deal with Python,
27:30our JSON module,
27:31will automatically,
27:32convert that syntax,
27:33in this teacher,
27:34we can store our address,
27:35so,
27:36in this teacher,
27:37we can store our address,
27:38so,
27:39in this address,
27:40we can create,
27:41in this object,
27:42in this object,
27:43in this object,
27:44in Python,
27:45we can create,
27:46in this dictionary,
27:48so,
27:49here,
27:50we can have the city,
27:51this is also going to be,
27:53key value,
27:54and,
27:55we can have Delhi,
27:56and,
27:57we can have the country,
27:59so,
28:00here,
28:01we can write India,
28:02so,
28:03in this way,
28:04we can have multiple values,
28:05so,
28:06we can create our subjects,
28:07and,
28:08here,
28:09we can create a list,
28:10and,
28:11here,
28:12we can create an array,
28:13so,
28:14in this array,
28:15we can have Python,
28:16we can have,
28:17data science,
28:18or,
28:19let's call it,
28:20AIML,
28:21so,
28:23in this way,
28:24we can have multiple values,
28:25so,
28:26this is some basic JSON data,
28:27that we have,
28:28now,
28:29how do we deal with Python,
28:30this JSON data,
28:31we can deal with Python,
28:32on this,
28:33what operations can perform,
28:34before we perform,
28:35we have to import the JSON module,
28:36and,
28:37in JSON module,
28:38we give multiple functions,
28:39there are two important functions,
28:41loads,
28:42and dumps,
28:43in this last,
28:44S,
28:45this is not for plural value,
28:47this S,
28:48basically,
28:49stands for string,
28:50when,
28:51let's suppose,
28:52our data,
28:53we represent it in JSON string,
28:55in the format,
28:56so,
28:57when we want to convert our JSON string,
28:59to Python object,
29:01we use this function,
29:03so,
29:05when we want to load JSON data,
29:07in the form of a Python object,
29:09then,
29:10we will use loads,
29:11and,
29:12when we want to dump data,
29:13dump,
29:14it means,
29:15here,
29:16when we want to convert our Python object,
29:18to a JSON string,
29:20that,
29:21we can store the JSON format,
29:22in the form of a JSON string,
29:24then,
29:25we will use dumps,
29:27let's look at an example,
29:28let's suppose,
29:29we have some JSON data,
29:30and,
29:31this JSON data string,
29:32already exists,
29:33so,
29:34we can store this JSON data,
29:35in the form of a string,
29:36for that,
29:37let's take these two parameters,
29:39let's suppose,
29:40this is a JSON string,
29:42and,
29:43since this is a string,
29:44let's try to,
29:45save this,
29:46and,
29:47print this,
29:48in the same line,
29:49so,
29:50this is a JSON format,
29:51which exists,
29:52in fact,
29:53if we print it,
29:54then,
29:55this is going to be,
29:56type string,
29:57when we store this data,
29:58in the Python file,
29:59this is only a Python file,
30:01which is stored in the file,
30:02in which the data is stored,
30:03so,
30:04we can store JSON data,
30:05in Python,
30:06in the form of a string,
30:07now,
30:08if we want to convert this data,
30:09we want to convert,
30:10so,
30:11let's also remove this comma,
30:13at the end,
30:14so,
30:15we have our JSON format,
30:16now,
30:17we have to convert it,
30:18to Python object,
30:19so,
30:20in Python object,
30:21we can use,
30:22JSON.loads,
30:23and,
30:24in loads,
30:25which is our JSON string,
30:26we pass it,
30:27so,
30:28in loads,
30:29we pass the string,
30:30our JSON data load,
30:31and it will convert into Python object,
30:33and,
30:34then,
30:35we can print our Python object,
30:36and,
30:37let's run this code,
30:38so,
30:39like Python object,
30:40means,
30:41that,
30:42this data will convert,
30:43because,
30:44we already know,
30:45what is JSON format,
30:46that is very similar to Python dictionary,
30:47so,
30:48this data will be converted,
30:49to a Python dictionary,
30:50and,
30:51in fact,
30:52we can print this data,
30:53we can print our Python object,
30:54so,
30:55we have these values,
30:56and,
30:57one change,
30:58you will notice,
30:59that,
31:00when we have JSON format,
31:01even though,
31:02it looks very similar to Python Dictionary,
31:03that is not Python format,
31:04that is not Python format,
31:05these are actually JavaScript,
31:06variables,
31:07which are JavaScript,
31:08and,
31:25but,
31:26we convert them into Python values,
31:28values have but we can convert them into python values with the dot loads function
31:33if we reverse this work then we can use dot dumps for example if i already have a python object
31:41let's create a python object here so we can have a name which is shraddha
31:51we can have this teacher name variable in which we can store the true value with capital p
31:58so this is a python object this is a python dictionary now if we want to convert this in json string
32:04here we can have the json string and here we only use json.dumps in which we will pass our python object
32:13and then we can print the type of json string and we can also print the json string let's save it
32:20and run so now we have this data in json string in the format convert and this is the data
32:26so here there was a capital t true and it converted into small true and in fact if i write none
32:32here it is going to get converted to null value so in python this which is our module
32:36automatically detects which type to which particular type to convert so when we have to deal with
32:42strings and dictionaries then we use these two functions but in majority cases we will be
32:47directly dealing with files we have this json data this can be directly in a file format
32:54so here we have to deal with files we use these two functions
32:59when we have to deal with strings it will be like this when we have to deal with files
33:03then we have singular values which is load and dump so when we want to read data from json file
33:10so for that we use load function and when we want to dump data from json file or write
33:16we want to write data from there we use dump and here we can combine the file io and json
33:22module to their concepts like let's clear this for example we have to read this json data from this file
33:28so how will we do this work we will basically open our file so we can open our file with open
33:34within this file we can pass the name of file data dot json now here this file is in the same folder
33:40where our code is in here so we are directly writing the name of the json file but if this file is in
33:46another folder then we have to write absolute path in that case we have to define our format
33:52then we want to define our format we want to just read from this file so we can open the file
33:56open the file and we can use json.loads and in this we can pass our file object in this file object
34:02all the data is in the file object so json module is going to load all the json data and convert it into
34:09python object so we have that data in python object and in fact then we can print this python object
34:14let's save it and run the code so in this way we are going to get the data we can print this python
34:21object this is going to be type dictionary let's run the code
34:26this is going to be type dictionary this is going to be type dictionary and this is all the data
34:28that we have stored here so inside this teacher we have small t valor true store here we are going to
34:36get this capital t valor true so this data is in python format we can also do it
34:41let's suppose we have some python data which we have already created we have a sample dictionary
34:46in which we have some data which has already stored we can have name so inside the name we can
34:52same data which we can write with it we can also have age so in h we can store any value we can also
35:00have some other variable let's suppose is teacher and inside this so this is basically python format
35:07which we have created a dictionary with help
35:09now we want to override this data into our file so for that we can open the file in the right mode
35:15and we can use json.dump function in json.dump
35:23first we have to pass that data which we want to dump you can call this in fact data
35:29so if we want to dump this data first we will pass that data after that we will pass our file object
35:34and in this way this data will be dumped in our file this will be overwritten by this data
35:44let's save it and run the code so as we have written this particular data for us
35:49so here we have small t valor true when we have added that thing that was capital t valor true which is the
35:55python syntax now when we are trying to dump data or when we are trying to load data we also have some
36:00additional options that we can use just for example when we dump data our data is all the json data
36:07in a single line we want to add indentation to add indentation we have an option which is indent
36:15and in indent we can give number of spaces value in the indentation we want to add four spaces
36:19indentation in our data so as we save it and if we rerun the code again
36:25so in that case we are going to get indentation in our output we can also sort our keys in the same format
36:32like now our keys are named, age, teacher they are not sorted in ascending or even in descending order
36:39so we can sort them here we can pass another option which is sort keys
36:43sort keys value we can pass as true so our keys automatically sort
36:48let's save this code come here and rerun it so this is how we use the json module in python
Be the first to comment