More Conditional Expressions in python language

More Conditional Expressions in python language
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
More Conditional Expressions in python language
More Conditional Expressions in python language














All the usual arithmetic comparisons may be made, but many do not use standard mathematical symbolism, mostly for lack of proper keys on a standard keyboard


Meaning
Math Symbol
Python Symbols
Less than
< 
< 
Greater than
> 
> 
Less than or equal
<=
Greater than or equal
>=
Equals
=
==
Not equal
!=

There should not be space between the two-symbol Python substitutes.

Notice that the obvious choice for equals, a single equal sign, is not used to check for equality. An annoying second equal sign is required. This is because the single equal sign is already used for assignment in Python, so it is not available for tests.

Warning

It is a common error to use only one equal sign when you mean to test for equality, and not make an assignment!

Tests for equality do not make an assignment, and they do not require a variable on the left. Any expressions can be tested for equality or inequality (!=). They do not need to be numbered! Predict the results and try each line in the Shell:
x = 5
x
x == 5
x == 6
x
x != 6
x = 6
6 == x
6 != x
'hi' == 'h' + 'i'
'HI' != 'hi'
[1, 2] != [2, 1]
An equality check does not make an assignment. Strings are case sensitive. Order matters in a list.

Try in the Shell:
When the comparison does not make sense, an Exception is caused. 
Following up on the discussion of the inexactness of float arithmetic in String Formats for Float Precision, confirm that Python does not consider .1 + .2 to be equal to .3: Write a simple condition into the Shell to test.
Here is another example: Pay with Overtime. Given a person’s work hours for the week and regular hourly wage, calculate the total pay for the week, taking into account overtime. Hours worked over 40 are overtime, paid at 1.5 times the normal rate. This is a natural place for a function enclosing the calculation.

Read the setup for the function:

def calcWeeklyWages(total hours, hourlyWage):
    '''Return the total weekly wages for a worker working totalHours,
    with a given regular hourlyWage.  Include overtime for hours over 40.
    '''
The problem clearly indicates two cases: when no more than 40 hours are worked or when more than 40 hours are worked. In case more than 40 hours are worked, it is convenient to introduce a variable overtime hour. You are encouraged to think about a solution before going on and examining mine.

You can try running my complete example program, wages.py, also shown below. The format operation at the end of the main function uses the floating point format (String Formats for Float Precision) to show two decimal places for the cents in the answer:

def calcWeeklyWages(totalHours, hourlyWage):
    '''Return the total weekly wages for a worker working totalHours,
    with a given regular hourlyWage.  Include overtime for hours over 40.
    '''
    if totalHours <= 40:
        totalWages = hourlyWage*totalHours
    else:
        overtime = totalHours - 40
        totalWages = hourlyWage*40 + (1.5*hourlyWage)*overtime
    return totalWages

def main():
    hours = float(input('Enter hours worked: '))
    wage = float(input('Enter dollars paid per hour: '))
    total = calcWeeklyWages(hours, wage)
    print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
          .format(**locals()))

main()

Here the input was intended to be numeric, but it could be decimal so the conversion from string was via float, not int.

Below is an equivalent alternative version of the body of calcWeeklyWages, used in wages1.py. It uses just one general calculation formula and sets the parameters for the formula in the if statement. There are generally a number of ways you might solve the same problem!

    if totalHours <= 40:
        regularHours = totalHours
        overtime = 0
    else:
        overtime = totalHours - 40
        regularHours = 40
    return hourlyWage*regularHours + (1.5*hourlyWage)*overtime


































Comments