{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Python Variables.ipynb", "provenance": [], "authorship_tag": "ABX9TyNaHyzT8vEu+M5zbjDt87/W", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "yRgHPumxce8E", "colab_type": "text" }, "source": [ "# **Python Variables**\n", "\n", "![alt text](https://images.unsplash.com/photo-1484417894907-623942c8ee29?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=889&q=80)\n", "\n", "\n", "In programming languages like C/C++, every time a variable is declared simultaneously a memory would be allocated this would allocation would completely depend on the variable type. Therefore, the programmers must specify the variable type while creating a variable. But luckily in Python, you don’t have to do that. Python doesn’t have a variable type declaration. Like pointers in C, variables in Python don’t store values legitimately; they work with references highlighting objects in memory.\n", "\n", "## **Variables**\n", "\n", "A variable is more likely a ***container*** to store the values. Now the values to be stored depends on the programmer whether to use integer, float, string or **[etc](https://www.w3schools.com/python/python_datatypes.asp)**.\n", "\n", "\n", "\n", "> ***A Variable is like a box in the computer’s memory where you can store a single value. — Al Sweigart***\n", "\n", "\n", "Unlike in other programming languages, in Python, you need not declare any variables or initialize them. Please read [this](https://stackoverflow.com/a/11008311/11646278).\n", "\n", "\n", "### **Syntax**\n", "\n", "\n", "The general syntax to create a variable in Python is as shown below:\n", "\n", "`variable_name = value`\n", "\n", "The `variable_name` in Python can be short as sweet as `a, b, x, y, ...` or can be very informative such as `age, height, name, student_name, covid, ...`\n", "\n", "\n", "> ***Although it is recommended keeping a very descriptive variable name to improve the readability.***\n", "\n", "\n", "\n", "### **Rules**\n", "\n", "**All set and done, there are some rules that you need to follow while naming a variable:**\n", "\n", "* A variable name must start with a ***letter*** or the ***underscore character***\n", "\n", "* A variable name cannot start with a ***number***\n", "\n", "* A variable name can only contain ***alpha-numeric characters*** and ***underscores***. For example, anything like this is valid: ***A-z, 0–9, and _***\n", "\n", "* Variable names are ***case-sensitive*** ***(height, Height***, and ***HEIGHT*** are three different variables names)\n", "\n", "### **Example**\n", "\n", "Below given is an example to properly initialize a value to a variable:" ] }, { "cell_type": "code", "metadata": { "id": "NQKdREzkdw76", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "70b43a3d-c3b4-4846-f786-64b07890bb4d" }, "source": [ "# This is a valid and good way to assign a value to a variable\n", "# For example you might want to assign values to variables to calculate the area of a circle\n", "\n", "pi = 3.142 # I could have also used \"math\" library (math.pi)\n", "radius = 5 # Interger value for radius\n", "area_of_circle = 0 # Used to store the value of area of circle\n", "\n", "area_of_circle = pi * (radius) ** 2 # Area = (PI * R^2)\n", "\n", "print(\"The area of the circle based on the given data is: \", area_of_circle)" ], "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ "The area of the circle based on the given data is: 78.55\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "DIyEuUw8d3aU", "colab_type": "text" }, "source": [ "### **Pictorial Example (Skim it quickly)**\n", "I# believe just by seeing a picture or an image, the concepts can be understood more quickly. Below is the pictorial representation of a variable and it being stored in the memory.\n", "\n", "\n", "![alt text](https://miro.medium.com/max/1050/1*dvv6ffYSxqTtd9wTB3TeKw.png)\n", "\n", "\n", "\n", "![alt text](https://miro.medium.com/max/1050/1*TQPtYle_1fHLVXvbpJICBA.png)\n", "\n", "\n", "---" ] } ] }