<jupyter_start><jupyter_text>Lesson 1: Python basic operators and data typesYour graphical calculator is a computer. Before you can use it to perform calculations, you need to learn how to talk to it. You already know that pressing the = button after inputting an expression will give you a value.The calculator only gives you a few buttons, and it is very difficult for you to come up with an expression that it cannot handle. But for that reason, it can't do very much beyond simple calculations.The computer in front of you is different. You get an entire keyboard, and full access to all the letters, numbers, symbols, and control keys. How do you use it to talk to your computer so that it can perform really powerful computations for you?  Python basic operators for integersPython can understand basic mathematical operations. It can handle the following operations:- `+` = plus  - `-` = minus  - `*` = multiply  - `/` = divide  - `//` = floor-quotient (round down after division)- `**` = exponent (power)- `%` = modulo (remainder after division)Type the following expressions line by line in the code cell below and run them to see the result.  (Remember to press Ctrl+Enter with the cell selected to actually run the code.)1. `2 + 2`2. `(3 + 4) * 2` (Python follows mathematical order of operations)3. `5 / 2` (Python returns the result of division operations as a float; see next section)4. `5 // 2` (Python can return an integer result by rounding down the result; this is called a floored quotient)5. `-5 // 2` (Floor-division is always rounded towards minus infinity)6. `3 ** 2`7. `2 % 2`8. `5 % 4`Try other expressions too, to understand how the above operators work.<jupyter_code># Try your code in this cell

2 + 2<jupyter_output><empty_output><jupyter_text>If you tried to run multiple lines at the same time in the cell above, you might notice that only one result is returned. The Jupyter Notebook cell runs the code line by line, and outputs only the final result, overriding the last result.To produce multiple lines of output, we will need the `print()` **function** (covered later in this lesson).  Python data type: floatsIn Python, numbers with decimals are known as **floats**.Type the following expressions line by line in the code cell below and run them to see the result.1. type(2)2. type(2.0)The `type()` function will be covered later in this lesson.<jupyter_code># Try your code in this cell

type(2)<jupyter_output><empty_output><jupyter_text>Python basic operators for floatsPython's operators work on floats as well.- `+` = plus  - `-` = minus  - `*` = multiply  - `/` = divide  - `**` = exponent (power)- `%` = modulo (remainder after division)Type these expressions yourself in the code cell below and run them to see the result.(Remember to press Ctrl+Enter with the cell selected to actually run the code.)1. `2 + 2.0` (Unlike with integers, in any operation involving a float, Python will cast the result as a float.)2. `(3 + 4) * 2.0` (Python follows mathematical order of operations for floats too)3. `4 / 2` (Python returns the result of division operations as a float, even if the answer can be represented as an integer.)4. `5.0 // 2` (Even floor-division returns a float.)5. `3.0 ** 2` (Even the power operator always returns a float result when either value is a float.)6. `2 % 2.0`7. `5 % 4.0`8. `5.0 / 0` (Python will raise a `ZeroDivisionError` when dividing by zero.)9. `5.0 % 0` (Python will raise a `ZeroDivisionError` with modulo zero.)<jupyter_code># Try your code in this cell<jupyter_output><empty_output><jupyter_text>VariablesIt would be troublesome if you have to keep typing in all the values in a calculation. What if you need to use constants, such as pi? Python lets you *assign* a value to a **variable**.In Python, a variable needs a name. Variable names cannot begin with a number; they must begin with a letter or underscore (`_`). Variable names can use letters, numbers, and underscore.**Invalid variable name:** 2019_lessons  **Valid variable name:** _2019lessons, lesson2019  By convention, variables begin with lowercase letters.  Assigning values to variables To view the value of a variable in the Python shell, just type its name and run the code in the cell.Run these expressions one by one in the code cell below and see the result.1. `num1 + 2`2. `num1 ** num1`3. `num2 = num1 + 2`<jupyter_code>#The next line creates the variable num1.
# Subsequent lines of code can access this value.
num1 = 2 #Do not remove this line. Type your code below this line.

num1 + 2<jupyter_output><empty_output><jupyter_text>Notice that line 3, `num2 = num1 + 2`, does not produce any output. But `num2` has been created, and a value assigned to it!Run the cell below to see:<jupyter_code># Try your code in this cell

num2<jupyter_output><empty_output><jupyter_text>You can assign any value to a variable in this way.  Inserting commentsDid you see this part of the line? `Do not remove this line. Type your code below this line.`Python ignores everything typed after a `` in the line. You can use this to insert comments in your code. Comments can be used to make notes for yourself to understand your code later.  The `print()` functionPython enables manipulation of data through **functions**.A function takes in zero, one, or more variables, and returns an output.To make Python **print** the result in the output, we can use the `print()` function. The print function requires an input variable to work. this input variable goes inside the parentheses of the `print()` function.Run the code cell below:<jupyter_code># Try your code in this cell

num1 = 2
print(num1)

num2 = num1 + 2
print(num2)

num3 = num1 ** num1
print(num3)<jupyter_output><empty_output><jupyter_text>Expected output:```244```Each `print()` function produces one line of output.You can also give a mathematical expression directly to the `print()` function. Run the code cell below:<jupyter_code># Try your code in this cell

print(num1 + 2)<jupyter_output><empty_output><jupyter_text>Python will **evaluate** the expression within the parentheses "`()`" first, and then send the result to the `print()` function.  ConstantsOther programming languages have special variables known as **constants**. Constants are variables whose value **cannot be changed** after they are initially assigned.Python does not support such variables. Instead, by convention, constants are named in all-UPPERCASE. This is a hint to other Python developers that changing its value is a Very Bad Idea.  Exercise 1: Use a constantUse the constant `PI` below to calcuate the circumference of a circle with radius 1.3.<jupyter_code># Try your code in this cell

PI = 3.14159 #Do not remove this line. Type your code below this line.

# Complete the code below using the constant PI and
# the variable r, and any appropriate operators
r = 1.3
circumference = #Write code here

# BEGIN SOLUTION
circumference = 2*PI*r
# END SOLUTION
circumference<jupyter_output><empty_output><jupyter_text>Python Self-Help: the `type()` functionThe `type()` function lets you examine a Python object and find out what type of object it is.  Python data type: stringIn the examples above, we were working only with numbers. One feature of Python that makes it very powerful is its ability to work with text as well.Text is stored as a string in Python. Python recognises anything between single-quotes `''` or double-quotes `""` as a string.Run the following expressions one by one in the cell below:1. `type(1)` (This is an integer.)2. `type(1.0)` (This is a float. Decimal numbers are known as **float**s)3. `type('1')` (This is a string.)4. `type("1")` (This is also a string.)5. `num1 = 1; type(num1)` (The variable `num1` has a value of `1` which is an integer.)6. `num1 = 1; type("num1")` (Explain: Why does this say `str` even though num1 should be an integer?)<jupyter_code># Try your code in this cell

type(1)<jupyter_output><empty_output><jupyter_text>The `type()` function in Python tells you what type of data is contained in an object.Notice in expressions 5 and 6 that we can put two expressions in one line by separating them with a semicolon. This is not recommended practice, but you can use it in a pinch when using the Python interpreter.  Single or double quotes?Run the following lines one by one in the cell below:1. `print("This is a string.")`2. `print("Is this a "string"?")`3. `print("It's a string!")`4. `print('What's a string?')`<jupyter_code># Try your code in this cell<jupyter_output><empty_output><jupyter_text>- single quotes may not be used within a single-quoted string.- double-quotes may not be used within a double-quoted string.  Python string operators: `+`, `*`Run the following expressions one by one in the cell below:1. `"This" "string"` (Adjacent strings without any operators will be joined together.)2. `"This"+" "+"string"` (The `+` operator joins strings.)3. `"This "+"joins "+3+"strings"` (The `+` operator only works on expressions with integers *or* strings, but not both.)4. `"This "+"joins "+str(3)+"strings"` (The `str()` function converts integers and floats to strings.)5. `print("This","joins",3,"strings")` (The `print()` function outputs multiple strings separated by a space, and **auto-converts other data types to strings**.)6. `"This"-"joins"` (Strings do not work with the `-` operator, or with `/`, `**` for that matter.)7. `print("Many"*3,"strings")` (Strings may be repeated with the `*` operator followed by an integer.)<jupyter_code># Try your code in this cell<jupyter_output><empty_output><jupyter_text>Obtaining user input Sometimes, you need the user to give you some data for you to use in your program. One way to get simple input is using the `input()` function.Run the expression in the cell below without changing it. Type in different things in the input box and see what you get.<jupyter_code># Try your code in this cell

