6 Python Exercise Problems for Beginners – from CodingBat (Python Tutorial #14)
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/03/1710508919_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: 6 Python Exercise Problems for Beginners – from CodingBat (Python Tutorial #14)
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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Here’s my other video about what Python projects to work on: https://youtu.be/-1j0_SVaAJM
Also, some timestamps:
1:30: Problem 1
4:54: Problem 2
7:10: Problem 3
8:36: Problem 4
11:49: Problem 5
16:18: Problem 6
Oh btw, I recently set up a subreddit for you guys for asking each other any programming-related questions. Feel free to use it any way you’d like: https://www.reddit.com/r/csdojo/
Thanks a lot
You are so good teacher and i never been a single teacher like you in my college…I did mca but I feel coding so difficult but as I saw your python series..it feel interest in programming. Thank you so much😊
NOTE: talking about exercise one, 1:49 weekday and vacation are called parameters not arguments.
You are so incredible I wish I had time for every video of yours, btw, do you teach online or any other way to guide and motivate us?
thank you very much for introducing me to that website its very helpful <3
For the first problem couldn’t you just return “vacation”
thanks bro
can anyone solve the logic_2->make_bricks problem
Thanks for sharing
simplified solution for the first problem
def sleep_in(weekday, vacation):
return vacation or not weekday
# Problem 6
def count_evens(list):
evens = 0
for n in list:
if n%2==0:
evens += 1
return evens
print(count_evens([2, 1, 2, 3, 4]))
print(count_evens([2, 2, 0]))
print(count_evens([1, 3, 5]))
# Problem 5
def double_char(string):
result = ""
for char in string:
char += char
result += char
return result
print(double_char('The'))
print(double_char('AAbb'))
print(double_char('Hi-There'))
# Problem 4
def first_last6(list):
if list[0] == 6 or list[-1] == 6:
return True
else:
return False
print(first_last6([1, 2, 6]))
print(first_last6([6, 1, 2, 3]))
print(first_last6([13, 6, 1, 2, 3]))
# Problem 3
def hello_name(name):
return f"Hello {name}!"
print(hello_name("Bob"))
# Problem 2
def string_times(string, times):
return string*times
result = string_times("Hi", 3)
print(result)
# Problem 1 – 1:30
def sleep_in(weekday, vacation):
if not weekday or vacation:
return True
else:
return False
should_sleep = sleep_in(False, True)
print(should_sleep)
More videos like this would be great! Super helpful! Thank you!
What I say to python:
If not weekday or vacation
What python understands:
If not weekday but vacation
i loveeeee your videos, extremely helpful!!!
on the counting evens one i made the same one as you but i forgot to return so i thought that it didnt scan the list properly so i made another list that copies the first one and then i scan the evens in 2nd one and then it didnt work and then i realised i didnt need to make another list but i just needed to return the value
im having a problem it says (compile problem: '(' expected line 3)
Thanks for the video. Codingbat is currently down so i can use these exercises 🙂
Awesome! Thank you (:
Damn, I am solving the easy warm ups, my answers don't quite match the solutions but it works and DAMN it feels good with a teeny tiny coding victory FINALLY ^^
01:30
01.30
You are such a great teacher. Thank you so much for all the help so far
HELP PLEASE!! For Question 5 def double_char(str) my code is
————————————-
def double_char(str):
return str
i=0
n =["Apple"]
for x in n:
o= x[i] * 2
i=+1
print (o)
—————————————
in this it's only multipling the first index, I don't know how to include every index of my string.
Is those 3 are the same :
Count++
And
Count +=1
And
Count =count+1
tysm you are the best 🙂
u said vacation was true, what makes it true?
The code @ 6:55 doesn't have any syntax to check whether the integer is positive or not. Shouldn't the second line be "if n > 0:" ?
1:30
As someone with dyslexia and ADHD who didn't do well in maths at school and didn't do anything to do with programming before this I am proud to say that I have completed this series and learnt the syntax for Python. I never thought I could do something like this. It was difficult for me but I did it and so can you.
Keep it up such a clear explanation 👌
Thank you very much for the content! Is there something wrong with your shoulders? They are so small =O
main difference of the Python is that the frac is in fix modulation. So that's why we found this 3/2 = 1 in Py-2 language formation!
the learn to code commercial guy with the wet lips creeps me out. Back up from the damn camera