1 00:00:00,380 --> 00:00:02,110 Welcome back Pythonistas. 2 00:00:02,110 --> 00:00:06,290 We've got one final section to complete, the analysis. 3 00:00:06,290 --> 00:00:10,630 I've chosen a few small items, but I encourage you to think of your own and 4 00:00:10,630 --> 00:00:12,920 add them here as well. 5 00:00:12,920 --> 00:00:17,639 The ones I came up with are the most recently published book in the database, 6 00:00:17,639 --> 00:00:23,000 the oldest book in the database and the total number of books in the database. 7 00:00:23,000 --> 00:00:25,580 Let's start with the newest book in the database. 8 00:00:25,580 --> 00:00:28,763 Let's create a variable called newest_book. 9 00:00:30,262 --> 00:00:33,759 And it should be equal 10 00:00:33,759 --> 00:00:39,784 to session.query(Book).order 11 00:00:39,784 --> 00:00:46,392 _by(Book.published_date), and 12 00:00:46,392 --> 00:00:52,424 we wanna grab the first entry there. 13 00:00:52,424 --> 00:00:58,073 And let's print it out to see what we get, newest_book, 14 00:00:58,073 --> 00:01:02,239 okay, now let's run this in the console. 15 00:01:07,384 --> 00:01:09,470 Oops, clear, there we go. 16 00:01:12,380 --> 00:01:15,950 All right, now we can finally hit 4. 17 00:01:15,950 --> 00:01:18,890 And let's see what we get. 18 00:01:18,890 --> 00:01:24,830 We got Title: Think Like a Programmer and it was published 2012-08-12. 19 00:01:24,830 --> 00:01:28,960 And I know there are newer books. 20 00:01:28,960 --> 00:01:32,813 So, the order right now is ordering by ascending so 21 00:01:32,813 --> 00:01:36,853 it's going from small to big and we need descending. 22 00:01:36,853 --> 00:01:43,619 So this one that we found here is actually the oldest book in the database. 23 00:01:43,619 --> 00:01:47,307 We can actually switch that to oldest and 24 00:01:47,307 --> 00:01:53,054 then we can do newest_book and set it equal to the same query but 25 00:01:53,054 --> 00:01:58,470 we need to make a small change, let's paste that. 26 00:01:58,470 --> 00:02:04,950 In here we need to add our desc() to say descending. 27 00:02:04,950 --> 00:02:07,438 And so I'm gonna do newest_book and 28 00:02:07,438 --> 00:02:12,094 then let's print the oldest_book cuz that should still give us that 29 00:02:12,094 --> 00:02:15,960 first book we just saw which was Think Like a Programmer. 30 00:02:17,450 --> 00:02:17,990 Save. 31 00:02:19,020 --> 00:02:21,770 Let's exit here, so we can run again. 32 00:02:23,080 --> 00:02:29,853 Number 4 and we gotta scroll up. 33 00:02:29,853 --> 00:02:32,730 Oops, too much, there we go. 34 00:02:32,730 --> 00:02:33,760 So we have one 2019. 35 00:02:33,760 --> 00:02:36,519 So it looks like November 12th 2019. 36 00:02:36,519 --> 00:02:39,377 We have Automate the Boring Stuff is the newest book, 37 00:02:39,377 --> 00:02:43,758 the most recent book in our database, and the oldest book in our database is this, 38 00:02:43,758 --> 00:02:46,510 Think Like a Programmer, from 2012, great. 39 00:02:48,790 --> 00:02:53,190 Next, let's tackle getting the total number of entries in our database. 40 00:02:53,190 --> 00:02:57,373 So let's create a new variable, and we're gonna print these out in a nicer format 41 00:02:57,373 --> 00:03:00,470 soon, so I'm gonna get rid of them for now. 42 00:03:00,470 --> 00:03:04,280 So let's do total_books. 43 00:03:04,280 --> 00:03:08,582 Set it = session.query(Book). 44 00:03:12,363 --> 00:03:15,150 And we're going to do count. 45 00:03:15,150 --> 00:03:20,008 Now remember count will give us a number that represents how many entries were 46 00:03:20,008 --> 00:03:24,714 returned and we know this will return all of the entries in our database, so 47 00:03:24,714 --> 00:03:26,900 this will give us the total count. 48 00:03:29,420 --> 00:03:37,870 So let's print(total_books), save. 49 00:03:37,870 --> 00:03:42,050 And let's close out of that, I'm gonna hit clear to so start up at the top. 50 00:03:43,590 --> 00:03:47,522 Okay, so I'm gonna run 2 first to view all books so 51 00:03:47,522 --> 00:03:50,820 we can see how many books, so we have 10. 52 00:03:50,820 --> 00:03:55,980 So our number here when we run 4 should give us the answer of 10. 53 00:03:58,320 --> 00:03:59,790 Scroll up, yep. 54 00:03:59,790 --> 00:04:01,145 10 books, perfect. 55 00:04:04,292 --> 00:04:07,410 What are some other data points that might be helpful? 56 00:04:07,410 --> 00:04:11,110 What about the number of books with Python in the title? 57 00:04:11,110 --> 00:04:11,727 Let's do that. 58 00:04:14,142 --> 00:04:23,099 Python_books=session.query(Book), and 59 00:04:23,099 --> 00:04:26,940 let's do .filter. 60 00:04:29,720 --> 00:04:34,216 And then inside of here we need to look for 61 00:04:34,216 --> 00:04:38,323 Book.title that is like where it has 62 00:04:41,482 --> 00:04:45,620 Python somewhere in the title. 63 00:04:45,620 --> 00:04:49,947 So these percent symbols just say somewhere in the title 64 00:04:49,947 --> 00:04:52,720 there's the word Python. 65 00:04:52,720 --> 00:04:55,660 And then we want the number of books. 66 00:04:55,660 --> 00:05:00,989 So again, we're gonna use .count at the end 67 00:05:00,989 --> 00:05:08,562 here to return the number of books that have Python in the title, 68 00:05:08,562 --> 00:05:14,608 then we can hit enter and print our python_books. 69 00:05:14,608 --> 00:05:22,660 Save, shut that down and then we can run 4, and we have to scroll up, 70 00:05:22,660 --> 00:05:25,390 and it looks like we have 5 books. 71 00:05:25,390 --> 00:05:27,968 And we can check that by running number 2. 72 00:05:27,968 --> 00:05:29,411 Let's see. 73 00:05:32,020 --> 00:05:38,451 So we got 1, 2, 3, 4 and 5, awesome. 74 00:05:44,172 --> 00:05:49,080 Now that we have our analysis, let's print these out in a nice format. 75 00:05:50,660 --> 00:05:55,292 So I'm gonna do a new line and 76 00:05:55,292 --> 00:06:00,310 I'm gonna do a couple stars and 77 00:06:00,310 --> 00:06:05,540 do BOOK ANALYSIS couple stars. 78 00:06:05,540 --> 00:06:09,855 Actually I'm gonna turn this just so we do it all at one into a triple string. 79 00:06:16,821 --> 00:06:20,743 Okay, so then we can use return and 80 00:06:20,743 --> 00:06:26,017 let's say Oldest Book, oops, we need to make 81 00:06:26,017 --> 00:06:31,170 this f string, there we go, oldest_book. 82 00:06:32,560 --> 00:06:36,543 And then let's do our Newest Book. 83 00:06:44,650 --> 00:06:46,132 And then let's do our Total Books. 84 00:06:56,562 --> 00:07:05,003 And our Number of Python Books. 85 00:07:08,002 --> 00:07:12,783 Save, and then I'm going to do an input here that way you can take as much time 86 00:07:12,783 --> 00:07:18,110 as you want to view the information before moving on and do it on a new line. 87 00:07:18,110 --> 00:07:25,272 Press enter to return to the main menu, save. 88 00:07:25,272 --> 00:07:27,790 Okay, now let's run it and check this out. 89 00:07:29,730 --> 00:07:35,153 So number 4, if I scroll up a bit, we have book analysis and 90 00:07:35,153 --> 00:07:39,494 we have the oldest_book, the newest_book, 91 00:07:39,494 --> 00:07:44,393 total_books and Number of Python Books, awesome. 92 00:07:44,393 --> 00:07:49,345 And if you wanna make this look even neater, you can specify which portions 93 00:07:49,345 --> 00:07:54,375 of each book you want to print out so you could do like oldest_book.title to 94 00:07:54,375 --> 00:08:00,250 just print the title if you wanna change how this is looking in the console. 95 00:08:00,250 --> 00:08:04,220 And then press enter to return to the main menu, and amazing it works. 96 00:08:05,270 --> 00:08:09,150 5 to exit, clear, clear our console. 97 00:08:11,570 --> 00:08:15,601 Spend some time thinking of data points you might be interested in, 98 00:08:15,601 --> 00:08:19,650 like the number of books published after 2015. 99 00:08:19,650 --> 00:08:23,443 This is a great way to practice your querying and analysis skills. 100 00:08:23,443 --> 00:08:28,643 Don't forget git add, 101 00:08:28,643 --> 00:08:31,505 git commit. 102 00:08:35,621 --> 00:08:37,305 And git push. 103 00:08:37,305 --> 00:08:45,148 Oops, got a typo in there. 104 00:08:50,957 --> 00:08:53,745 Review what you've learned in this course. 105 00:08:53,745 --> 00:08:57,260 Take some time to think through the code we've created together, 106 00:08:57,260 --> 00:08:59,695 then start a project of your own. 107 00:08:59,695 --> 00:09:03,835 This is the best way to test yourself and solidify your knowledge. 108 00:09:03,835 --> 00:09:09,077 How about a practice app to track your studying, you could input the minutes or 109 00:09:09,077 --> 00:09:14,220 hours studied, date and content covered as a practice project. 110 00:09:14,220 --> 00:09:16,688 I also put, in the teacher's notes below, 111 00:09:16,688 --> 00:09:20,340 some ways to improve on the work we've done so far. 112 00:09:20,340 --> 00:09:23,691 Keep practicing and improving those skills Pythonistas. 113 00:09:23,691 --> 00:09:27,590 Even a little coding every day will keep the forgetfulness away.