Skip to content

Instantly share code, notes, and snippets.

@Mohitsharma44
Created October 14, 2020 00:53
Show Gist options
  • Save Mohitsharma44/250003b728a4759b2401110c28f94752 to your computer and use it in GitHub Desktop.
Save Mohitsharma44/250003b728a4759b2401110c28f94752 to your computer and use it in GitHub Desktop.
test
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"As the name implies, a variable is something that can change. A variable is just a way of referring to a memory location used by a Python program. Based on the datatype of the variable, the Python interpreter allocates the memory and decides what can be stored in the reserved memory. This makes Python a dynamically-typed language\n",
"\n",
"If you are familiar with other programming languages like C, C++ or Java it might be tempting to consider variable as just a container to store data. However in Python, you can at best, think of variables as pointers. This is why you can dynamically change the type of data that a variable is pointing at. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assigning values to variables\n",
"\n",
"One of the main differences between Python and strongly typed languages like C++ or Java is the way it deals with the data types. In languages like C++ or Java, every variable must have a unique data type (i.e if a variable is of type string it cannot store integers or floats). Moreover, every variable has to be declared before it can be used, thus binding it to the data type that can be stored in it. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when a value is assigned to a variable. This means that a variable that was used to store a string can now be used to store an integer. Try it out.\n",
"\n",
"Do something like this"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am in NYC\n"
]
}
],
"source": [
"var = 'I am in NYC'\n",
"print(var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**var** is a string in the above case.\n",
"\n",
"Well.. don't take our word for it. Let's confirm. If we run the next line,"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we saw in the grid in the 01.00, Type is a built-in function that returns any datatype If we point our variable var to, lets say, an integer, it will return int "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var = 123\n",
"type(var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**So what are the rules of naming a variable?**\n",
"\n",
"Every language has some rules for naming the identifier of variables (aka the variable name). In Python, a valid identifier is a non-empty sequence of characters of any length with:\n",
"\n",
"The start of the character can be an underscore _ or a capital or lowercase letter. However, it is generally recommended to use all uppercase for global variables and all lower case for local variables. The letters following the first letter can be a digit or a string. Python is a case-sensitive language. Therefore, **var** is not equal to **VAR** or **vAr**.\n",
"\n",
"Apart from the above restrictions, Python keywords cannot be used as identifier names. These are:\n",
"\n",
"||||||\n",
"|:-------|:--------|:---------|:--------|:----|\n",
"|and |del |from |not |while|\n",
"|as | elif |global |or |with |\n",
"|assert | else | if |pass |yield|\n",
"|break | except | import |print\n",
"|class | exec | in |raise\n",
"|continue| finally | is |return \n",
"|def | for | lambda |try\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiples\n",
"Python allows you to assign a single value to several variables simultaneously. For example:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"x = y = z = a = 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or even assign different values to different variables:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World 1 2\n"
]
}
],
"source": [
"x, y, z, a = 'Hello', 'World', 1, 2\n",
"print (x,y,z,a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try printing the above variables. Hint: The key to printing lies above!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment