mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Created using Colaboratory
This commit is contained in:
parent
54cf2e5da8
commit
3e55fa9d98
612
data-analytics/variables.ipynb
Normal file
612
data-analytics/variables.ipynb
Normal file
@ -0,0 +1,612 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "rKD4_pfUaEug"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Variables"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "jhQgNqS5aEui"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In Python, *variables* are used to store and manipulate data. A variable is essentially a named location in the computer's memory where a value can be stored. These values can be of different types, such as numbers, text, or more complex data structures."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Fn2yr5-NaEui"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Motivation\n",
|
||||||
|
"\n",
|
||||||
|
"Welcome to your journey of learning about variables in programming! Let's explore the reasons why learning about variables is valuable for beginners:\n",
|
||||||
|
"- **Data Storage**: Variables are like containers that can hold different types of information in your programs. They allow you to store and manage data, such as numbers, words, or other types of information. This makes your programs more dynamic and interactive because you can work with different kinds of data.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Real-World Problem Solving**: Variables are powerful tools for solving real-world problems in programming. They help you store and manipulate information that is important for your program to work correctly. For example, you can use variables to store a user's name or to perform calculations. By using variables, you can create programs that solve specific problems and make your life easier.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Code Readability and Maintainability**: Using variables makes your code easier to read and maintain. They act as labels that give meaning to the values you use in your program. This helps you and others understand what each value represents and makes it easier to update or change values when needed. Variables make your code more organized and easier to work with."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "3DsYS6Y0aEuj"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Assignment\n",
|
||||||
|
"\n",
|
||||||
|
"In programming, it is customary to reuse objects throughout a project. Rather than repeatedly defining an object in code, we can assign it to a variable and utilise the variable instead. This practice is known as **\"Don't Repeat Yourself\" (DRY)** coding, which you will encounter frequently in this course.\n",
|
||||||
|
"\n",
|
||||||
|
"To assign a value to a variable, we use the assignment operator (`=`). The value on the right side of the `=` sign is assigned to the variable on the left side."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "s_QyS_XxaEuj"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"age = 25"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "K1WavsG0aEuk"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In this example, we assign the value `25` to the variable `age`. Now, whenever we refer to `age` in our code, it will hold the value `25`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "ZfWhqqtdaEuk"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "e9d9brR_aEul"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Here, we assign the string `\"John\"` to the variable `name`. The variable `name` will store the text value `\"John\"`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "rx-dhE2waEul"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = 10\n",
|
||||||
|
"y = 5\n",
|
||||||
|
"sum = x + y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "23KHb0IKaEul"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In this case, we assign the result of the expression `x + y` to the variable `sum.` The values of `x` and `y` are added, and the sum is stored in the variable `sum`. We will talk more in depth about expression and operations in a future lesson.\n",
|
||||||
|
"\n",
|
||||||
|
"> When you assign a value to a variable, Python creates an object in memory to hold that value. The object represents the data and has its own unique identity.\n",
|
||||||
|
"\n",
|
||||||
|
"If we now go back to the previous example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "Xh9DY8BLaEul"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "r7nnosfraEul"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"When you assign the string value `\"John\"` to the variable `name`, Python creates a string object in memory and assigns it the value `\"John\"`. The variable name now references that object."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Rr8oNYWVaEul"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Naming Conventions\n",
|
||||||
|
"\n",
|
||||||
|
"When naming variables in Python, it's important to follow certain rules:\n",
|
||||||
|
"- Variable names must start with a letter or an underscore (`_`).They cannot start with a digit.\n",
|
||||||
|
"- The name can consist of letters (both lowercase and uppercase), digits, and underscores. However, it's recommended to use only lowercase letters for variable names.\n",
|
||||||
|
"- Names cannot start with a number or contain these symbols: __\\: \\' \\\" \\, \\< \\> \\/ \\? \\| \\\\ \\( \\) \\! @ \\# \\$ \\% \\^ \\& \\* \\~ \\- \\+__\n",
|
||||||
|
"- Variable names are case-sensitive, so `myVariable` and `myvariable` would be considered different variables\n",
|
||||||
|
"- Avoid using `l` (lowercase l), `O` (uppercase o), or `I` (uppercase i) as single-character names\n",
|
||||||
|
"- Avoid using reserved words or built-in function names as variable names. Reserved words are specific keywords that Python uses for its syntax, and built-in function names are names already used by Python for predefined functions.\n",
|
||||||
|
"\n",
|
||||||
|
"The list of reserved words can be found in the official Python documentation by visiting the following link: [Python 3.x Reserved Words](https://docs.python.org/3/reference/lexical_analysis.html#keywords). The list of built-in functions can be found in the official Python documentation by visiting the following link: [Python 3.x Built-in Functions](https://docs.python.org/3/library/functions.html).\n",
|
||||||
|
"\n",
|
||||||
|
"### Snake Case for Variable Naming\n",
|
||||||
|
"\n",
|
||||||
|
"In Python, it's common to use snake case for variable naming. Snake case is a naming convention where words are written in lowercase and separated by underscores.\n",
|
||||||
|
"\n",
|
||||||
|
"Examples of variable names using snake case:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "dkJcO7x7aEum"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"first_name = \"John\"\n",
|
||||||
|
"last_name = \"Doe\"\n",
|
||||||
|
"total_amount = 100.50"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "NJ1VCP1OaEum"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In the above examples, snake case is used to name variables. This convention improves the readability of the code by clearly separating words in the variable names."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "llrqoLudaEum"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"FirstName = \"John\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "hYEytaYNaEum"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In this example, the variable name `FirstName` does not follow the snake case convention. It's recommended to use lowercase letters and underscores to make the variable name `first_name` instead."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Viw-dRi7aEum"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Calling Variables\n",
|
||||||
|
"\n",
|
||||||
|
"Once you have assigned a value to a variable, you can call or access its value throughout your code. Calling a variable means retrieving its stored value to use it in expressions, statements, or other operations.\n",
|
||||||
|
"\n",
|
||||||
|
"To call a variable, simply use its name within your code. For example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "P7mCGEH_aEun",
|
||||||
|
"outputId": "9c3d3764-7d6c-4ad0-c17e-d16382017e09"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"'John'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\"\n",
|
||||||
|
"name"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "gwsr5ASvaEuo"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Calling variables in Python is as simple as using their names within your code. You can retrieve the stored values and use them in expressions, statements, calculations, or any other operation.\n",
|
||||||
|
"\n",
|
||||||
|
"If we now define two variables and want to call both of them:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "-X3DjnPKaEuo",
|
||||||
|
"outputId": "a7548c7d-24bf-426d-b9cf-8cf367587048"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"25"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\"\n",
|
||||||
|
"age = 25\n",
|
||||||
|
"name\n",
|
||||||
|
"age"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "5fWPAkrLaEup"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In the given code, when you call `name` and `age`, only the value of the last expression (`age`) is automatically displayed. The value of `name` is also evaluated, but it is not explicitly displayed.\n",
|
||||||
|
"\n",
|
||||||
|
"If you want to see both the values of `name` and `age` in the output, we will need to use the built-in Python function `print`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Y4fZ5qRIaEup"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## The print() Function\n",
|
||||||
|
"\n",
|
||||||
|
"The `print` function is a built-in Python function that allows you to display output to the console. It can be used to print variables, literal values, and the results of expressions or calculations.\n",
|
||||||
|
"\n",
|
||||||
|
"The basic syntax for using the `print` function is as follows:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "Tsa5BT_WaEup"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(value1, value2, value3, ...)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "DJq4b6JvaEup"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Here, `value1`, `value2`, `value3`, and so on represent the values or variables that you want to display. For example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "60r8GHROaEuq",
|
||||||
|
"outputId": "43b187c1-6120-41ab-c275-55487b8d572a"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"John\n",
|
||||||
|
"25\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\"\n",
|
||||||
|
"age = 25\n",
|
||||||
|
"\n",
|
||||||
|
"print(name)\n",
|
||||||
|
"print(age)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "JA399BZHaEuq"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Here's a breakdown of how the `print` function is used in this code:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "q1i-zhpEaEuq"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(name)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "T8oU2itxaEuq"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"This line of code calls the `print` function with the `name` variable as an argument. The `print` function then displays the value of the name variable, which is `\"John\"`, as output."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "Y5fzoyMSaEur"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(age)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "VDjuP6knaEur"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Similarly, this line of code calls the `print` function with the `age` variable as an argument. The `print` function displays the value of the `age` variable, which is `25`, as output.\n",
|
||||||
|
"\n",
|
||||||
|
"The `print` function can accept one or more arguments, separated by commas. It automatically converts the given values into a string representation and outputs them to the console. If multiple values are provided as arguments, the `print` function inserts a space between them by default. For example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "OvPjf1r9aEur",
|
||||||
|
"outputId": "1894cd46-3ab9-4664-a0ef-df4c2191c813"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"25 John\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"name = \"John\"\n",
|
||||||
|
"age = 25\n",
|
||||||
|
"\n",
|
||||||
|
"print(age, name)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "NhImV-m2aEur"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Reassignment\n",
|
||||||
|
"\n",
|
||||||
|
"In Python, you can change the value of a variable by assigning a new value to it. This is known as variable reassignment. Here's an example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "T_v1siigaEur",
|
||||||
|
"outputId": "a0aa525d-ee43-4084-e072-02a0e3beaac0"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"5\n",
|
||||||
|
"10\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = 5\n",
|
||||||
|
"print(x)\n",
|
||||||
|
"\n",
|
||||||
|
"x = 10\n",
|
||||||
|
"print(x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "68bJ7I-UaEur"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In this example, the variable `x` is assigned the value of `5`. We then print the value of `x` using the `print()` function, which outputs `5`.\n",
|
||||||
|
"\n",
|
||||||
|
"Next, we reassign the value of `x` to `10`. When we print the value of `x` again using `print()`, we get `10` as the output.\n",
|
||||||
|
"\n",
|
||||||
|
"> Note that when you reassign a variable, its previous value is discarded and replaced with the new value."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "jrj1a60naEur"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Types\n",
|
||||||
|
"\n",
|
||||||
|
"In Python, variables can hold values of different *data types*.\n",
|
||||||
|
"\n",
|
||||||
|
"> In programming, a data type refers to the classification or categorization of data that determines the type of values it can hold and the operations that can be performed on it. Each data type has its own characteristics and behaviors.\n",
|
||||||
|
"\n",
|
||||||
|
"Some of the data types that Python supports are:\n",
|
||||||
|
"- Integers (int): whole numbers without decimal points, such as `5`, `-3`, `0`\n",
|
||||||
|
"- Floating-point numbers (float): numbers with decimal points, such as `3.14`, `-0.5`, `1.0`\n",
|
||||||
|
"- Strings (str): sequences of characters enclosed within quotation marks, such as `\"Hello, World!\"`, `'Python'`, `\"12345\"`\n",
|
||||||
|
"- Booleans (bool): logical values that are either `True` or `False`\n",
|
||||||
|
"- Lists (list): ordered collections of values that can be of different data types\n",
|
||||||
|
"\n",
|
||||||
|
"We will go into detail in each and more of these data types in future lessons.\n",
|
||||||
|
"\n",
|
||||||
|
"> Python is a dynamically-typed language, which means that the data type of a variable is determined at runtime, based on the type of value it is currently holding. In Python, the `type()` function is a built-in function that allows you to determine the data type of a value or variable. It returns the data type of the provided object as a result. Here's an example:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "y-YmTkqgaEus",
|
||||||
|
"outputId": "5c43e53f-f4a2-4f49-bc4a-dad381876d7a"
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"<class 'int'>\n",
|
||||||
|
"<class 'str'>\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = 5\n",
|
||||||
|
"print(type(x))\n",
|
||||||
|
"\n",
|
||||||
|
"x = \"Hello\"\n",
|
||||||
|
"print(type(x))\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "V6OwioioaEus"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"In this example, we first assign the integer value `5` to the variable `x` and print its data type using the `type()` function. The output is `<class 'int'>`, which indicates that `x` is an integer.\n",
|
||||||
|
"\n",
|
||||||
|
"Next, we reassign `x` to the string value `\"Hello\"` and print its data type again using `type()`. The output is `<class 'str'>`, which indicates that `x` is now a string.\n",
|
||||||
|
"\n",
|
||||||
|
"Note that Python also provides several built-in functions to convert values from one data type to another, such as `int()`, `float()`, `str()`, and `bool()`, among others. These functions can be used to convert the value of a variable from one data type to another."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Dt2uPtjJaEus"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Best Practices\n",
|
||||||
|
"\n",
|
||||||
|
"When working with variables in Python, following best practices can make your code more readable, maintainable, and less prone to errors. Here are some key practices to keep in mind:\n",
|
||||||
|
"- **Use meaningful and descriptive variable names**: Choose variable names that clearly indicate the purpose or content of the variable. This helps in understanding the code and makes it more readable. For example, instead of using single-letter variable names like `x`, use descriptive names like `age`, `name`, or `total_sales`.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Follow naming conventions**: In Python, it is common to use lowercase letters and underscores to separate words in variable names. This convention is known as **snake_case**. For example, `my_variable`, `total_count`, or `user_name`. Following naming conventions improves code readability and consistency.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Initialise variables before use**: Always initialise variables with a value before using them. This prevents potential errors that may occur when trying to use variables that haven't been assigned a value yet.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Avoid using reserved words**: Do not use reserved words or built-in function names as variable names. These words have special meanings in Python and are used to perform specific tasks. For example, avoid using variable names like `print`, `for`, or `list`, as it can lead to unexpected behavior and errors.\n",
|
||||||
|
"\n",
|
||||||
|
"- **Update variable names when their purpose changes**: If the purpose of a variable changes during the course of your code, update its name to reflect the new usage. This ensures that the variable name accurately represents its current purpose, improving code clarity.\n",
|
||||||
|
"\n",
|
||||||
|
"By following these best practices, you can write cleaner, more readable, and less error-prone code that is easier to understand and maintain."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "ZhyGPAscaEuw"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Variable Errors and Debugging\n",
|
||||||
|
"\n",
|
||||||
|
"When working with variables, it is common to encounter errors or bugs in your code. Here are some common variable-related errors and tips for debugging them:\n",
|
||||||
|
"- `NameError: Name variable_name is not defined`: This error occurs when you try to use a variable that has not been defined or is out of scope. Double-check the spelling of your variable names. Make sure the variable is defined before using it.\n",
|
||||||
|
"\n",
|
||||||
|
"- `TypeError: unsupported operand type(s) for 'operator': 'type1' and 'type2'`: This error occurs when you try to perform an operation that is not supported between the given data types. Ensure that you are performing valid operations on variables of compatible types. Convert variables to the appropriate types if needed. We will learn more about this in future lessons.\n",
|
||||||
|
"\n",
|
||||||
|
"- `SyntaxError: invalid syntax`: This error typically occurs when there is a mistake in the syntax of your code. Check for missing parentheses, quotation marks, colons, or other syntax elements around your variables. Ensure that variables are correctly assigned and referenced. We will learn more about this in future lessons."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "fCDfvgpJaEux"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Key Takeaways\n",
|
||||||
|
"\n",
|
||||||
|
"- Variables are used to store and manipulate data in Python. They are like containers that hold values of different types, such as numbers, strings, or booleans.\n",
|
||||||
|
"- Variable assignment is the process of giving a name (identifier) to a value or an expression. It follows the syntax `variable_name = value`.\n",
|
||||||
|
"- Choose meaningful and descriptive variable names to improve code readability. Follow naming conventions, such as using lowercase letters and underscores for multi-word variable names (`snake_case`).\n",
|
||||||
|
"- Python is a dynamically-typed language, which means the data type of a variable is determined at runtime based on the value it holds. You can use the `type()` function to check the data type of a variable.\n",
|
||||||
|
"- The `print()` function is used to display the value of a variable or any other content on the console. It allows you to output information for debugging or to provide feedback to users."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "beedbe2faf2f7048d727558d0bc3221e7eba2a0b921cac4d4771b2feb8f74b30"
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.10.2 64-bit",
|
||||||
|
"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.2"
|
||||||
|
},
|
||||||
|
"colab": {
|
||||||
|
"provenance": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user