var = input('Type something: ')
var_type = type(var)
print('Your input is type',var_type)<jupyter_output><empty_output><jupyter_text>Notice that input is always treated as a string, even if you only keyed in numbers. If you want numerical input, you will have to manually **cast** the input string to `int` using the `int()` function, or to `float` using the `float()` function.Run the cells below:<jupyter_code>var = input('Type a number: ')
int(var)
var = input('Type another number: ')
float(var)<jupyter_output><empty_output><jupyter_text>You can also convert `int` to `float`, or `float to `int`, using these functions.Run the cells below:<jupyter_code>num = 2
float(num)
num = 2.0
int(num)<jupyter_output><empty_output><jupyter_text>You can cast numbers to strings using the `str()` function.What happens if you run the code in the below cell *without* the `str()` function?<jupyter_code>var = 2
new_var = str(var) #What happens if you remove the str() function from this line?
print('The value of var is '+new_var)<jupyter_output><empty_output><jupyter_text>Q1: Can the above code work if `var` is a float? A1:  BEGIN SOLUTION Yes. A float can be converted to a string too. END SOLUTION  Python comparators Python provides a few **comparators** for comparing objects or variables.  The comparators return `True` if the expression is true, or `False` if the expression is false.The following operators are supported:- `a == b` (`a` equal to `b`)  - `a != b` (`a` not equal to `b`)  - `a < b` (`a` less than `b`)  - `a <= b` (`a` less than or equal to `b`)  - `a > b` (`a` greater than `b`)  - `a >= b` (`a` greater than or equal to `b`)Run the following expressions one by one in the cell below:1. `1 == 1` (Replace the `==` in this expression with the other comparators and see what result you get.)2. `1 == 2` (Replace the `==` in this expression with the other comparators and see what result you get.)3. `'a' = 'a'` (Comparators work on strings as well.)4. `'a' == 'A'` (lowercase and uppercase strings are treated differently.)5. `(1 + 4) != (2 + 3)` (Python can compare the result of expressions if they are enclosed in parentheses.)6. `1 + 4 == 2 + 3` (... and can do so even if they are not!)7. `'104' <= '1050'` (Try the comparators with strings. Are you surprised by the result? Can you figure out what the rule for comparing strings is?)<jupyter_code># Try your code in this cell<jupyter_output><empty_output><jupyter_text>Python booleansNotice that the result of each line above is always `True` or `False`. These are special data types known as **booleans**. There are only two boolean values, and they are the reverse of each other.Run the following expressions one by one in the cell below:1. `type(True)` (booleans are a different data type fron integers, floats, and strings)2. `not True` (The keyword `not` in front of a Boolean reverses its value)3. `not(False)` (`not()` is available for use as a function)4. `int(True), int(False)` (`True` and `False` values are represented as the integers `1` and `0` when converted. You can cast booleans to `float` as well.)5. `str(True), str(False)` (Note that `True` and `False` are not strings. To use them with other strings, they must first be converted.)6. `type(true)` (`True` and `False` booleans are special keywords and must always start with uppercase. `true` and `false` will be treated as variables!)7. `True + 1` (When using booleans with operators, they are automatically casted to numerical values.)8. `print('This sentence is',True)` (When using booleans in sentences that autocast types to strings, `True` and `False` are casted as strings)<jupyter_code># Try your code in this cell<jupyter_output><empty_output><jupyter_text>Errors in Python: `SyntaxError`When Python is not able to carry out an operation in the code you typed, it will halt and raise an error.One common error you might encounter from running the code so far is `SyntaxError`. Syntax refers to the grammatical rules of the programming language. Some examples of syntax rules are below:1. The number of opening parentheses "`(`" must match the number of closing parentheses "`)`".3. Both sides of the assignment operator `=` must have a valid expression...and so on.A `SyntaxError` means there is a problem with your syntax and the Python interpreter is unable to understand your code.Run the following lines of code one by one and see what message is returned with `SyntaxError`:1. `num1 =` (This line is incomplete; `SyntaxError` will show you where the code terminated using a caret `^` character)2. `print(2 + 2` (This line has a missing closing parenthesis "`)`"; Python unexpectedly reached the **End Of File**, also known as **EOF**, before the code is complete.)<jupyter_code># Try your code in this cell
print(2 + 2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09568824809575625, 'fraction_numerical': 0.015029923830250272, 'mean_word_length': 4.73741708934842, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 75, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '99408', 'license': 'MIT', 'n_tokens_mistral': 4297, 'n_tokens_neox': 4067, 'n_words': 2141, 'path': 'Lessons-Basic/lesson_01.ipynb', 'repo_name': 'nyjc-computing/intro-to-python', 'size': '14704'}