6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone so as you learn Python or any other programming language for that matter I think one thing to keep in mind is that it's really really important to practice using it as much as possible so that means building lots of things and solving lots of problems now one good website for finding problems to solve for beginners is called coding bat they have a bunch of problems here in Java and Python on topics like strings lists and logic I think it's a really good website for improving your coding skills and your problem solving skills and today we're gonna go over six problems from the Python section together now unfortunately coding bat only uses Python 2 instead of Python 3 but actually you don't have to worry too much about it because Python two and three are pretty similar so if you're more used to Python 3 pretty much the only thing you need to worry about as you go through the problems here is when you divide one integer by another integer so for example when you divide 3 by 2 by writing 3 divided by 2 the result you get in Python 2 is different from the result you get in Python 3 anyway just in case you want to learn more about the difference between Python 2 and 3 I'm gonna put this article I found about the difference between the two in the description below anyway for the problems I'm gonna cover today you don't have to worry about that at all so let's go through some problems here together so for the first problem I'm gonna go to this warm-up one section and then I'm just gonna select the first problem here sleep in now there's a bunch of text here explaining the problem but let me just explain it for you you're supposed to write a function called sleep in which takes two arguments weekday and vacation and both of them are gonna be boolean arguments so either true or false and you need to write this function sleep in so that it returns true so we sleep in if weekday is false or vacation is true so we basically sleep in if it's not a weekday or if we're on vacation so again we need to disfunction sleep in so that it returns true only when weekday is false or vacation is true so how can we do that well there are a few different ways of going about this you might say well you can just write it like English so you could write if weekday is equal to false or vacation is equal to true then we want to sleep in so we return true and else return false so this solution should work just fine and note here that when weekday is false and vacation is true as well this is still going to return true because this whole expression in the if Clause this expression right here is going to be true as well now what's cool about coding bat is once you write your solution here in the text box if you click this button right here go you can check if your solution was correct or not automatically so let me click go here and right here it says all correct now we can still simplify this code a little bit so let's do that before we go to the next problem so here in the if Clause instead of saying or vacation equals true we can actually just say or vacation and that's because vacation is already a boolean value you know it's true or false so we can put it right after the or keyword without anything else and then here instead of saying weekday equals false we can just say not weekday and that's basically for the same reason weekday is a boolean value true or false so we can just put the word not right before weekday so this whole expression we have here not weekday or vacation is pretty much equivalent to what we had earlier and note here that by putting the word not in front of weekday we're basically just flipping the boolean value so if weekday is false this expression not weekday becomes true if weekday is true that weekday becomes false okay I hope that was clear enough let me know in a comment if anything was unclear anyway let's click this button go to see if this solution is correct and it is we get all correct again now we could simplify this solution a little bit more but it's good enough for now so let's go to the next problem just click the back button in the browser and then click back button again and go to warm up - and let's find the first problem of this section string x okay so I think this problem is kind of interesting you're supposed to write a function called string x which takes two arguments the first of those arguments is going to be a string and the second argument is going to be a non-negative integer when you return from this function is gonna be the first argument repeated a certain number of times and that's gonna be the second argument so if you're given hi and - you need to return hi hi and if you're given hi and 3 you need to return hi hi hi so think about this problem for a second and see if we can solve this problem yourself okay here's my solution and to explain my solution I open up Jupiter notebook here where of course you can experiment with Python code so let's say you're given this string hi like we saw earlier let's put it in this variable call the S for now and if you wanted to get hi hi you know this string repeated twice you can just write s plus s and once you print it you get hi hi and if you wanted to get high we pretty three times you can just write s plus s plus s and then you get high high high and we can just keep going like that if you wanted to get high repeated four times you can just write s plus s plus s plus and so on but instead you can also write s times four and this gives us hahaha which is what we want so we can directly apply this thing here to the problem we saw earlier here we have a function called string x and the two arguments that we're taking are called STR and n so the only thing you actually need to return here is STR x and and that's it once you click go and you know scroll down here a little bit you see that this is all correct so this solution is correct ok let's go to the next problem click the back button and go back to the home page and then go to string 1 and let's take the first problem here as well hello name this problem is pretty simple given a string name for example Bob returned a greeting of the form hello Bob so if we get Bob we return hello Bob and if we get Alice we return hello Alex think about it for a second and I'm gonna give you my solution in a second ok here's my solution you can basically just use the thing we saw earlier where we put together or concatenate multiple strings together with the plus sign so in this function that's called hello name which takes the string argument name you might write return hello in a string plus name plus exclamation mark and then let's click go here and once we check it oops it looks like we didn't get the results correctly so let's see what's wrong here we're supposed to get hello Bob here and we are getting hello Bob here but we missed a space here it looks like and it's the same thing with all the other cases too so let's fix that by adding a space here right after the word hello and once we click go here it's ok now it's all correct all right let's go to the next problem go back to the home page and then find it lists one here and find the first problem of these problems first last 6 this problem is pretty simple as well you need to just a function called first last six which takes a list as the argument and returns true if the first element or the last element of this list is 6 so again think about it and here's my solution so the key for solving this problem is finding the first element and the last element of the given list so let's think about how we can do it in jupiter notebook again let's put this list 1 2 6 in a new variable called let's say a so a is now a list of 1 2 & 6 selecting the first element is really easy you can just write a square brackets 0 and this gives us the first element of this list 1 but what about the last element well one way to do it is like this the index of the last element is 2 because the index of the first element is 0 and the second element is 1 and the third element or the last element here is 2 and the length of this list is 3 so using those two facts you can just write this a square brackets Len parentheses a minus 1 and then once you print it you should get the last element of this list and that's because Len of a gives us the length of this list which is 3 and then minus 1 gives us 2 which is the index of the last element so let's see if that works and it does it gives us 6 but actually in Python there's a shortcut for it so to find the last element in Python you can just write print a square brackets minus 1 and this is basically equivalent to what we had earlier here so once you run this cell you get the same results 6 so let's now use this shortcut a square brackets minus 1 to find the last element of the list let's go to the problem page and using that shortcut you can write in this function first last six which takes the lists numbs if the first elements numb square bracket zero is equal to six or the last element numbs square brackets minus one equals six then return true because that was a problem we wanted to return true from our function if the first element or the last element is six and otherwise or else return false so that's it let's see if this is correct and it is all correct so just like we saw in the first problem I think we could simplify this function a little bit but it's good enough for now so let's go to the next problem go back to the home page and skip the logic sections and go to the string too let's go to the first problem double car in this problem we need to write a function called double car which takes a string as the argument and returns a new string and in this new string we need to repeat every character in the original string twice so if we have th e as the argument we need to return TT HH and ee and if we get a b b as the argument we need to return a a b b b b okay so think about it for a second maybe pause video and here's my solution to solve this problem it's gonna be useful to learn this one little trick here so let's say you're given a string let's say D which is equal to ABC what if you wanted to add a character or string to this string so what if you wanted to add a character for example F what you can do that by writing D equals D plus F so this says the new value of D is gonna be the old value of T which is ABC put together with F so this way the new value of T is gonna be ABC F but actually there's a short for it you can just write D plus equals f and this is the same thing this way we can add this character or this string F to this string D so after this once we print D we get ABC F so this is how you can add a character to a string let's see how we can apply this idea to the problem we saw earlier so let's say you're given this string let's call this string given and you're given ABC in a string and with the problem we saw earlier we'll need to produce a new string that's going to be a BB and CC so every character repeat it twice how can we do that well first of all you can just initialize a new string let's call this to return because we're gonna return this variable which is gonna be an empty string and then run a for loop over this given string given by writing for C in given as in for each character C in the string given to return plus equals C so this way we're saying for every character in the given string we're gonna add that character to return but instead of adding it just once we want to add it twice how can we do that well one way to do that would be to write it twice by writing to return plus equals C and to return plus equals C but there is a slightly cleaner way of doing it it's to write to return plus equals C times two so this way we're multiplying C by two so when C is equal to a we get double a and when C is equal to B we get B B and so on so that way we'll be able to add every character twice two to return once you run this block of code and then print to return we get what we wanted hey bebe and CeCe and just a quick note here this particular method might not be the most efficient solution depending on how your particular version of Python is implemented but you don't have to worry about that right now because probably the input is going to be pretty small anyway so let's go back to the problem and let's actually put this idea into an actual function so we have double car which is gonna take a string here we need to first initialize a new variable called let's say to return again to an empty string and then we need to run a for loop as we saw earlier by writing for C in SD R : and then to return plus equals C times two and they add then return this string to return let's see if it works and it does all correct okay let's go to the next problem go back to the home page and then find the lists to section find the first problem count evens and the problem is the following you need to write a function called count evens which takes a list of integers and you need to return the number of even integers in the given list so in this example 2 1 2 3 4 there are 3 even integers 2 2 & 4 so you need to return 3 think about it for a second maybe pause the video right here and here's my solution okay to solve this problem you need to be able to use the mod operator so let me just give you a quick overview of the mod operator just as a review so an example of a model operator would be 5 mod 3 and the mod operator gives you the remainder that you get by dividing the first number by the second number so this is asking what's the remainder that you get when you divide 5 by 3 what you get to as the remainder so that's what you get here what if you had 7 mod 3 well this gives us 1 because that's the remainder here and what if we wanted to find if a given number is an even number or not well you can do that by writing the given number for example 5 mod 2 and this way if the given number is an even number we get zero because the remainder is zero but if the given number is an odd number we get one because the remainder is 1 in that case when you divide the first number with the second number so let's use that concept to solve this problem and this function count evens which takes nums which is a list of numbers we're gonna write first initialize a variable called count to 0 we're gonna use this variable to count the number of even numbers and then we're gonna run a for loop by writing for X in Nam's so for each number which we're calling X in the given Nam's if X mod 2 equals 0 then that means X is an even number so increase count by 1 by writing count plus equals 1 and else we don't have to do anything so we don't have to write else even and after this for loop we just need to return counts so return count and we're done so what we did here is we initialize count to 0 and then we run a for loop for X in nums or for each number X in the given list and then if X is an even number or if X mod of 2 is 0 then increase the count by 1 and then at the end return counts let's see if this works and it does all correct okay that's it for the problems if you like this type of exercises there are a few resources I recommend one is of course solving more problems on coding bat because there are a lot more than the ones I cover today and another one is called project Euler at project Euler dot Nets the problems on this side are more mathematically oriented and there are language-independent to another resource I want to recommend is my own course on LinkedIn learning and Linda Calm it's called get ready for your coding interview it's supposed to be for coding interviews but I think because I use Python in this course it's gonna be good for developing your problem-solving skills and skills in Python also if you're looking for either Python book recommendations or project ideas for Python I recently published the video about that so I'm gonna put a link to that in the description just in case you're curious anyway that's it for this video thank you as always for watching my videos and I will see you guys in the next video
Info
Channel: CS Dojo
Views: 607,079
Rating: 4.940856 out of 5
Keywords: python exercise problems, python problems
Id: lx7oqZ7Nl3k
Channel Id: undefined
Length: 20min 16sec (1216 seconds)
Published: Wed Aug 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.