Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python "thinking" help
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hello, I am thinking if in python - is there a "check for color under you" command??? Ondrejoda 7 9,898 Feb-17-2018, 07:25 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020