10 Python Shortcuts You Need To Know

  Рет қаралды 289,747

Tech With Tim

2 жыл бұрын

💻 Want to jumpstart your career in tech? Click the link below to match with the best programs for you and join your first coaching session. The first 1,000 people to click the link can join their free career coaching session with a Career Karma coach today: ck.chat/tech-with-tim
Welcome back to another video! In this video, I will be showing 10 useful python shortcuts! These won't be advanced shortcuts, just some python language features to save some time and lines of code. If you use python a fair amount, then this will definitely be worth your time.
⭐️ Timestamps ⭐️
00:00 | Overview
01:38 | F Strings
03:35 | Unpacking
06:07 | Multiple Assignment
07:35 | Comprehensions
10:55 | Object Multiplication
11:53 | Ternary Conditions
13:17 | Zip Function
15:22 | *args & **kwargs
18:53 | For & While Else
21:25 | Sort By Key
24:09 | BONUS
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python: tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python
👕 Merchandise: teespring.com/stores/tech-with-tim-merch-shop
🔗 Social Medias 🔗
📸 Instagram: tech_with_tim
📱 Twitter: TechWithTimm
⭐ Discord: discord.gg/twt
📝 LinkedIn: www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: techwithtim.net
📂 GitHub: github.com/techwithtim
🔊 Podcast: anchor.fm/tech-with-tim
🎬 My KZfaq Gear 🎬
🎥 Main Camera (EOS Canon 90D): amzn.to/3cY23y9
🎥 Secondary Camera (Panasonic Lumix G7): amzn.to/3fl2iEV
📹 Main Lens (EFS 24mm f/2.8): amzn.to/2Yuol5r
🕹 Tripod: amzn.to/3hpSprv
🎤 Main Microphone (Rode NT1): amzn.to/2HrZxXc
🎤 Secondary Microphone (Synco Wireless Lapel System): amzn.to/3e07Swl
🎤 Third Microphone (Rode NTG4+): amzn.to/3oi0v8Z
☀️ Lights: amzn.to/2ApeiXr
⌨ Keyboard (Daskeyboard 4Q): amzn.to/2YpN5vm
🖱 Mouse (Logitech MX Master): amzn.to/2HsmRDN
📸 Webcam (Logitech 1080p Pro): amzn.to/2B2IXcQ
📢 Speaker (Beats Pill): amzn.to/2XYc5ef
🎧 Headphones (Bose Quiet Comfort 35): amzn.to/2MWbl3e
🌞 Lamp (BenQ E-reading Lamp): amzn.to/3e0UCr8
🌞 Secondary Lamp (BenQ Screenbar Plus): amzn.to/30Dtafi
💻 Monitor (BenQ EX2780Q): amzn.to/2HsmUPZ
💻 Monitor (LG Ultrawide 34WN750): amzn.to/3dSD7tS
🎙 Mic Boom Arm (Rode PSA 1): amzn.to/30EZw9m
🎚 Audio Interface (Focusrite Scarlet 4i4): amzn.to/2TjXsih
💸 Donations 💸
💵 One-Time Donations: www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8
💰 Patreon: www.patreon.com/techwithtim
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
⭐️ Tags ⭐️
-Tech With Tim
- Shortcuts
- Unpacking
- F Strings
- Comprehensions
- Ternary Conditions
⭐️ Hashtags ⭐️
#TechWithTim #PythonShortcuts

Пікірлер: 210
@TechWithTim
@TechWithTim 2 жыл бұрын
💻 Want to jumpstart your career in tech? Click the link below to match with the best programs for you and join your first coaching session. The first 1,000 people to click the link can join their free career coaching session with a Career Karma coach today: ck.chat/tech-with-tim
@Shalonchi
@Shalonchi 2 жыл бұрын
Something that he left out about unpacking is that you could use * to group a section of your collection. Let's grab his example tup = (1,2,3,4,5), if we would unpack it as follow a, *b, c = tup, than a = 1, b = [2,3,4], c = 5
@n49o7
@n49o7 2 жыл бұрын
You can also use this to optionally assign. For example, with a, b, *c = (1, 2), a == 1, b == 2, c == []. It's pretty ugly, though.
@yogiblak7819
@yogiblak7819 2 жыл бұрын
In your example, what would determine that b = [2,3,4]? Is is because it is the MIDDLE values and 1 and 5 are the beginning and end? How would I get b to = [3,4,5] if a, *b, c = (1,2,3,4,5)?
@evanli4272
@evanli4272 2 жыл бұрын
@@yogiblak7819 Hi I know this is late but I assume that you would instead just do a, b, *c = (1,2,3,4,5), because the star automatically divides it up into the number of divisions you need
@yogiblak7819
@yogiblak7819 2 жыл бұрын
@@evanli4272 but there are 3 variables, so does it automatically assign a to first value, c to last and be to all values in between? Whatn if there were 60 elements in the list? Would b = all 48 of the values not in position 0 and and len - 1?
@jelmermulder7276
@jelmermulder7276 Жыл бұрын
That's exactly (one of the things) what I was looking for. Thanks
@xhjb4ever
@xhjb4ever 2 жыл бұрын
“The first thing you need to know is the sponsor of this video”, haha
@shwm19
@shwm19 2 жыл бұрын
Was just about to say the same.
@stephane5346
@stephane5346 2 жыл бұрын
raid shadow legends
@sdaniels1288
@sdaniels1288 2 жыл бұрын
Smooth segue, lol!
@borat1
@borat1 2 жыл бұрын
That’s Linus levels of smooth
@vinicus508
@vinicus508 2 жыл бұрын
Hahaha so smooth. But with this high quality content I dont mind at all haha.
@Bassel-Ojjeh
@Bassel-Ojjeh 2 жыл бұрын
You are truly a legend Tim. I used to code when I was younger but lost my passion for it however now I am re-engaged to coding and your videos have been FANTASIC. Keep up the excellent quality of your teaching and videos.
@huantian
@huantian 2 жыл бұрын
Watch out when you multiply a nested list like that! All the sublists will be the same object, and changing one will change the rest
@paulah1639
@paulah1639 2 жыл бұрын
This is important. I didn’t know that. Thanks for pointing it out.
@kenny-kvibe
@kenny-kvibe 2 жыл бұрын
I was once debugging exactly this problem for one whole day, where I didn't understand what happens at: [[random()]*5]*5 The 2D list was containing the same values because at run-time the "random" call executes first (most inner brackets) and returns a value and then that value gets mutated, and that mutation (column) gets mutated further on. It basically mutated the pointer/reference that points to the first "random" value. Must be really carefull when it comes to this!
@arkie87
@arkie87 2 жыл бұрын
@@kenny-kvibe I also had to debug this problem once. Luckily for me, it was in a relatively small program, so I could track it down somewhat quickly.
@jplkid14
@jplkid14 Жыл бұрын
I ran into this and thought to myself...this sucks...lol
@fadamitanolatomiwas.miguel6626
@fadamitanolatomiwas.miguel6626 2 жыл бұрын
God bless you Tim! Thank you! From Nigeria
@leowright9554
@leowright9554 2 жыл бұрын
Long life to the nigerian fried rice, it's very tasty
@playerscience
@playerscience 2 жыл бұрын
Right!👍
@marcoreiser8836
@marcoreiser8836 2 жыл бұрын
11:25 , you probably wouldn't want to do this since it uses the original list's reference to duplicate it. If you make changes to one of the nested lists the other lists will also be changed. If you do x[0][0] = 2 for example, x is now [[2,2,3],[2,2,3],[2,2,3],...] which is most likely not the desired behavior.
@alexbadger8385
@alexbadger8385 2 жыл бұрын
Thank you for going over list comprehension. I was so confused on how to work them and their logic. You explained it so simply and beautifully
@serg472
@serg472 2 жыл бұрын
There is no name for the multiple assignment because there is no multiple assignment here, it is still a tuple unpacking: x, y = 1, 2 is the same as: x, y = (1, 2) A tuple is defined by the commas, not the brackets. Brackets are optional in tuples and used just for readability, that's why a tuple with one element has to be declared as (1,) and not (1).
@kenhaley4
@kenhaley4 Жыл бұрын
Exactly right. Too bad it's not usually explained that way.
@paulah1639
@paulah1639 2 жыл бұрын
Very helpful video. Straight to the point. I watched it several times to make sure I remember each one. I am using all of them now including the bonus itertools. Thanks a lot Tim. 👍👍
@Shepherd-ik1qw
@Shepherd-ik1qw 4 ай бұрын
EXTREMELY more helpful than I had expected !!! Thanks Tim !!
@uscjake868
@uscjake868 2 жыл бұрын
Thanks Tim! I couldn't program at all 6 months ago, and with videos like yours it really helps make coding fast and productive.
@marckiezeender
@marckiezeender 2 жыл бұрын
fun fact: the "multiple assignment" syntax is actually the same as the unpacking syntax: the right side of the equation creates a tuple that is then unpacked by the left side. for instance: x, y = a, b creates a tuple (a, b) and then unpacks it into x, y
@darrorpsk6148
@darrorpsk6148 2 жыл бұрын
thank you so much for putting the compilation list up front.
@DerMarkus1982
@DerMarkus1982 Жыл бұрын
That bit about For / Else was very insightful to me, specifically the fact that Else: ... only gets triggered if the For ... part has completed without a *break* . Thanks for this!
@matbailie3816
@matbailie3816 Жыл бұрын
Don't use this. There's a book full of reasons this is bad, not least being that even the creator regrets its inclusion in python.
@joshkristine6189
@joshkristine6189 2 жыл бұрын
“The first thing you need to know is the sponsor of this video” You got it right Tim! 🤣 You Legend! Thanks for sharing this video.
@BoredSpaceRoo
@BoredSpaceRoo 2 жыл бұрын
Who in their right mind gave this video thumbs down? It is brilliant. Thank you Tim you Python genius. I’ve learnt so much from your videos.
@adityapundir6529
@adityapundir6529 2 жыл бұрын
Thanks a lot Tim!
@jinhoyoon2497
@jinhoyoon2497 2 жыл бұрын
God this is the kind of stuff that makes me love Python. Thank you for this video! I’ve learned so much!
@JeroenStiers
@JeroenStiers 2 жыл бұрын
A very well paced and the exact level of detail for such a video. For a future version, you can maybe include the enumerate function to loop over a list while keeping track of the index (to do something every x elements - like printing a message)
@timwebster85
@timwebster85 Жыл бұрын
args and kwargs finally make sense! Thanks Tim
@muktadirulislammahi6542
@muktadirulislammahi6542 2 жыл бұрын
Though I knew much of those, It was quite helpful!!! Thanks tim!❣️
@polojuninho
@polojuninho 2 жыл бұрын
That "bonus" one were very useful for me! Thx a lot!
@emaddeve20
@emaddeve20 2 жыл бұрын
This Video is so helpful. Thank You Tim
@samuelosagie-aruya923
@samuelosagie-aruya923 2 жыл бұрын
This channel inspires me please keep up the good work.
@xwhitelight
@xwhitelight 2 жыл бұрын
Very very useful! Thank you Tim.
@erin1569
@erin1569 2 жыл бұрын
Another cool thing combining multiple assignment, * and fstring: tup = (1, 2, 3, 4, 5) a, *b, _, c = tup print(f"{a = }; {b = }; {c = }") >>> a = 1; b = [2, 3]; c = 5 _ works as a variable (usually a throwaway one).
@troeteimarsch
@troeteimarsch 2 жыл бұрын
that was eye opening, thank you!
@Gabirell
@Gabirell 2 жыл бұрын
Thank you! Good work!
@sinasmith7521
@sinasmith7521 2 жыл бұрын
your contents are really amazing man ...Thanks !
@level-onegamer8873
@level-onegamer8873 2 жыл бұрын
Thank you Tim That explain so much for me👍
@mirajalmahmud2534
@mirajalmahmud2534 2 жыл бұрын
Just started learning python, your vids help me a lot. Thx man !!!
@danbrown6698
@danbrown6698 2 жыл бұрын
This video is of extremely useful! You explain things pretty clear with great examples👍
@MaBuSt
@MaBuSt 2 жыл бұрын
for list comprehension, or any other 'tips', its very easy to get in a habbit of trying to keep everything tight and fun and pythonic, but what should guide your behavior is the following- find someone who has had to troubleshoot a problem in a python module while under extreme time pressure, ask them what 'pythonic' habits are very hard to understand when under extreme duress. One of the pythonic habbits that because very hard to trouble-shoot under time pressure is list comprehensions where people have put way too much logic into a single conditional. How much is too much? A good rule of thumb would be 'anything more than 1 conditonal'. 1 conditional is easy, why is that our limit? Because the number of times that 1 condition turns into 2, or 3, or more during refactoring... is very high.. each step/addition makes sense when it is added but when you are trying to comprehend the entire list comprehension while on the run and in a hurry, it because a huge issue. list comprehensions are nice, just keep them very short and simple. create additional list comprehensions if you need to keep conditionalizing them. Make sure that the You under time pressure, or your friend or coworker, isn't in big trouble later.
@SimonLam2024
@SimonLam2024 2 жыл бұрын
Wow, I didn't know the For/While Else and other useful stuff. Thank you!
@senaysew8231
@senaysew8231 2 жыл бұрын
Kind man! Thank U!
@h8handles
@h8handles 2 жыл бұрын
You really post a bunch it is great to see
@sleepy314
@sleepy314 2 жыл бұрын
Great. Just right speed and content.
@ShivaPrasad-hm5lk
@ShivaPrasad-hm5lk 2 жыл бұрын
Your videos are my energy packs
@MakarovAlex98
@MakarovAlex98 2 жыл бұрын
Hey, Tim, hey! Thank you very much for your videos and tutorials. You make very useful content. Please give advice on what books and resources to use to study Python. I want to learn how to create bots and applications on a python. I’d really appreciate your help
@yogiblak7819
@yogiblak7819 2 жыл бұрын
You said "Tern-er-rary". LOL! I don't know why I find that so funny, but I do. 😂 Keep up the great work! I love your videos and how fast you fly through it without all the padding like most KZfaqrs. You get in, explain it, show an example, and move on. The way it should be! Fan for life..
@unknownman5296
@unknownman5296 2 жыл бұрын
700k🎉🎉🎉🎉congratz tim
@wesleydcm
@wesleydcm 2 жыл бұрын
Very util! Thanks!
@b391i
@b391i 2 жыл бұрын
Keep going buddy 😎👍
@yuchaofan
@yuchaofan 2 жыл бұрын
great explanation of args and kwargs
@stephenaustin3026
@stephenaustin3026 2 жыл бұрын
I've been following you for quite a while, but this is the first time I recognized from your accent that you're Canadian!
@arvidd
@arvidd 2 жыл бұрын
Wut? He is!?
@dreamy-star
@dreamy-star 2 жыл бұрын
'for & while else' and last bonus is pretty nice tips!!
@ryu8777
@ryu8777 Жыл бұрын
awesome as usual. My bookmarks are full because of your great videos ))
@ingluissantana
@ingluissantana 2 жыл бұрын
great video!!! Thanks!!!!
@TheUnofficialMaker
@TheUnofficialMaker 2 жыл бұрын
Great tips, thanks.
@Diamond_Hanz
@Diamond_Hanz 2 жыл бұрын
Thanks Tim
@mightye6669
@mightye6669 2 жыл бұрын
pretty cool vid. thx mate
@peasant5983
@peasant5983 2 жыл бұрын
cool stuff, thanks Tim
@akira_asahi
@akira_asahi Жыл бұрын
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
@asaf7132
@asaf7132 2 жыл бұрын
thanks for all the videos! may I ask to make some full time episodes on useful modules in python like intertools and random and much more I don't even know 😅
@MrAdrien914
@MrAdrien914 2 жыл бұрын
I love to use kwargs for django models, makes everything so much faster to write, read and manage in case you change the model
@sanjay.s4916
@sanjay.s4916 2 жыл бұрын
wooow ! awesome tricks and thanx for the video
@kostiantynjohanlanguedoc3155
@kostiantynjohanlanguedoc3155 2 жыл бұрын
Thank You 😊 very much...
@rverm1000
@rverm1000 2 жыл бұрын
Nice I didn't know any of them except for f string
@potatoesama5789
@potatoesama5789 2 жыл бұрын
Love this
@user-jl7xw1oq5v
@user-jl7xw1oq5v 2 жыл бұрын
Great video! Really helpful
@user-gn6vj2mx4v
@user-gn6vj2mx4v 2 жыл бұрын
אחי מה אתה עושה פה?
@user-jl7xw1oq5v
@user-jl7xw1oq5v 2 жыл бұрын
@@user-gn6vj2mx4v היי! מתכנת כמוך (:
@alagappank1242
@alagappank1242 2 жыл бұрын
Good work💪💪
@Lunolux
@Lunolux 2 жыл бұрын
nice video, thx for the explaination of *args, **kwargs, for else and while else ; this is cool, first time i see that in programmation langage
@AbubakarMulla23
@AbubakarMulla23 2 жыл бұрын
Useful video bro...
@RashidKhan-je8ys
@RashidKhan-je8ys 2 жыл бұрын
Thank you
@ananthramvijayaraj4554
@ananthramvijayaraj4554 2 жыл бұрын
hey, what happened to the audio from 11:41 to 11:44
@wateryagarvideos5186
@wateryagarvideos5186 2 жыл бұрын
Nothing, he just didnt say anything.
@bunmiade9767
@bunmiade9767 2 жыл бұрын
Hello Tim. Your illustrations are very good. May I use your video for my class? I teach Python also.
@dillonbarnes232
@dillonbarnes232 2 жыл бұрын
Looks like an awesome video and super useful! *_Also, was the first comment._*
@nayanjha5375
@nayanjha5375 2 жыл бұрын
Thanking Tim
@deniskomoda6423
@deniskomoda6423 10 ай бұрын
Thanks for another amazing video! I think I had an "AHA!" moment here, and I tried looking somewhere else if someone had already asked about it, but I found out I cannot even phrase it properly to render precise results in my search. I'll try to phrase here. Regarding the *args, if you use name, say, a list with another name besides args, you can still use the * to unpack the values exactly the same way, right? Up till now, I was attached to the idea that *args you could only use * to unpack iterables using specifically the variable name args, which I decided to test, and it is not true. So, is it a convention at all to use the variable name args when * unpacking with *args? Or is *args is just a way to refer to this technique of unpacking interables with *?
@kvelez
@kvelez Жыл бұрын
Thanks for teaching us "zip".
@guitaek4100
@guitaek4100 2 жыл бұрын
Multiplying a nested list causes trouble as x[0] is x[1] evaluates to True. That means if you change one, the other is changed as well
@vijayragav4717
@vijayragav4717 2 жыл бұрын
We need some Time out with Tim!
@ananthramvijayaraj4554
@ananthramvijayaraj4554 2 жыл бұрын
so true
@mehdismaeili3743
@mehdismaeili3743 Жыл бұрын
excellent.
@IntangirVoluntaryist
@IntangirVoluntaryist Жыл бұрын
what editor are you using that runs it right out of it and puts errors on the line where they errored?
@sergiomendoza6833
@sergiomendoza6833 2 жыл бұрын
nice tricks!!
@the_ehiane
@the_ehiane 2 жыл бұрын
Damn! Bro dosent even think. He just goes with the flow.
@Darkev77
@Darkev77 2 жыл бұрын
Is there a way to unpack an iterable with an unknown number of entries? Or is it necessary to know the number of elements before hand?
@mywebuniversity
@mywebuniversity Жыл бұрын
Very good 👍
@mustafakhan5821
@mustafakhan5821 2 жыл бұрын
Pretty cool.
@amit12000
@amit12000 2 жыл бұрын
Nice video Tim
@MaBuSt
@MaBuSt 2 жыл бұрын
WRT the f-string segment of the video, another way of printing variables that is very often used in early python (and thus everywhere in legacy code) is the format, bracket string: money = '$10.00' print("We have {} left to spend".format(money)) I used to use f-strings exclusively until some of my code was getting deployed into < python 3.0 environments and causing all kinds of issues. If you know you are deploying into a brand new, all-modern python environment then there is no issue but if you are not sure then you should seriously consider format bracket strings.
@sorachii8932
@sorachii8932 2 жыл бұрын
This was awesome. I consider myself an advanced python programmer yet I found valuable information in this video. Thanks a lot👍🏾
@MrJamestreynolds
@MrJamestreynolds Жыл бұрын
I'd like to know how Tim gets the execution duration of the script at the bottom of the terminal. Is this a built in feature of vscode? Ie: "Finished in xxxxms"
@zeroxiph
@zeroxiph Жыл бұрын
What font do you use?
@hul8376
@hul8376 2 жыл бұрын
very good
@codershorts
@codershorts 2 жыл бұрын
What’s difference in itertools chain and list extend ?
@inescadete780
@inescadete780 2 жыл бұрын
*How do you move your cursor one space forward without Arrow keys?*
@iansjackson
@iansjackson 2 жыл бұрын
In the section on list comprehension, in printing out the dictionary example at the end I could not discern why the otput had the order (or seemngly lack of order) it did! Could anyone explain, I'm sure I'm missing the obvious?
@ONLYUSEmyTOILET
@ONLYUSEmyTOILET Жыл бұрын
The dict comprehension is iterating over a set of the characters in the sentence. Sets are an unordered data structure and therefore don't preserve insertion order.
@architech5940
@architech5940 Жыл бұрын
Great tutorials! However, it's hard to believe that One can start a 100k career from tutorials and 3 month boot camps.. So if you were to create a road map for individuals pursuing a dev job as a self taught person, what would this road map be? How likely am I to land a job assuming I did posses the necessary skills? What are dev interviewers expecting from the interviewee? What should I avoid in learning code so as not to waste my time. Much appreciated!
@beanbag1415
@beanbag1415 Жыл бұрын
I''m learning python in a uni class and I love tips like this to make me seem smarter lmao.
@BinxyxdGameing
@BinxyxdGameing Жыл бұрын
what software do u use to edit python
@miguelvasquez9849
@miguelvasquez9849 2 жыл бұрын
Awesome
@cotekokotrees1527
@cotekokotrees1527 10 ай бұрын
can you do ursina video
@soniablanche5672
@soniablanche5672 Жыл бұрын
There is also formatted string 'Hello, {}'.format(name)
@DrDeuteron
@DrDeuteron Жыл бұрын
12:50 is x = int(2 > 3) too clever for python?
@MuhammadAli-pe5ef
@MuhammadAli-pe5ef 2 жыл бұрын
what editor you use? kindly recommend
@sandpaperunderthetable6708
@sandpaperunderthetable6708 2 жыл бұрын
Sublime text
@ahoe
@ahoe 2 жыл бұрын
Hey Tim, quick question. A few weeks back I heard about a feature, where you can save the result of a condition and use it later in the program again. Do you know what that was called?
@jamie1304
@jamie1304 2 жыл бұрын
a variable?
@dylan-dylan-dylan
@dylan-dylan-dylan 8 ай бұрын
If the “condition” you are taking about is a local variable inside of a function, you may want to look up the idea of a “closure”.
@advancedcuber9226
@advancedcuber9226 2 жыл бұрын
hey time ...I have an idea ...might you teach us the way of making a level editor in pygame using grids ...or just tell how to make a grid in pygame ...pls ...pls its a humble request...I have been given a task and I have to complete it within 10 days ..
@archsapostle1stapostleofth738
@archsapostle1stapostleofth738 8 ай бұрын
11:29 be really careful with the nested list multiplication. It creates n copies of links to the same list, so if you change one list, you will change them all
Don’t take steroids ! 🙏🙏
00:16
Tibo InShape
Рет қаралды 24 МЛН
How to open a can? 🤪 lifehack
00:25
Mr.Clabik - Friends
Рет қаралды 13 МЛН
Surprise Gifts #couplegoals
00:21
Jay & Sharon
Рет қаралды 29 МЛН
Don’t take steroids ! 🙏🙏
00:16
Tibo InShape
Рет қаралды 24 МЛН