You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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:"
"# 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",
0 commit comments