Shortest way to run seven if statements in Python
<p>I have an <code>if</code> statements that checks a number and then return a string value accordingly.</p>

<p><strong>code:</strong></p>

<pre><code>def get_weekday(day):
    if day in ['1', 1]:
        return 'Monday'
    elif day in ['2', 2]:
        return 'Tuesday'
    elif day in ['3', 3]:
        return 'Wednesday'
    elif day in ['4', 4]:
        return 'Thursday'
    elif day in ['5', 5]:
        return 'Friday'
    elif day in ['6', 6]:
        return 'Saturday'
    elif day in ['7', 7]:
        return 'Sunday'
    return 'Invalid day selected'
</code></pre>

<p>The question may appear subjective but I think there should be a <strong>pythonic</strong> and better and shorter and cleaner way of writing this.</p>

<p>You could use a dictionary. The <code>get</code> method of the dictionary returns the default <code>'Invalid day selected'</code> when the given <code>day</code> is not in the dictionary:</p>

<pre><code>days_in_week = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}

def func(day):
    return days_in_week.get(int(day), 'Invalid day selected')
</code></pre>

<p>The cast to integer using <code>int</code> allows the function to take both strings and integer <code>day</code> parameter.</p>

python