<jupyter_start><jupyter_text>Expressions and OperatorsNow we know how to define variables and assign values to them, but there isn't much point in defining variables unless we can do something useful with them! In this section we will see how to use those variables in expressions and as operands to simple operators. This will allow us to do useful computation for the first time in our code. Once you have read through this notebook there will be some exercises for you to complete to test your understanding of the material so far.  Algebraic Expressions and OperatorsLet us start with basic algebraic expressions - these involve the simple mathematical operators that you've been using your entire life. You can do all of the basic mathematical operations you would expect to be able to do in Python, using the notation you would expect. However there are a few standard operations that have some unusual syntax, and they are the ones we will concentrate most detail on in this section.<jupyter_code>#Addition, with the result being stored in the variable x
x = 1 + 2
print(x)
#Subtraction and multiplication work in the same way, as do brackets.
y = 20 - (3 * 6)
print (y)<jupyter_output><empty_output><jupyter_text>Notice that in both cases above, all of the input values are integers, and the output values are also of integer type. If you change any of the values to a float, you will find that the output is also a float. Python does this to avoid losing any precision in the result, as adding a float to an int will likely result in a number that has a fractional element, and the only way to guarentee being able to output that number is to make it a float. Technically what happens is Python "upcasts" the int to a float, that is it changes the int value into a float, and adds together two floats to output a float.<jupyter_code>x = 1.5 + 2
print("x = ", x)

y = 1. + 2
print("y = ", y)<jupyter_output><empty_output><jupyter_text>We can do powers (exponentiation) using the `**` notation. Again, if both inputs are integers we will get an integer back, or if one value is a float, Python will return us a float.<jupyter_code>x = 5 ** 2
print(x)<jupyter_output><empty_output><jupyter_text>Division is a little more complicated, as you can probably imagine. Most division operations will result in a fractional result, so Python assumes that the output of any division will be a float. However, there is a way to obtain only the integer part of the result, ignoring the fractional part, and Python will return an integer type. We can do this using the `//` operator.<jupyter_code># Division will return a floating point number, even if the result could be an integer
x = 7 / 2
print("x = ", x)

y = 4 / 2
print("y = ", y)


#In this case Python will do the division and just ignore the decimal part to return an integer
z = 7 // 2
print("z = ", z)<jupyter_output><empty_output><jupyter_text>The final basic operator is the modulo or remainder operator, for which we use the percentage sign `%`. This gives us the remainder after integer division. We could combine the modulo operation with the integer division operator above to reconstruct our original number if we so wished. I.e. 7 = (2 * 3) + 1Where the 3 is the result of the integer division, and the 1 is the remainder.<jupyter_code>#Remainder after dividing 7 by 2
x = 7 % 2
print("x = ", x)<jupyter_output><empty_output><jupyter_text>Boolean Expressions and OperatorsNow we move on to Boolean expressions, and these involve the simple mathematical comparison operators. Again, you can do all of the basic mathematical comparisons you would expect to be able to do in Python, and when these are evaluated you get a Boolean value in return. You can also include any algebraic expressions within you Boolean expressions. The algebraic expressions will be evaluated first, and then the Boolean expressions. See the long list of examples below:<jupyter_code>#Check if one value is greater than another
a = (3 > 2)
print("a = ", a)

b = (2 > 3)
print("b = ", b)

#Check if two values are the same
c = (3 == 3)
print("c = ", c)

#Check if two values are different
d = (3 != 2)
print("d = ", d)

#Check for less than or equal to
e = (3 <= 2)
print("e = ", e)

#Use algebraic expressions 
f = ((20-2) >= (3*5))
print("f = ", f)<jupyter_output><empty_output><jupyter_text>As before, these expressions will work with floats too. Python will also happily compare floats and integers by upcasting any integers to floats. This is important to note if you're using the equality `==` operator, or the disequality `!=` operator in particular. You can see in the example below, if we take a float and an integer that represent the same number, Python will be able to recognise them as being equal.<jupyter_code>#Check if two values are the same, where one is a float and the other an int
a = (3.0 == 3)
print("a = ", a)<jupyter_output><empty_output><jupyter_text>Moving beyond the Boolean expressions above, we also have Boolean operators in Python. These allow us to take Boolean values and do Boolean logic with them. The most common Boolean operators are `and`, `or`, and `not`. The output of a Boolean operation is a Boolean value, that is either `True` or `False`. Our Boolean operators may be operating on Boolean expressions, which in turn could contain algebraic expressions, so we have to be clear on the order of operations. Things will happen in this order:1. First the algebraic expressions are evaluated.2. Next the Boolean expressions are evaluated.3. Finally the Boolean operators are evaluated.4. We output our final Boolean value.<jupyter_code>a = True and False
print("a = ", a)

b = True or False
print("b = ", b)

c = not True
print("c = ", c)

d = (5 > 4) and (2 >= 2)
print("d = ", d)

e = ((20-2) >= (3*5)) or not (2 < 3)
print("e = ", e)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06982714359062125, 'fraction_numerical': 0.010610987506417936, 'mean_word_length': 4.456582633053221, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '35793', 'license': 'Apache-2.0', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1535, 'n_words': 912, 'path': 'Topic 1 - Python Data Types/1.2 - Expressions and Operators.ipynb', 'repo_name': 'KaiyeYang-git/Introduction-to-Computer-Science', 'size': '5843'}