diff --git a/.ipynb_checkpoints/1_variables_operators-checkpoint.ipynb b/.ipynb_checkpoints/1_variables_operators-checkpoint.ipynb new file mode 100644 index 0000000..5d0ce83 --- /dev/null +++ b/.ipynb_checkpoints/1_variables_operators-checkpoint.ipynb @@ -0,0 +1,1688 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# LAB | Variables & Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Variables are used to store data in a program.**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Consider the following analogy: you have a water source and you want to collect some water. If you don't have a placeholder (mug, bucket, bottle,...), you will not be able to collect it as the water will drain through your fingers.*\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### Naming variables\n", + "\n", + "In Python, variables must have valid names. Here are some rules for naming variables:\n", + " - Variable names can contain letters (a-z, A-Z), digits (0-9), and underscores (_).\n", + " - Variable names cannot start with a digit.\n", + " - Variable names are case-sensitive, meaning myVar and myvar are considered different variables.\n", + " - It's recommended to use descriptive names that reflect the purpose of the variable.\n", + " \n", + " Here are the [rules](https://www.w3schools.com/python/gloss_python_variable_names.asp).\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Assigning Values\n", + "\n", + "To assign a value to a variable, you use the assignment operator (=). Here's an example:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "age = 25" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above example, we assigned the value 25 to the variable `age`. Now, we can use the variable age to refer to that value throughout the program." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Variable Reassignment\n", + "\n", + "Variables in Python can be reassigned with new values. You can change the value of a variable simply by assigning a new value to it. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "age = 25\n", + "age = 30" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above code, we initially assigned the value 25 to the variable `age`. Then, we reassigned it with the value 30. Now, the variable `age` holds the new value." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Looking at the value of a variable" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Jupyter Notebook, when you write the name of a variable (`age`for example) in a cell and run the cell, it automatically displays the value of `age` on the screen. This is because Jupyter Notebook, by default, **displays the output of the last line of code in a cell**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This is a comment. You can write comments in code cells with #, such as this one." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "new_age = 40 # Let`s declare a new variable\n", + "new_age # Will show the value of the variable on the screen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "age # Will show the value of the variable on the screen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "new_age # Will show the value of the variable on the screen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "age\n", + "new_age # In this case, will show the value of the last line of code on the screen (variable new_age)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python and in Jupyter Notebook, you can use the `print()` function to display the value of a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(new_age)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `print()` function is a built-in function in Python that allows us to output information. In this case, we are printing the value of the variable `new_age`.\n", + "\n", + "So, when you run this code, `print(new_age)`it will display 40 as the output. The value 40 is stored in the variable new_age, and the `print()` function helps us see that value on the screen." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Both methods achieve the same result of showing the value of new_age on the screen.\n", + "\n", + "The advantage of using `print()` is that you can display multiple values or combine them with other text or variables. However, when you want to quickly check the value of a variable or see the result of a single expression, writing the variable name or expression in a cell without using print() is a convenient and concise way to display the output. We will look at this later on the course." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Variable Types\n", + "\n", + "Variables in Python can hold different types of data. \n", + "\n", + "*Python is a dynamically-typed language, meaning you don't need to explicitly specify the variable type. The type of a variable is determined based on the value assigned to it.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "y = 20.5\n", + "z = 'hello'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the code x = 10, we are assigning a value to a variable. We are creating a variable called x and assigning it the value 10.\n", + "\n", + "To display the value of x, we use the `print()` function. When you run this code, `print(x)` will display 10 as the output. The value 10 is stored in the variable x, and the `print()` function helps us see that value on the screen." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(z)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Observation \n", + "\n", + "In programming, a function is like a pre-written instruction that performs a specific task. It's like a special command that you can use in your code. These functions are either created by other programmers or provided by the programming language itself.\n", + "\n", + "The print() function is one such built-in function in Python. It is ready to use without requiring any additional setup. This function allows us to display information on the screen.\n", + "\n", + "In Python, functions are names followed by opening and closing parentheses.\n", + "\n", + "*(FYI so are methods. We won't go into detail about methods in this lesson, but we will cover them during the bootcamp.)*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data Types " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python, data has different types that define the kind of information it represents. Understanding data types is essential for performing operations and manipulating data. Let's explore the common data types in Python:\n", + "\n", + "- **Integer**: represents whole numbers, i.e. positive or negative integer values. _(eg. 10, 3, -5, 0)_\n", + "- **Float**: represents decimal numbers. _(eg. 20.2, 100.2403, -5.50)_\n", + "- **String**: represents text _(e.g., \"Hello, World!\", \"Python\")_. A string is considered a group of characters. It can be encapsulated in a single quote or double quotes _(eg. \"hello\", \"Ironhack\", \"Teacher\")_\n", + "- **Boolean**: represents either True or False\n", + "\n", + "It's important to understand the data type of a variable because it determines how the variable behaves and what operations can be performed on it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To check our data types we will use the function `type()`. *This is a function, such as print(), but performs a different task.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Integer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "type(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As you can see from the example above, `print` and `type` are followed by `()` and they are functions. They take an argument (x) and the return another value. `type` for example returns the type of the argument (x)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Float" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y = 20.5\n", + "print(y)\n", + "type(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String\n", + "String can be encapsulated in single quotes or double quotes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "z = \"hello\"\n", + "print(z)\n", + "type(z)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 'hello there'\n", + "print(x)\n", + "type(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Note:** As demonstrated in the examples above, strings can be defined using either double `\" \"` or single quotes `' '`. You may be wondering about the appropriate usage of double quotes versus single quotes. In general, the choice depends on the content of your string. If your string includes single quotes, such as in the case of *I couldn't make it*, then it is necessary to use double quotes. However, if your string does not contain single quotes, you can confidently use single quotes." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Note:** In the previous examples, the strings were of small length, consisting of a single line. However, what if we need to define a string that spans multiple lines? In such cases, we employ three single or double quotes to delimit the string. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Multiple line string\n", + "x = '''Float - stores decimal numbers. By default, Python interprets any number that includes a decimal point as a double precision floating point number. Like some other programming languages, in Python there is not much difference between float and double except that float is in-built in Python, while double is imported from a library called NumPy.\n", + "We will talk about the libraries in detail later.\n", + "Hello\n", + "There!'''\n", + "print(x)\n", + "type(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Boolean" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = True # This is highlighted in green because it is a keyword\n", + "print(x)\n", + "type(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = False\n", + "print(x)\n", + "type(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = \"True\" # In this case it is a string because we encapsulated it in double quotes\n", + "print(x)\n", + "type(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Observation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python, Boolean values (True and False) are actually treated as a subclass of integers. This means that `True` is equivalent to the integer value 1, and `False` is equivalent to the integer value 0. \n", + "\n", + "This means that you can perform numeric operations on Boolean values, and they will be implicitly converted to their corresponding integer values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bool_true = True\n", + "bool_false = False\n", + "\n", + "print(bool_true + 1) # Output: 2 (True + 1 = 1 + 1 = 2)\n", + "print(bool_false + 1) # Output: 1 (False + 1 = 0 + 1 = 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Understanding that Boolean values (True and False) are equivalent to 1 and 0 in certain operations can be useful when performing calculations or conditional checks in your code, as we will study during the bootcamp." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Operators are special symbols or keywords used to perform operations on data. They allow us to manipulate values and perform calculations." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are various types of operators, including arithmetic, assignment, comparison, logical, and bitwise operators. \n", + "We will look at some of the more common operators that we use in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# We will define two integer variables here and use operators on them\n", + "x = 10\n", + "y = 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# We will define two string variables here and use operators on them\n", + "name = \"Peter\"\n", + "surname = \"Gates\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Arithmetic Operators " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Arithmetic operators are used to perform mathematical calculations on numeric data types. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here on the left hand side we have the operator and on the right we can see how they can be used with variables\n", + "- `+` - Addition (x + y): The addition operator is used to add two or more numbers together.\n", + "- `-`\t- Subtraction (x - y): The subtraction operator is used to subtract one number from another.\n", + "- `*`\t- Multiplication (x * y): The multiplication operator is used to multiply two or more numbers.\n", + "- `/` - Division (x / y): The division operator is used to divide one number by another. It returns a floating-point result.\n", + "- `//` - Integer division (x // y): The integer division operator divides one number by another and returns the integer quotient, discarding any decimal places.\n", + "- `%` - Modulus or modulo (x % y): The modulus or modulo operator returns the remainder of the division between two numbers.\n", + "- `**` - Exponentiation (x ** y): The exponentiation operator raises a number to the power of another number." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# You can try and test these operators as shown below" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x+y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x-y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x*y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x/y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x%y # Gives the remainder" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x**y # 'y' times multiplication of 'x'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x // y # Rounds off the result to the lower integer value on the number line" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In addition to working with numerical values, some arithmetic operators can also be used with strings in Python. Let's explore how these operators behave when applied to strings:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Addition (+): The addition operator is used to concatenate or join two or more strings together." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "name + surname" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Multiplication (*): The multiplication operator can be used to repeat a string a certain number of times." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "name * 3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "These two arithmetic operators can also be used with other data structures that we will see in the following lessons, such as lists." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Please note that the subtraction (-), division (/), integer division (//), modulo (%), and exponentiation (**) operators are not defined for strings, lists or other data structures in Python. \n", + "\n", + "What means to substract one word from another one? Or to divide?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Assignment Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Assignment operators are used to assign values to variables. They allow you to store and update data in variables. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We have already seen it before when we talked about 'Variables' and 'Data Types'." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python, there are several assignment operators available:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`= (Equals)` - The equals sign assigns the value on the right-hand side to the variable on the left-hand side." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`+= (Add and Assign)` - The plus-equals operator adds the value on the right-hand side to the variable's current value and assigns the result back to the variable." + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "x += 3 is equivalent to x = x + 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "x+=3\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`-= (Subtract and Assign)` - The minus-equals operator subtracts the value on the right-hand side from the variable's current value and assigns the result back to the variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "x-=3\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`*= (Multiply and Assign)` - The multiply-equals operator multiplies the variable's current value by the value on the right-hand side and assigns the result back to the variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "x*=3\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`/= (Divide and Assign)` - The divide-equals operator divides the variable's current value by the value on the right-hand side and assigns the result back to the variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "x/=3\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And so on with modulus, floor divison, and exponentiation." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "These assignment operators can be combined with various arithmetic and logical operations to perform calculations and update variable values in a concise manner.\n", + "\n", + "Remember that assignment operators are used to update the value of a variable, so the variable on the left-hand side should already exist before using an assignment operator on it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Comparison Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Comparison operators are used to compare values and determine the relationship between them. They return either `True` or `False` based on the comparison result. Returns `True` if the condition is met, otherwise returns a `False`. \n", + "\n", + "In Python, there are several comparison operators available:\n", + "\n" + ] + }, + { + "cell_type": "raw", + "metadata": { + "vscode": { + "languageId": "raw" + } + }, + "source": [ + "== Equal x == y\n", + "!= Not equal x != y\n", + "> Greater than x > y\n", + "< Less than x < y\n", + ">= Greater than or equal to x >= y\n", + "<= Less than or equal to x <= y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10 # Initialize variable `x` with a value 10\n", + "y = 5 # Initialize variable `y` with a value 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`== (Equal to)` - The double equals operator compares if two values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x == y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`!= (Not equal to)` - The exclamation mark followed by equals compares if two values are not equal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x != y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`< (Less than)` - The less than operator compares if the value on the left is less than the value on the right." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x > y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`> (Greater than)` - The greater than operator compares if the value on the left is greater than the value on the right." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x >= y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`<= (Less than or equal to)` - The less than or equal to operator compares if the value on the left is less than or equal to the value on the right." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x= (Greater than or equal to)` - The greater than or equal to operator compares if the value on the left is greater than or equal to the value on the right." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x <= y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "These comparison operators allow you to compare numbers, strings, and other data types in Python. They are commonly used in conditional statements and loops to make decisions based on the comparison results.\n", + "\n", + "It's important to note that the comparison operators always return a Boolean value, either True or False, depending on the outcome of the comparison." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Logical Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Logical operators are used to combine and manipulate logical values (True or False). They allow you to perform logical operations on conditions and make decisions based on the results. In Python, there are three logical operators available:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10 # First we will initialize variable `x` with a value 10.\n", + "y = 15 # We will initialize variable `y` with a value 15." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**and**: The and operator returns True if both of the conditions on either side of it are True. Otherwise, it returns False." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "x < 5 and y < 10 # Returns False since both conditions are False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "x > 5 and y < 20 # Returns True since both conditions are True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x < 5 and y < 20 # Returns False since one of the conditions is False, even the other one is True" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**or**: The or operator returns True if at least one of the conditions on either side of it is True. If both conditions are False, it returns False." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x < 5 or y < 10 # Returns False since both conditions are False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x > 5 or y < 20 # Returns True since both conditions are True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x < 5 or y < 20 # Returns True since one of the conditions is True, even one is False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**not**: The not operator is a unary operator that negates the logical value of a condition. It returns True if the condition is False and False if the condition is True." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "not(x < 5 or y < 20) # x < 5 or y < 20 is True, so applying not we get False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "not(x < 5 or y < 10) # x < 5 or y < 10 is False, so applying not we get True" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data Type Compatibility" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "While some operations are supported across different data types, it's important to recognize that not all data types can be used together in the same way. Performing certain operations with incompatible data types can result in errors." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, what is 1 plus \"hello\"? " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "1+\"hello\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We get an error because the computer doesn't know how to sum a number plus a string (neither do we). " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As a general rule of thumb, if we can't perform certain operations in our minds, the computer won't be able to do them either. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Computers are designed to follow strict rules and execute operations precisely. When we attempt to combine incompatible data types, like adding a number to a string, the computer recognizes this mismatch and throws an error. It's important to remember that computers can only perform operations that are explicitly defined for each data type." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data Type Casting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python, data type casting, also known as type conversion, allows you to change the data type of a value from one type to another. This can be useful when you want to perform operations or manipulate data that requires specific data types. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`int()` function: To convert a value to an integer, you can use the `int()` function. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_float = 3.14 # This is a float\n", + "num_int = int(num_float)\n", + "print(num_int) # Output: 3\n", + "print(type(num_int)) # Output: int" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_str = \"42\" # This is a String\n", + "num_int = int(num_str)\n", + "print(num_int) # Output: 42\n", + "print(type(num_int)) # Output: int" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "`float()` function: To convert a value to a float, you can use the `float()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_int = 5 # This is a integer\n", + "num_float = float(num_int)\n", + "print(num_float) # Output: 5.0\n", + "type(num_float) # Output: float" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_str = \"3.14\" # This is a string\n", + "num_float = float(num_str)\n", + "print(num_float) # Output: 3.14\n", + "type(num_float) # Output: float" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`str()` function: To convert a value to a string, you can use the str() function. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_int = 42 # This is a integer\n", + "num_str = str(num_int)\n", + "print(num_str) # Output: \"42\"\n", + "type(num_str) # Output: str" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_bool = True # This is a boolean\n", + "num_str = str(num_bool)\n", + "print(num_str) # Output: \"True\"\n", + "type(num_str) # Output: str" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`bool()` function: To convert a value to a boolean, you can use the `bool()` function. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_int = 0 # This is a integer\n", + "num_bool = bool(num_int)\n", + "print(num_bool) # Output: False\n", + "type(num_bool) # Output: bool" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Remember that not all types can be converted to each other. Some conversions may result in data loss or unexpected behavior, so it's important to understand the limitations and implications of type casting." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## More Resources" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "https://www.programiz.com/python-programming/operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "https://www.tutorialspoint.com/python/python_basic_operators.htm" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. Excercise - Data Types " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**1.1** For the given variables, use the `type()` function in Python to check their data types:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x1 = 1.1\n", + "\n", + "x2 = \"Ironhack\"\n", + "\n", + "x3 = \"1.1\"\n", + "\n", + "x4 = True\n", + "\n", + "x5 = \"True\"\n", + "\n", + "x6 = -1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2. What is the difference between the variables `x1` and `x3`?**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.Substract `x3` from `x1`. Explain what happens.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**4. What is the difference between the variables `x4` and `x5`?**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**5.Substract `x4` from `x5`. Explain what happens.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. Exercise - Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.1** In this problem, we will demonstrate how to take user input. To gather input from the user, we can use the `input(message)` function, where *message* is a message of type *string* that will be displayed to the user. \n", + "\n", + "```py\n", + "x1 = input(\"Please enter an integer number: \")\n", + "x2 = input(\"Please enter another integer number: \")\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Observation:** When you run two or more lines of code in the same cell, or in different cells, in Jupyter Notebook, it will be executed one at a time, in sequence. \n", + "In this exercise, each of the `input()` functions will create a cell box where you can enter a value and press Enter to confirm. You can identify these input cells because they have a star symbol on the left side as shown [here](https://education-team-2020.s3.eu-west-1.amazonaws.com/data-analytics/prework/unit1/input_function.png), indicating that the computer is waiting for your input (or executing code).\n", + "After you enter a value, the star symbol will disappear, and the next `input()` function will be executed, displaying a new cell box with a star symbol for the next input. It's important to note that **while an input cell has a star symbol, it will prevent you from running other cells** (if you have the default settings on your Jupyter Notebook environment)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**_Question 1_**: \n", + "\n", + "Use the code provided above and ask the user to input two numbers. Then, print the values of the two variables. What is the type of `x1` and `x2`? Don't just answer by looking at it's value, use the function `type()` to do so." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Even though we entered *integers*, the `input()` function converts them into *strings* **by default** (it's the way the function `input()` is defined). Now we will perform data type conversion from *string* to *integer*. Use the `int()` function to do so. Print again the values after performing data type conversion and the corresponding type." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**_Question 2_**: \n", + "\n", + "Perform the following simple comparisons, using operators:\n", + "\n", + "- Check if the two variables are equal.\n", + "- Check if `x1` is greater than `x2`.\n", + "- Check if `x2` is greater than `x1`.\n", + "- Check if `x1` is not equal to `x2`.\n", + "\n", + "Store the difference between `x1` and `x2` in another variable `x3` (subtract the smaller number from the larger number). \n", + "\n", + "Increment the smaller of the two variables (`x1` and `x2`) with the difference. Use the shorthand addition operator for the same. Again check if `x1` and `x2` are equal or not.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. Exercise - Concatenation\n", + "\n", + "Write a program that asks the user to enter their first name and last name as separate strings. Concatenate the two strings and display a greeting message, such as \"Hello, John Doe!\"." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Additional Content " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "y = 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Identity Operator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Identity operators are used to compare the identity of two objects in Python. They determine whether two variables or values refer to the same object in memory." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Understanding Object Identity:\n", + "\n", + "In Python, objects are stored in memory, and variables hold references (or pointers) to those objects. When we use identity operators, we are comparing the memory addresses of the objects to check if they are the same." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**is**: The is operator checks if two variables refer to the same object." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Returns `True` if both variables are the same object.\n", + "The `x` is the `y`. Otherwise returns False." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x is y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**is not**: The is not operator checks if two variables do not refer to the same object." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Returns `True` if both variables are not the same object.\n", + "`x` is not `y`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x is not y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The is operator checks if two variables refer to the same object.\n", + "\n", + "The is not operator checks if two variables do not refer to the same object." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Membership Operator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Membership operators are used to test the membership of a value in a sequence (such as a string, list, tuple, or set) in Python. They determine whether a value is present or not in the given sequence." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = 'I'\n", + "b = 'Ironhack'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**in**: The in operator checks if a value exists in a sequence." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Returns `True` if a sequence with the specified value is present in the object.\n", + "`x` in `y`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a in b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**not in**: The not in operator checks if a value does not exist in a sequence." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Returns `True` if a sequence with the specified value is not present in the object.\n", + "`x` not in `y`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a not in b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Membership operators perform membership testing by checking if a value matches any element in the given sequence. They return a boolean value (True or False) based on the test result." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/1_variables_operators.ipynb b/1_variables_operators.ipynb index 5d0ce83..c885b76 100644 --- a/1_variables_operators.ipynb +++ b/1_variables_operators.ipynb @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -74,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -105,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -114,9 +114,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "new_age = 40 # Let`s declare a new variable\n", "new_age # Will show the value of the variable on the screen" @@ -124,27 +135,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "age # Will show the value of the variable on the screen" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "new_age # Will show the value of the variable on the screen" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "age\n", "new_age # In this case, will show the value of the last line of code on the screen (variable new_age)" @@ -159,9 +203,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "40\n" + ] + } + ], "source": [ "print(new_age)" ] @@ -197,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -208,9 +260,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], "source": [ "print(x)" ] @@ -226,18 +286,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20.5\n" + ] + } + ], "source": [ "print(y)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], "source": [ "print(z)" ] @@ -294,9 +370,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], "source": [ "x = 10\n", "print(x)" @@ -304,9 +388,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(x)" ] @@ -327,9 +422,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20.5\n" + ] + }, + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "y = 20.5\n", "print(y)\n", @@ -346,9 +459,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z = \"hello\"\n", "print(z)\n", @@ -357,9 +488,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello there\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x = 'hello there'\n", "print(x)\n", @@ -382,9 +531,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Float - stores decimal numbers. By default, Python interprets any number that includes a decimal point as a double precision floating point number. Like some other programming languages, in Python there is not much difference between float and double except that float is in-built in Python, while double is imported from a library called NumPy.\n", + "We will talk about the libraries in detail later.\n", + "Hello\n", + "There!\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Multiple line string\n", "x = '''Float - stores decimal numbers. By default, Python interprets any number that includes a decimal point as a double precision floating point number. Like some other programming languages, in Python there is not much difference between float and double except that float is in-built in Python, while double is imported from a library called NumPy.\n", @@ -404,9 +574,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + }, + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x = True # This is highlighted in green because it is a keyword\n", "print(x)\n", @@ -415,9 +603,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + }, + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x = False\n", "print(x)\n", @@ -426,9 +632,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x = \"True\" # In this case it is a string because we encapsulated it in double quotes\n", "print(x)\n", @@ -453,9 +677,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "1\n" + ] + } + ], "source": [ "bool_true = True\n", "bool_false = False\n", @@ -495,7 +728,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, "outputs": [], "source": [ @@ -506,7 +739,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -545,7 +778,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -554,63 +787,140 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x+y" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x-y" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x*y" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x/y" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x%y # Gives the remainder" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "100000" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x**y # 'y' times multiplication of 'x'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x // y # Rounds off the result to the lower integer value on the number line" ] @@ -631,9 +941,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'PeterGates'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "name + surname" ] @@ -647,9 +968,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'PeterPeterPeter'" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "name * 3" ] @@ -707,9 +1039,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], "source": [ "x = 10\n", "print(x)" @@ -724,16 +1064,28 @@ }, { "cell_type": "raw", - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "raw" + } + }, "source": [ "x += 3 is equivalent to x = x + 3" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n" + ] + } + ], "source": [ "x = 10\n", "x+=3\n", @@ -749,9 +1101,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], "source": [ "x = 10\n", "x-=3\n", @@ -767,9 +1127,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n" + ] + } + ], "source": [ "x = 10\n", "x*=3\n", @@ -785,9 +1153,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.3333333333333335\n" + ] + } + ], "source": [ "x = 10\n", "x/=3\n", @@ -845,7 +1221,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -862,9 +1238,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x == y" ] @@ -878,9 +1265,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x != y" ] @@ -894,9 +1292,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x > y" ] @@ -910,9 +1319,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x >= y" ] @@ -926,9 +1346,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x 5 and y < 20 # Returns True since both conditions are True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x < 5 and y < 20 # Returns False since one of the conditions is False, even the other one is True" ] @@ -1029,27 +1504,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x < 5 or y < 10 # Returns False since both conditions are False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x > 5 or y < 20 # Returns True since both conditions are True" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x < 5 or y < 20 # Returns True since one of the conditions is True, even one is False" ] @@ -1063,18 +1571,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "not(x < 5 or y < 20) # x < 5 or y < 20 is True, so applying not we get False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "not(x < 5 or y < 10) # x < 5 or y < 10 is False, so applying not we get True" ] @@ -1102,9 +1632,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for +: 'int' and 'str'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[60], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n", + "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" + ] + } + ], "source": [ "1+\"hello\"" ] @@ -1247,11 +1789,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + }, + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "num_int = 0 # This is a integer\n", + "num_int = 1 # This is a integer\n", "num_bool = bool(num_int)\n", "print(num_bool) # Output: False\n", "type(num_bool) # Output: bool" @@ -1308,7 +1868,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ @@ -1327,10 +1887,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(x1) # Float\n", + "type(x2) # String\n", + "type(x3) # String\n", + "type(x4) # Boolean\n", + "type(x5) # String\n", + "type(x6) # Integer" + ] }, { "cell_type": "markdown", @@ -1344,7 +1922,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# x1 is a Float and x3 an String. A float in a number with a comma and a string is like a Word." + ] }, { "cell_type": "markdown", @@ -1358,7 +1938,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "x3-x1 # This will give an error because you cannot subtract a string from a float" + ] }, { "cell_type": "markdown", @@ -1372,7 +1954,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# x4 is a Boolean and x5 is a String. A boolean is a data type that can only be True or False, while a string is a sequence of characters." + ] }, { "cell_type": "markdown", @@ -1386,7 +1970,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "x4-x5 # This will give an error because you cannot subtract a string from a boolean." + ] }, { "cell_type": "markdown", @@ -1427,10 +2013,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "9\n", + "\n", + "\n" + ] + } + ], + "source": [ + "x1 = input(\"Please enter an integer number: \")\n", + "x2 = input(\"Please enter another integer number: \")\n", + "print (x1)\n", + "print (x2)\n", + "print (type(x1))\n", + "print (type(x2))" + ] }, { "cell_type": "markdown", @@ -1441,10 +2045,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "9\n", + "\n", + "\n" + ] + } + ], + "source": [ + "x1 = int(x1)\n", + "x2 = int(x2)\n", + "print (x1)\n", + "print (x2)\n", + "print (type(x1))\n", + "print (type(x2))" + ] }, { "cell_type": "markdown", @@ -1466,10 +2088,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x1 == x2\n", + "x1 > x2\n", + "x2 > x1\n", + "x1 != x2\n", + "\n", + "x3 = x1-x2\n", + "\n", + "x1 += x3\n", + "x1 == x2" + ] }, { "cell_type": "markdown", @@ -1482,10 +2125,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Guille Fiallo Montero!\n" + ] + } + ], + "source": [ + "FirstName = input(\"Please enter your first name: \")\n", + "LastName = input(\"Please enter your last name: \")\n", + "print (\"Hello,\", FirstName, LastName + \"!\")" + ] }, { "cell_type": "markdown", @@ -1496,7 +2151,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -1544,9 +2199,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x is y" ] @@ -1568,9 +2234,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "x is not y" ] @@ -1600,7 +2277,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -1625,9 +2302,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a in b" ] @@ -1649,9 +2337,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a not in b" ] @@ -1666,7 +2365,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -1680,7 +2379,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.1" } }, "nbformat": 4,