Python Practice - Interview Question - Fizz Buzz | Mosh
Skills:
Python for Data80%
Key Takeaways
The video demonstrates how to solve the Fizz Buzz algorithm using Python, covering the rules and implementation of the function, and providing tips for improvement and testing.
Full Transcript
one of the questions that often comes in programming interviews is the fizzbuzz algorithm you might be surprised that there are a lot of developers out there with even years of experience but they don't know how to solve this simple programming problem that's why I decided to include this in my course so let's see how this algorithm works and then you can spend 10 to 15 minutes on this exercise on your own so here we have a function fizz buzz that takes on input and depending on the input we give it it returns different results here are the rules if the input that we give it is divisible by 3 it will return the string fizz let me show you so we run this program we get Fitz if the input is divisible by 5 it will return Buzz take a look here's buzz now the third rule if the input is divisible by both 3 & 5 it will return fizzbuzz so let's pass 15 here we'll get fizzbuzz for any other numbers it will return the same input so if I pass 7 it's not divisible by 3 or 5 it will return 7 itself so spend 10 to 15 minutes on this exercise you will see my solution next all right let's see how we can solve this problem so we get the input and check to see if input is divisible by 3 then we can set a variable like result to phase now let me temporarily simplify these rules because I want to show you a really cool technique so let's imagine if the input is divisible by 3 we'll return fizz otherwise we'll return bus so LS : we set the result to buzz and finally we'll return this result variable this is one way to implement these rules but it's not the best way there is a better and cleaner way let me show you so instead of defining this result variable and then returning it we can simply return this string here and also here and then we can get rid of line six so we remove one unnecessary line of code this was one technique the other technique I want to show you is when you have an if statement and in your if lock you're returning a value you don't really need an else clause because if this condition is evaluated to false the control moves to the else clause and all we are doing here is returning a different value so this is the only possible outcome so here we can remove this else clause and simply return bus so with this technique I remove an unnecessary indentation as well I've made our code more linear okay now let's go back to our original algorithm so if the input is divisible by three we'll return fizz now we check to see if the input is divisible by five will return bus so here we don't really need an Elif because if this condition is false we'll get here anyway so we can check if input is divisible by five we will return buzz once again if this condition is false the control moves here now we check the same if the input is divisible by three and it is also divisible by five then we will return fizz bus now this expression is a little bit long I would prefer to make it more readable by putting parentheses around the first and second parts like this that is cleaner now if this condition is also false the control moves here that means our number is not divisible by three or five so we should simply return the input once again we don't need an else or another if we simply return the input now let's run this program and see if it's working properly or not so we call our function with three and we get fizz beautiful let's call it with five we get Buzz perfect what if we call it with 15 we should get fizz bus however we get fees what is going on here the reason this happened was because when we pass 15 here line two is executed obviously 15 is divisible by three so that is why we immediately return fizz to solve this problem we need to move this condition which is more specific to the top so I've selected these two lines using alt + up I move these lines up beautiful all right now let's run the program one more time so we get fizzbuzz once again let's test it with three now we get fares let's test it with five when writing functions don't assume that your function is working make sure to test it with all different kinds of values let's test it but 5 we get Buzz beautiful what if you pass 7 which is not divisible by 3 or 5 we get 7 beautiful hi guys thank you for watching this tutorial my name is Mohammad Ani and I have tons of tutorials like this for you on my channel so be sure to subscribe and also please like and share this video if you want to learn Python properly from scratch with depth I have a comprehensive python tutorial for you the link is below this video so click the link to get started thank you and have a fantastic day
Original Description
Fizz buzz is a popular Python interview question. Watch this video and practice Python.
🔥Subscribe for more Python tutorials like this: https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA?sub_confirmation=1
🔥Python for Beginners Tutorial: https://youtu.be/yE9v9rt6ziw
Python Exercises for Beginners:
https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/
Python Cheat Sheet:
https://programmingwithmosh.com/python/python-3-cheat-sheet/
Want to learn more from me? Check out my blog and courses:
http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/
https://twitter.com/moshhamedani
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Programming with Mosh · Programming with Mosh · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
6 Visual Studio Tips to Increase Your Productivity | Mosh
Programming with Mosh
Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Programming with Mosh
Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Programming with Mosh
Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Programming with Mosh
Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Programming with Mosh
Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Programming with Mosh
Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Programming with Mosh
Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Programming with Mosh
Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Programming with Mosh
Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Programming with Mosh
Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Programming with Mosh
Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Programming with Mosh
Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Programming with Mosh
Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Programming with Mosh
Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Programming with Mosh
Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Programming with Mosh
Clean Code: Learn to write clean, maintainable and robust code
Programming with Mosh
C# Events and Delegates Made Simple | Mosh
Programming with Mosh
C# Generics Tutorial: Whats and Whys | Mosh
Programming with Mosh
Debugging C# Code in Visual Studio | Mosh
Programming with Mosh
Repository Pattern with C# and Entity Framework, Done Right | Mosh
Programming with Mosh
Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Programming with Mosh
Architecture of Angular 2+ Apps
Programming with Mosh
Working with Components in Angular
Programming with Mosh
C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Programming with Mosh
Difference between Junior and Senior Developers
Programming with Mosh
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Programming with Mosh
[Pluralsight]: Become a Full-stack .NET Developer
Programming with Mosh
Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Programming with Mosh
Value Types and Reference Types in JavaScript
Programming with Mosh
Using Redux in Angular 2+ Apps | Mosh
Programming with Mosh
Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Programming with Mosh
Profile and optimize your Angular 2 apps
Programming with Mosh
Build a Real-world App with ASP.NET Core and Angular 2
Programming with Mosh
Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Programming with Mosh
Two-way Binding and ngModel in Angular 4
Programming with Mosh
Udemy Live 2017: Teaching Tech Panel
Programming with Mosh
Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Programming with Mosh
My Brand New Angular Course
Programming with Mosh
TypeScript Tutorial - TypeScript for React - Learn TypeScript
Programming with Mosh
Access Modifiers in TypeScript
Programming with Mosh
TypeScript Interfaces
Programming with Mosh
TypeScript Classes
Programming with Mosh
TypeScript Constructors
Programming with Mosh
TypeScript Properties
Programming with Mosh
Angular Tutorial for Beginners: Learn Angular & TypeScript
Programming with Mosh
AngularJS vs Angular 2 vs Angular 4 | Mosh
Programming with Mosh
Angular Material Tutorial | Mosh
Programming with Mosh
Angular Animations Tutorial | Mosh
Programming with Mosh
Firebase in Angular Applications | Mosh
Programming with Mosh
Deploying Angular Applications | Mosh
Programming with Mosh
Building Forms in Angular Apps | Mosh
Programming with Mosh
Directives in Angular Applications
Programming with Mosh
Routing and Navigation in Angular | Mosh
Programming with Mosh
Angular 4 in 40 Minutes
Programming with Mosh
[NEW COURSE] Unit Testing for C# Developers
Programming with Mosh
Unit Testing C# Code - Tutorial for Beginners
Programming with Mosh
C# Classes Tutorial | Mosh
Programming with Mosh
C# Object Initializers Tutorial
Programming with Mosh
C# Constructors Tutorial | Mosh
Programming with Mosh
More on: Python for Data
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI