Top 10 Python One Liners YOU MUST KNOW!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone I'm Patrick and in this video I have 10 python one-liners for you now a quick note a one-liner doesn't always mean it's the best solution often a longer version with better readability is the better choice but still most of them are pretty useful and it's really cool to know them also most of them are very beginner friendly so if you're already an advanced python programmer there's a good chance that you know all of them already but anyways let me know in the comments if you know all of them or if I forgot one that you really like and now without further Ado let's get started we can very easily swap two variables without the need of a third temporary variable we can simply say a b equals B and A with list comprehension we can create a list in one line instead of writing a for Loop and appending the items we can create the list like so we use brackets and then the syntax I times I for I in range 10. this will create the very same list as in the first example you can see the values are identical and we can even extend this example so if we want to use a if statement in the for Loop this would add another line and we can simply apply the if statement at the end of the list comprehension syntax and then again we have the same result with the ternary operator we can create an if else statement in one line instead of writing this out we can say VAR equals the first value if then the statement else and then the second value we can print only the elements of a collection in one line so if you print data here then this also prints the list brackets but let's say we want to print only the elements we could write a for Loop and say print I and use the end argument with a space but this also prints a space in the end and also if you print the next statement this is still in the same line so we have to add another print statement after the loop you see this soon becomes a bit techy so a simpler and better way is to say print and then a star and then the list this will unpack the elements and print only them without the space in the end we can print how many days are left in this year so we import date time at a semicolon to stay in the same line and then print date time date and then the last day of the year minus date time date today and then only the days and for today this prints 47 days are left and now I also want to show you that we could apply this in the terminal by saying python minus C and then add this statement and the very cool trick now is to add this as an alias to your system so in my case I add this Alias to my terminal configuration and then I can use the Alias and it always tells me how many days are left in the year we can reverse a list in one line by applying list slicing so we say a equals a and then from start to end with a minus one step so in this case it reverse the list and this also works for a string so if we use a string here for example level then we can reverse this and in this case it's actually the same so this is a palindrome which is a common interview question so with this one line you can check if a string is a palindrome we can easily assign multiple values to variables in one line just by defining them one after each other and they can also take different data types so here we assign a name and H in the language and if you print them you see all the values have been assigned we can convert a string with numbers to an integer list in one line so here we have a user input that is still a string if we apply user input split and print this then we have this as a list but notice that all the elements are still also a string so then we can apply the build in map function and map all the items to an integer and then we have to convert this to a list and now if we print this then we see we have an integer list we can read all the lines of a file into a list in one line so here we have a text file with names and now we can say names equals line dot strip for line in and now we open the file and we open this in read mode and now if you print this then it prints all the lines a separate item and it also removed all the white space and the last tip is for your terminal we can start an HTTP server in one line with python so here I'm in a folder where I have an index HTML file and everything else here I developed my website and now I can say python minus M HTTP dot server and this will start a server on my localhost so now I can open this and you see here it's serving my website so then I can inspect this and develop this and I can also navigate to all routes here all right and that's it let me know in the comments if I forgot an important one liner and also if you enjoyed this video drop a like and then you also might enjoy this video so I hope to see you next time bye
Info
Channel: Patrick Loeber
Views: 137,156
Rating: undefined out of 5
Keywords: Python
Id: ZW-TWrEF6qc
Channel Id: undefined
Length: 4min 52sec (292 seconds)
Published: Tue Nov 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.