One of the features I love is Live Templates in PyCharm: With it, you can code super fast! They are incredibly useful for speeding up common tasks such as looping over (or generating from) a collection object.
In this brief blog, show how to use them in PyCharm (works for both Community or Professional editions)
Comprehensions and generators can help write concise yet expressive code. Consider this loop:
small_words =  for word in quip.split(" "): if(len(word.upper()) <= 4): small_words.append(word.upper()) print(small_words)
This is quite verbose.
Here is how it could be expressed with a list comprehension - which is quite readable as well:
small_words = [word.upper() for word in quip.split(" ") if len(word) <= 4] print(small_words)
Here are the parts of the comprehension:
That's easy and nice, isn't it!
You can use Live Templates - the template code segments that you can fill - in PyCharm.
Here is the list of live templates. You can access it as
PyCharm -> Preferences -> Editor -> Live Templates.
The live templates
itere are easy and simple ones to get started with.
In live action in this YouTube Video.
It's repetition made easy!
This is where the fun begins - let it be list, set or dictionaries, comprehensions are easy with PyCharm. Just start typing
comp which stands for 'comprehension'. For list comprehension, type
l, so it's
compl. For adding an if condition, add an
i, so it becomes
compdi for set and dictionary comprehensions with optional if conditions.
That's how you process lists!
That's how you deal with sets!
That's how you deal with dictionaries!
Generators create a sequence - we iterate over them one at a time. If you try to print a generator you'll get a generator object (not its content printed!). For example, if small_words is a generator, then print(small_words) will print cryptic reading one like -
<generator object <genexpr> at 0x102c1b7c8>. So convert to list, for example, before printing, like
Here is an example.
That's how you deal with generators!
main- main check
itere- for loop
compli- list comprehension
compsi- set comprehension
compdi- dictionary comprehension
Have fun playing around with comprehensions and generators in PyCharm. Check out for ~1 min PyCharm Tips & Tricks here.
Have fun coding!