Day 2: Python Data Types#

Welcome back to our journey through Python and its applications in machine learning! In our previous session, we laid the foundation by exploring Python’s syntax, variables, and basic arithmetic operators. Today, we’re going to delve deeper into Python’s diverse world of data types, and we will also introduce logical and comparison operators. These elements are not only fundamental to programming in Python but are also crucial in performing calculations and making decisions in code - skills that are essential for data analysis and machine learning. By the end of this lesson, you’ll have a stronger grasp of how to manipulate different types of data and how to apply logical reasoning in your programming.

More about Data Types#

  • Boolean (bool): This data type is used to represent the truth values, True and False. It is often the result of comparisons or logical operations.

    is_raining = True
    is_sunny = False
    
  • None Type: None is a special data type in Python used to signify ‘nothing’ or ‘no value here’. It’s commonly used to represent the absence of a value or a default state.

    result = None
    

Logical and Comparison Operators#

  • Logical Operators:

    • and: Returns True if both operands are true.

    • or: Returns True if at least one of the operands is true.

    • not: Returns True if the operand is false.

    if is_raining and is_sunny:
        print("Look for a rainbow!")
    
  • Comparison Operators:

    • == and !=: Check for equality and inequality.

    • <, >, <=, >=: Compare numeric values for less than, greater than, less than or equal to, and greater than or equal to.

    • is and is not: Check for object identity.

    • in and not in: Check for membership in a collection (like lists, strings).

    if temperature >= 25:
        print("It's a warm day.")
    if 'a' in 'cat':
        print("The letter is in the word.")
    
  • Using Operators in Combination: Python allows for chaining comparison operators to make a compound statement that’s concise and easy to read. The n < x < m syntax is used to check if a value x is between n and m. This is essentially shorthand for (n < x) and (x < m). It’s a cleaner, more intuitive way to express a range check and is particularly useful in conditional statements and loops.

  • Use of Parentheses: Parentheses are used to group parts of expressions and control the order of evaluation. Python evaluates expressions in parentheses first, following the standard mathematical precedence rules.

  • Evaluation Order (Inside to Out, Left to Right): Python evaluates expressions from the inside of the parentheses to the outside and from left to right. This order can affect the result, especially when using logical operators.

  • Short Circuit Evaluation in Boolean Expressions:

    • In an and expression, Python stops evaluating as soon as it encounters a False value.

    • In an or expression, Python stops evaluating as soon as it encounters a True value.

Think about why: if Python encounters a() and b(), but a() evaluates to False, it doesn’t matter what value b() evaluates to – the boolean expression will always evaluate to False. Similarly, a() or b() does not need to evaluate its second term if the first term evaluates to True.

This behavior is known as short-circuit evaluation. Python evaluates a first; if a is True, it doesn’t even check b because the or condition is already satisfied. If a is False, then Python evaluates and returns the value of b, whether it’s True or False. This feature is particularly useful in scenarios where the second operand is a fallback or default option.

This means that not all parts of a boolean expression may be evaluated, which can be useful for avoiding errors (such as division by zero) or for optimizing performance.

Hands-On Project: Logic and Calculation#

Create a program that determines if a person is eligible for a specific discount at a store. The discount criteria are as follows:

  • The person must be either a senior citizen (age 65 or older) or a student.

  • The total purchase amount must be more than $50.

  • If today is Tuesday, there’s an additional 5% discount for everyone.

Write a program that takes three inputs: age, student status (True/False), total purchase amount, and whether today is Tuesday (True/False). The program should output whether the person is eligible for the discount and the final price after any additional Tuesday discount.

Solution#

def calculate_discount(age, is_student, total_purchase, is_tuesday):
    discount_eligible = (age >= 65 or is_student) and total_purchase > 50
    final_price = total_purchase

    if discount_eligible and is_tuesday:
        final_price *= 0.95  # Apply an additional 5% discount

    return discount_eligible, final_price
# Test the function
age = 70
is_student = False
total_purchase = 60
is_tuesday = True

eligible, final_price = calculate_discount(age, is_student, total_purchase, is_tuesday)
print(f"Discount Eligible: {eligible}")
print(f"Final Price: ${final_price:.2f}")
Discount Eligible: True
Final Price: $57.00
# Test the function
age = 30
is_student = False
total_purchase = 60
is_tuesday = True

eligible, final_price = calculate_discount(age, is_student, total_purchase, is_tuesday)
print(f"Discount Eligible: {eligible}")
print(f"Final Price: ${final_price:.2f}")
Discount Eligible: False
Final Price: $60.00
# Test the function
age = 25
is_student = True
total_purchase = 60
is_tuesday = True

eligible, final_price = calculate_discount(age, is_student, total_purchase, is_tuesday)
print(f"Discount Eligible: {eligible}")
print(f"Final Price: ${final_price:.2f}")
Discount Eligible: True
Final Price: $57.00
# Test the function
age = 70
is_student = True
total_purchase = 60
is_tuesday = True

eligible, final_price = calculate_discount(age, is_student, total_purchase, is_tuesday)
print(f"Discount Eligible: {eligible}")
print(f"Final Price: ${final_price:.2f}")
Discount Eligible: True
Final Price: $57.00

Practice Problem#

Now, try a similar problem to test your understanding:

Problem Statement: Write a program to determine if a customer is eligible for a membership upgrade at a gym. The criteria for the upgrade are:

  • The customer must have been a member for at least 2 years or be over the age of 60.

  • The customer must have attended at least 100 gym sessions.

  • If the customer has referred more than 3 friends, they get an automatic upgrade regardless of other criteria.

The program should take four inputs: years of membership, age, number of gym sessions attended, and number of friends referred. Output whether the customer is eligible for the membership upgrade.

Try solving this problem using the concepts of boolean logic and arithmetic operators, and compare your approach and solution to the example provided!

Further Resources#