Using Classes and Objects in Python | Learning Python for Beginners | Code with Kylie #9

  Рет қаралды 35,941

Kylie Ying

Kylie Ying

3 жыл бұрын

In this video, I'm diving into the topic of classes in Python. Python is an object-oriented programming language, which means that much of the code revolves around objects. We can create new types of objects by creating classes, and we can create new instances of those classes to use in our code.
In this video, I'm using the beach as an example class. I go over how to define a class, initialize it, and then add methods to the class. In addition, I demonstrate these concepts using examples and talk about how changing an attribute in one instance of a class does not change attributes in other instances necessarily.
I also talk about class variables vs instance variables and how they differ. I talk about how to customize an initialization function and pass in parameters in order to set the attributes and such. Fun stuff!
Other good resources to check out:
docs.python.org/3/tutorial/cl...
www.w3schools.com/python/pyth...
www.learnpython.org/en/Classe...
The goal of this video is to teach you how to start using classes!! :)
Feel free to leave any questions.
Please consider subscribing if you liked this video: kzbin.info?sub_...
Thanks for watching everyone!
~~~~~~~~~~~~~~~~~~~~~~~~
Follow me on Instagram: / kylieyying
Follow me on Twitter: / kylieyying
Check out my website: www.kylieying.com

Пікірлер: 89
@yonassisay3061
@yonassisay3061 3 жыл бұрын
you are good ,Appreciate that but what is the difference b/n function and Class? and what is the d/f between (in function ) def __beach __: and def beach : ?
@KylieYYing
@KylieYYing 3 жыл бұрын
Good question. Let's use our beach example and lets say we want a script that will go through all the beaches in the world and pick out ones that might be fun to go to. Functions are just blocks of code that have a name. I can make any few lines of code into a function. Usually when I make functions, each one has a specific "task" that makes it logically separable from the rest of the code (so in my script i might have some functions like get_all_beaches which might have code that gets all the beaches in the world, then filter_beaches which might have code that filters through all of those beaches, etc.) You could technically just put everything into the script without the functions but it helps with organization. On the other hand, classes represent objects, so it's not really just "a chunk of code that we name". Instead, it's like saying "hey let's create something that helps us represent a beach". In my example, after getting all the beaches in the world, I would create beach objects for each beach just to represent them in a clean way. Then I can store name, location, water color, etc. as part of the object's properties instead of as a random dictionary. Does this make any sense?
@KylieYYing
@KylieYYing 3 жыл бұрын
And then for the difference between the functions within the class that are like __init__() vs something_like_this.. __init__ is a standardized python function for classes. Functions like this are actually called magic methods. When you create an instance of the object, python knows to run this function __init__(). In a similar way, if we have a method in the class __str__(), this actually would tell python how to make a string representation of the object so if we have cancun_beach = Beach('Cancun') and we do print(cancun_beach), it would print whatever the string representation is, so it actually prints cancun_beach.__str__()... Other functions are ones that you are defining and adding to the class. Take a look at these resources if my explanation isn't very good: www.geeksforgeeks.org/dunder-magic-methods-python/#:~:text=Dunder%20or%20magic%20methods%20in%20Python%20are%20the%20methods%20having,__%2C%20__repr__%20etc. www.journaldev.com/22460/python-str-repr-functions Hope this helps
@yonassisay3061
@yonassisay3061 3 жыл бұрын
Tnx i understand, again tnx alot
@vincentludwig9584
@vincentludwig9584 3 жыл бұрын
@Alijah Mathias < Scammer
@kasrasharafi5007
@kasrasharafi5007 Жыл бұрын
Awsome👌
@pythonantole9892
@pythonantole9892 Жыл бұрын
I have always struggled to understand classes especially the use of "self". This is the best explanation i have come across so far and now i have a good understanding of classes. I'm actually surprised at how easy it is, almost feel stupid that i struggled with the concept for t6oo long. Thank you! Thank you!
@paulcap9688
@paulcap9688 2 жыл бұрын
Hands down one of the clearest explanations of basic Python on YT. Thanks so much for your vides Kylie
@jjjjjkkkkk
@jjjjjkkkkk 2 жыл бұрын
Kylie, I've just watched 5 videos on classes back to back and this was by far the best explanation.
@echoic-yl5wj
@echoic-yl5wj Жыл бұрын
Watched almost all popular OOP videos and this one helped me understand it a whole lot more, thank you
@hartsgrace1205
@hartsgrace1205 Жыл бұрын
I cannot thank you enough Kylie. You are a beautiful soul. I was two runtime errors away from dropping the class because I couldn't figure out Zybooks but you broke everything done so wonderfully it just made sense. So my future bachelor's degree is all thanks too you!!!
@teacherinthailan6441
@teacherinthailan6441 3 жыл бұрын
Excellent lesson! You really are a great teacher. Thanks once again.
@BlueGuitarMusic
@BlueGuitarMusic 3 жыл бұрын
Such an awesome explanation. Thank you so much you have helped me a ton with this difficult concept. I hope you find success teaching- amidst others I have seen- your teaching style is very easy to pick up and learn from right away. VERY awesome, very thankful!
@chude_o
@chude_o 2 жыл бұрын
Great tutorial. Been struggling with classes a lot and this really helps.❤❤❤
@user-jf9rf4dh1c
@user-jf9rf4dh1c 6 ай бұрын
You motivate me to do better!!! I struggle with this simple concept!!!
@elementour
@elementour 3 жыл бұрын
so it seems as though video number 9 is when we take things up a notch. I might have to watch this one a few times as my brain currently feels like it did after i watched Inception for the first time haha
@saswatighosh3162
@saswatighosh3162 3 жыл бұрын
U are so talented ,indeed an inspiration to me!
@rajathpd3513
@rajathpd3513 3 жыл бұрын
Loved the video! It helped a lot!
@pierreissa3179
@pierreissa3179 2 жыл бұрын
your videos are very helpful. Thank you so much.
@bhaithrinley9602
@bhaithrinley9602 2 жыл бұрын
Hi ! I am a monk. I have been learning coding to help the Tibetan astronomy science to preserve. I could have done some projects on it, and I am still on progress to accomplish. but because of lack knowledge on python I am stuck ..... I watch your videos , they are helping me to build up concepts.....Thanks
@ayanupadhaya6972
@ayanupadhaya6972 3 жыл бұрын
I love you Kylie, you are so great at teaching...
@gblakney
@gblakney 2 жыл бұрын
Love your videos! So clear and thorough! is the terminal app the same thing as the command prompt? and how do you paste that path in so fast? and maybe you have a video that is a brief intro to using the terminal app to run the code I write in vscode? never mind... figured it out on my own. Still love your videos! slowly working my way through all of them. started with PyCharm Community, mostly because it has lots of buttons, then moved to Mu, which I use with my middle school club, then downloaded vscode, which seems pretty intuitive. thanks for being so patient with all of us!
@mlungisindlela6810
@mlungisindlela6810 Жыл бұрын
Thank you so much Kylie
@sushma6274
@sushma6274 3 жыл бұрын
Very nice explanation!!!
@ghostwhowalks5623
@ghostwhowalks5623 2 жыл бұрын
Awesome video! How would you save this class as a separate .py file and import it into other notebooks? I've been trying....but name-errors all over....some functions inside the class are "not defined"......
@shashidharmuniswamy2620
@shashidharmuniswamy2620 2 жыл бұрын
Thank you for the video! When a parameter is passed to a class, why should the parameter be assigned to 'self.parameter' and an attribute be created? Isn't the parameter available throughout the class and given? Is it to assign some type of memory to the parameters?
@royjuarez664
@royjuarez664 Жыл бұрын
You are a good teacher.
@omarkalom1962
@omarkalom1962 3 жыл бұрын
Thank you Kylie.
@cgmsounds
@cgmsounds 2 жыл бұрын
I love you didnt wait to get your desk to do this.
@witthayamaidee9430
@witthayamaidee9430 2 жыл бұрын
Thank you Kylie
@sebastiansuarez9883
@sebastiansuarez9883 3 жыл бұрын
I do not code and do not ever plan on it, but I watch all your vids!
@KylieYYing
@KylieYYing 3 жыл бұрын
🥺🥺🥺 thank you
@ankithrajashekar2406
@ankithrajashekar2406 2 жыл бұрын
Ahh your videos are so helpful! Coding is fun who knew?! Well I guess you did😂
@processorbot8761
@processorbot8761 3 жыл бұрын
I love your channel Kylie :-D
@t0mcat-
@t0mcat- 2 жыл бұрын
Thank you Teacher Kylie
@gitaralang
@gitaralang Жыл бұрын
thanks for the vid really helpful. Can I ask what is the purpose of the last 3 blocks starting in if __name__? thanks a lot and more power!
@magdalineidogun5935
@magdalineidogun5935 2 жыл бұрын
Heyy Kylie, how are you doing. I'm a new subscriber of your channel and love the contents you have on. I am also a new learner of Python language. I wanted to know how i can be good at writing my own codes and what steps i should take from the basics. Thank youuu
@vass8438
@vass8438 Жыл бұрын
Kylie, when I execute this code in Pycharm (windows) I get what am not doing right?
@pythonholic
@pythonholic 2 жыл бұрын
Thank you queen ♥️♥️♥️
@getoverhere4465
@getoverhere4465 3 жыл бұрын
Nice!
@wah704
@wah704 2 жыл бұрын
Hey! I'm having some issues with the last bit of code at 11:43. Everything up until "print(cape_cod_beach.parts)" works just fine, but the second last line gives me an error: AttributeError: 'Beach' object has no attribute 'add_parts'. Very helpful video! Thank you so much!
@mlungisindlela6810
@mlungisindlela6810 Жыл бұрын
You literally started my career in coding
@vincentludwig9584
@vincentludwig9584 3 жыл бұрын
12:05
@brunoavelar4003
@brunoavelar4003 2 жыл бұрын
Why do you run your program with the terminal app? Isn't it more efficient to just press "run" in the text editor?
@KylieYYing
@KylieYYing 2 жыл бұрын
Sometimes you need to control which directory you run it from and tbh I like terminal because it makes me look cool hahaha
@HDArtzy
@HDArtzy Жыл бұрын
thank you
@arpeggio1231
@arpeggio1231 Жыл бұрын
damn girl you go fast, I've gotta pause the video to catch up rofl. But you helped me heaps though very good examples you shown Thanks
@rachitmehta7738
@rachitmehta7738 3 жыл бұрын
Hi . I have one doubt: 1) What is the need for if __name__ == '__main__':
@tunamusic2314
@tunamusic2314 2 жыл бұрын
i also don't understand that. can anyone explain ???
@pauluseche2932
@pauluseche2932 3 жыл бұрын
hey can someone tell me what program she uses to code. Im new to programming and i would love to have those small windows that explain the different functions and things. thanks :). sorry for bad english
@AMoore-qx6vv
@AMoore-qx6vv 3 жыл бұрын
It’s Visual Studio Code!
@KylieYYing
@KylieYYing 3 жыл бұрын
Other good resources to check out: docs.python.org/3/tutorial/classes.html www.w3schools.com/python/python_classes.asp www.learnpython.org/en/Classes_and_Objects Subscribe for more coding lessons/tutorials!! :) Follow me on insta/twitter: @kylieyying
@opthumara1463
@opthumara1463 3 жыл бұрын
mam can you tell me what is the use of __name__ ?
@DonovanKong
@DonovanKong 3 жыл бұрын
Appreciate the lesson Kylie! #nodeskgang!
@KylieYYing
@KylieYYing 3 жыл бұрын
:’) Im getting one soon!
@forhadali5927
@forhadali5927 2 жыл бұрын
Nice vedio i am from Bangladesh
@rachitmehta7738
@rachitmehta7738 3 жыл бұрын
Great tutorials . I fpossible could you make tutorials for python app development too . Thanx . :)
@MrHorse16
@MrHorse16 3 жыл бұрын
Would the initialisation still work if we renamed __init__ to something else?
@KylieYYing
@KylieYYing 3 жыл бұрын
No, to custom initialize a class you need to define the code under the __init__ method
@MrHorse16
@MrHorse16 3 жыл бұрын
Kylie Ying Thank you! Could you also explain to me __name__ == ‘__main__’? Cheers!
@KylieYYing
@KylieYYing 3 жыл бұрын
That part is basically saying if you’re directly running the script (ie ‘python3 main.py’) then you run that part under the if. Then you might ask, when would you not run the script directly? This is the case when we actually define maybe functions or classes in a script (let’s say this is helper.py) and then import them from another file (let’s say main.py). Well in helper.py, I might want to run some stuff to help make sure I’m coding the right thing, but I don’t want these to actually show up when I try running main.py. If we don’t have that if statement then when we run main.py we actually run whatever random code is in helper.py. But if we add the if statement, then we can import helper without running the random code under the if statement. Does that make sense? Good practice for making sure your code doesn’t print out random stuff when you have a ton of imports and files.
@MrHorse16
@MrHorse16 3 жыл бұрын
Kylie Ying Thank you for the succinct reply! I guess these practices will be more helpful as my code gets more complex. Also ur channel is underrated af keep it up :-)
@KylieYYing
@KylieYYing 3 жыл бұрын
🥰🥰 thank you!!!
@teaspells9994
@teaspells9994 2 жыл бұрын
But how don't use this for real world projects?
@hamidthedev5383
@hamidthedev5383 Жыл бұрын
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler go ahead best of luck!!
@musicNmusique
@musicNmusique 3 жыл бұрын
Which text editor is this?
@KylieYYing
@KylieYYing 3 жыл бұрын
Vscode
@martaklis7096
@martaklis7096 8 ай бұрын
could You please, explain me line 27 ?(;
@Abhishek-wv9hm
@Abhishek-wv9hm 3 жыл бұрын
Hi Kylie watching all your videos since morning you are really good can you please try more projects also thanks I was lacking with some of concepts .
@KylieYYing
@KylieYYing 3 жыл бұрын
Yes, the plan is to do more projects after I’m doing with the intro python series
@KylieYYing
@KylieYYing 3 жыл бұрын
I will also be redoing some projects (hangman/tic tac toe)!! Now that I’m better at making coding videos haha
@aravindontagodi
@aravindontagodi 3 жыл бұрын
Make more videos on oop in python
@helovesdata8483
@helovesdata8483 3 жыл бұрын
True coders start on the floor anyways ...lol I just started python two weeks ago and I'm working on classes now.
@mohgawsih6958
@mohgawsih6958 3 жыл бұрын
Take it easy lady . . . some of us here are old . . . go slower please and avoid confusing names . . . otherwise . . . u r great as usual . . . thank you
@fhnine2698
@fhnine2698 5 ай бұрын
thanks appreciate, no bla bla
@aaravagarwal7806
@aaravagarwal7806 2 жыл бұрын
OBJECTS!!!!!!!!!!!!! AAAAAAARRGGHHH!
@adonis9555
@adonis9555 3 жыл бұрын
Hi world
@ig2247
@ig2247 2 жыл бұрын
Hye
@diegos2184
@diegos2184 Жыл бұрын
mamasita
@Ihavetoreturnsomevideotapes
@Ihavetoreturnsomevideotapes 3 жыл бұрын
Miss that old simp in the comments section (:
@KeithGalli
@KeithGalli 3 жыл бұрын
Sorry dude, it's no simp september. I'll be back next month!
@metehand8838
@metehand8838 2 жыл бұрын
why you cute tho ? thanks dude nice lesson
@francoisjarzabek1412
@francoisjarzabek1412 3 жыл бұрын
Ok I need to re watch the self part I understand the code and result but self nope 😅
@KylieYYing
@KylieYYing 3 жыл бұрын
Hahahahah think of yourself as a “Person” object. We are all instanced of the Person object. In order to get the person’s name you do self.name for example which refers to that person’s self.... it tells the object to look at that specific instance and look for the attribute... does that make more sense?
@YM-lm8xr
@YM-lm8xr 3 жыл бұрын
this video's learning curve is toooo steep.....
@javhaasuhochir8126
@javhaasuhochir8126 2 жыл бұрын
oh shit the class is the worst topic I struggle
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 24 МЛН
Cat story: from hate to love! 😻 #cat #cute #kitten
00:40
Stocat
Рет қаралды 13 МЛН
КАРМАНЧИК 2 СЕЗОН 5 СЕРИЯ
27:21
Inter Production
Рет қаралды 575 М.
Why? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 46 МЛН
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 104 М.
OOP Class Inheritance and Private Class Members - Python for Beginners!
16:12
Real-World Coding Interview for Software Engineering (OOP and DP)
1:14:18
A.I.  teaches itself to drive in Trackmania
15:04
Yosh
Рет қаралды 4,8 МЛН
Making Minecraft from scratch in 48 hours (NO GAME ENGINE)
16:38
Python Object Oriented Programming (OOP) - For Beginners
53:06
Tech With Tim
Рет қаралды 3,3 МЛН
AI learns to play Google Chrome Dinosaur Game || Can you beat it??
10:21
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 138 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 24 МЛН