Python Forum
Python "thinking" help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python "thinking" help (/thread-31230.html)



Python "thinking" help - DrManhattan71 - Nov-29-2020

I’m learning python and probably wading in too quickly without really having the basics as second nature (I mean I've done a couple of courses that quickly razz through the basics, and I even did an edX basic data science course and have done about a dozen of projectEuler but via clumsy brute force methods)

lots of examples, but I think things like list comprehension and having intuitive grasp of how the data structures work with indexing and stuff, but just picking out one example

how do I train myself to not write functions like #1 below (my solution to an easy codewars problem) but right away think about solutions #2 or #3 which just seem better and more “pythony”.

def get_count(input_str):
    num_vowels = 0
    # your code here
    vowels = ["a","e","i","o","u"]
    for vowel in vowels:
        num_vowels += input_str.count(vowel)

    return num_vowels

def get_count2(inputStr):
    return sum(1 for let in inputStr if let in "aeiouAEIOU")

def get_count3(inputStr):
    return sum(c in 'aeiou' for c in inputStr)