Python #5: Types & Casts, Booleans

Types & Casts

So again the main 2 types of values are integers (numbers) and strings (text)

We can also invert one type into the other by using the ‘int’ or ‘str’ classes.

x = 42
print(str(x))

Output: 42

This ‘x = 42’ is an integer. We can convert it to a string by using ‘str’. I know it seems like a function but it’s not technically one, it’s a class. And either way the end result is still an integer so using the string convert in this case looks redundant, but down the line, we’re gonna have to incorporate variables into strings.

def say_age(years):
    print("You are " + years + " years old")

say_age(99)

In this code, we’re trying to concatenate the ‘years’ variable with the strings before and after it. Problem is, ‘years’ is an integer, as we have the 99 at the bottom there. Concatenation requires all the operands to be the same types, yet with ‘years’ by itself, we’re trying to force an integer into the string. That’s like me asking what’s 2+5*d; not ‘d’ as a variable but as a letter, that doesn’t make sense.

def say_age(years):
    print("You are " + str(years) + " years old")

say_age(99)

Output: You are 99 years old

So this is when we use the ‘str’ class to convert it to a string. Don’t forget, even though 99 is a number, thus an integer, if you want to place that into a string, it has to be a string to fit in. Python types are bigots, they want to stay with their own kind.

Booleans

A regular equal sign just assigns a value to a variable. A double equal sign will test if 2 values are equal.

print(42==42)
print("sam"=="sat")

Output: True  False

Obviously both 42s are the same, thus they’re true, but ‘sam’ and ‘sat’ aren’t the same, thus they’re false.

There’s little point in doing this for single values because we can just look and tell if they’re the same or not. This really becomes useful when it comes to equations.

print((8+2)*3==50-20)

Output: True

These are 2 different equations, but they have the same result, 30, so they output as true.

Keep in mind, Python follows PEMDAS too. You see I parenthesized the 8+2 then I multiplied it by 3. If I left the parentheses out, it would multiply 2*3 first then add the 8, which is 14, a different result.

On the flip side, while double equals tests if they’re the same, we can use an exclamation point and equal to test if they’re not the same.

print(3+4!=5+5)

Output: True

These 2 equations don’t have the same result, so this outputs as true since we’re testing if they’re not the same.

There are also ‘and’ & ‘or’ values in Booleans.

Say I asked you to go to the store and get me a diet Coke. ‘Diet’ and ‘Coke’, those are 2 qualities that need to be satisfied.

is_coke = True
is_diet = False

print(is_coke and is_diet)

Output: False

So you go to the store, you see a Coke, but it’s not diet, and I’m serious about my diet. If we print that regular Coke using this ‘and’ value, it’ll output as false, because it doesn’t satisfy all the qualities.

If I decide to compromise that I do want a diet soda, but I could settle for a regular Coke, we’d use the ‘or’ value

is_coke = True
is_diet = False

print(is_coke or is_diet)

Output: True

Since only one of these qualities has to be satisfied now, and one has been, this code will output as true now.

To recap, ‘and’ means all qualities have to be satisfied, ‘or’ means at least one of them does. And keep in mind that ‘True’ and ‘False’ are set values recognized by Python. If for some reason you choose to name a variable ‘true’ or ‘false’, you’ll have to capitalize the first letter when using the values ‘True’ or ‘False’ if you want Python to recognize it.

2 more variants. You can negate (or invert) a Boolean value by putting a ‘not’ in front of it.

x = not(True)
print(x)

Output: False

Lastly, instead of writing ‘True’ or ‘False’, you can use a ‘bool’ function followed by a parenthesized 0 or 1

x = bool(0)
y = bool(1)

print(x)
print(y)

Output: False  True

‘bool’ is not just a variable name in this case, it’s a built-in function. 0 will always represent false and 1 will represent true

Leave a comment