Skip to content

Commit 585e6db

Browse files
committed
Created using Colaboratory
1 parent b29c32a commit 585e6db

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Python_Variables.ipynb

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Python Variables.ipynb",
7+
"provenance": [],
8+
"authorship_tag": "ABX9TyNaHyzT8vEu+M5zbjDt87/W",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {
20+
"id": "view-in-github",
21+
"colab_type": "text"
22+
},
23+
"source": [
24+
"<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Python_Variables.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {
30+
"id": "yRgHPumxce8E",
31+
"colab_type": "text"
32+
},
33+
"source": [
34+
"# **Python Variables**\n",
35+
"\n",
36+
"![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",
37+
"\n",
38+
"\n",
39+
"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",
40+
"\n",
41+
"## **Variables**\n",
42+
"\n",
43+
"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",
44+
"\n",
45+
"\n",
46+
"\n",
47+
"> ***A Variable is like a box in the computer’s memory where you can store a single value. — Al Sweigart***\n",
48+
"\n",
49+
"\n",
50+
"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",
51+
"\n",
52+
"\n",
53+
"### **Syntax**\n",
54+
"\n",
55+
"\n",
56+
"The general syntax to create a variable in Python is as shown below:\n",
57+
"\n",
58+
"`variable_name = value`\n",
59+
"\n",
60+
"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",
61+
"\n",
62+
"\n",
63+
"> ***Although it is recommended keeping a very descriptive variable name to improve the readability.***\n",
64+
"\n",
65+
"\n",
66+
"\n",
67+
"### **Rules**\n",
68+
"\n",
69+
"**All set and done, there are some rules that you need to follow while naming a variable:**\n",
70+
"\n",
71+
"* A variable name must start with a ***letter*** or the ***underscore character***\n",
72+
"\n",
73+
"* A variable name cannot start with a ***number***\n",
74+
"\n",
75+
"* A variable name can only contain ***alpha-numeric characters*** and ***underscores***. For example, anything like this is valid: ***A-z, 0–9, and _***\n",
76+
"\n",
77+
"* Variable names are ***case-sensitive*** ***(height, Height***, and ***HEIGHT*** are three different variables names)\n",
78+
"\n",
79+
"### **Example**\n",
80+
"\n",
81+
"Below given is an example to properly initialize a value to a variable:"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"metadata": {
87+
"id": "NQKdREzkdw76",
88+
"colab_type": "code",
89+
"colab": {
90+
"base_uri": "https://localhost:8080/",
91+
"height": 35
92+
},
93+
"outputId": "70b43a3d-c3b4-4846-f786-64b07890bb4d"
94+
},
95+
"source": [
96+
"# This is a valid and good way to assign a value to a variable\n",
97+
"# For example you might want to assign values to variables to calculate the area of a circle\n",
98+
"\n",
99+
"pi = 3.142 # I could have also used \"math\" library (math.pi)\n",
100+
"radius = 5 # Interger value for radius\n",
101+
"area_of_circle = 0 # Used to store the value of area of circle\n",
102+
"\n",
103+
"area_of_circle = pi * (radius) ** 2 # Area = (PI * R^2)\n",
104+
"\n",
105+
"print(\"The area of the circle based on the given data is: \", area_of_circle)"
106+
],
107+
"execution_count": 1,
108+
"outputs": [
109+
{
110+
"output_type": "stream",
111+
"text": [
112+
"The area of the circle based on the given data is: 78.55\n"
113+
],
114+
"name": "stdout"
115+
}
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {
121+
"id": "DIyEuUw8d3aU",
122+
"colab_type": "text"
123+
},
124+
"source": [
125+
"### **Pictorial Example (Skim it quickly)**\n",
126+
"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",
127+
"\n",
128+
"\n",
129+
"![alt text](https://miro.medium.com/max/1050/1*dvv6ffYSxqTtd9wTB3TeKw.png)\n",
130+
"\n",
131+
"\n",
132+
"\n",
133+
"![alt text](https://miro.medium.com/max/1050/1*TQPtYle_1fHLVXvbpJICBA.png)\n",
134+
"\n",
135+
"\n",
136+
"---"
137+
]
138+
}
139+
]
140+
}

0 commit comments

Comments
 (0)