{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOxdKJDid8uDS5AetF2yyFY",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# **Python variables**\n",
"Python variables are symbolic names that act as containers for storing data values in a computer's memory. They allow you to refer to data by a meaningful name instead of its raw value, making code more readable and maintainable.\n",
"\n",
"Key characteristics of Python variables:\n",
"Assignment: Variables are created the moment a value is assigned to them using the assignment operator (=)."
],
"metadata": {
"id": "Czpi-DR2i7fM"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SryKrZ_ci1sv"
},
"outputs": [],
"source": [
" my_variable = 10\n",
" name = \"Alice\"\n",
" is_active = True"
]
},
{
"cell_type": "markdown",
"source": [
"# **Dynamic Typing: **\n",
"Python is dynamically typed, meaning you do not explicitly declare the data type of a variable. The interpreter infers the type based on the assigned value. A variable's type can also change during program execution if a new value of a different type is assigned."
],
"metadata": {
"id": "oNhHoj3ljAqz"
}
},
{
"cell_type": "code",
"source": [
" x = 5 # x is an integer\n",
" x = \"hello\" # x is now a string"
],
"metadata": {
"id": "OD14ZQE0jAx2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**No Declaration Required:** Unlike some other programming languages, Python does not require a separate declaration step before assigning a value to a variable.\n",
"\n",
"**Case-Sensitivity:** Variable names are case-sensitive. myVariable and myvariable are treated as two distinct variables.\n",
"\n",
"**Naming Conventions:**\n",
"Variable names must start with a letter or an underscore (_).\n",
"They can contain letters, numbers, and underscores.\n",
"Keywords (reserved words like if, for, while) cannot be used as variable names.\n",
"\n",
"Common practice in Python is to use snake_case (e.g., user_age, total_score).\n",
"\n",
"**Scope: **\n",
"Variables can have different scopes (local or global), determining where they can be accessed in the program. Local variables exist only within the function where they are defined, while global variables are accessible throughout the entire program.\n",
"\n",
"**Mutability**: Variables can store mutable (e.g., lists, dictionaries) or immutable (e.g., numbers, strings, tuples) data types. When a mutable variable's value is changed, the object it refers to is modified in place. When an immutable variable's value is changed, a new object is created, and the variable is made to refer to this new object."
],
"metadata": {
"id": "ibQa3Ie7jA4O"
}
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "t0O3cm7ijA-l"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Creating Variables**\n",
"Python has no command for declaring a variable.\n",
"\n",
"A variable is created the moment you first assign a value to it."
],
"metadata": {
"id": "L7d_6bLOjBI-"
}
},
{
"cell_type": "code",
"source": [
"x = 5\n",
"y = \"John\"\n",
"print(x)\n",
"print(y)"
],
"metadata": {
"id": "LbDY_7FIjBQm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Casting**\n",
"If you want to specify the data type of a variable, this can be done with casting."
],
"metadata": {
"id": "JHf2yGR3jBXG"
}
},
{
"cell_type": "code",
"source": [
"x = str(3) # x will be '3'\n",
"y = int(3) # y will be 3\n",
"z = float(3) # z will be 3.0"
],
"metadata": {
"id": "T8H5xtC5jBdW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Get the Type**\n",
"You can get the data type of a variable with the type() function."
],
"metadata": {
"id": "SuBXJUcOjBkG"
}
},
{
"cell_type": "code",
"source": [
"x = 5\n",
"y = \"John\"\n",
"print(type(x))\n",
"print(type(y))"
],
"metadata": {
"id": "DL1uJ4Q6jBq3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Single or Double Quotes?**\n",
"String variables can be declared either by using single or double quotes:"
],
"metadata": {
"id": "cgAbISLwjBx3"
}
},
{
"cell_type": "code",
"source": [
"x = \"John\"\n",
"# is the same as\n",
"x = 'John'"
],
"metadata": {
"id": "J4BOADl-jB72"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Case-Sensitive**\n",
"Variable names are case-sensitive."
],
"metadata": {
"id": "5fVtko9fjCDG"
}
},
{
"cell_type": "code",
"source": [
"a = 4\n",
"A = \"Sally\"\n",
"#A will not overwrite a"
],
"metadata": {
"id": "NlodAVXxjCJ-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Python Variables**\n",
"\n",
"In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value.\n",
"\n",
"Unlike Java and many other languages, Python variables do not require explicit declaration of type.\n",
"The type of the variable is inferred based on the value assigned."
],
"metadata": {
"id": "hNRSoaaTj78U"
}
},
{
"cell_type": "code",
"source": [
"# Variable 'x' stores the integer value 5\n",
"x = 5\n",
"\n",
"# Variable 'name' stores the string \"Samantha\"\n",
"name = \"Samantha\"\n",
"\n",
"print(x)\n",
"print(name)"
],
"metadata": {
"id": "EtHz9AbVj8Ft"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Rules for Naming Variables**\n",
"To use variables effectively, we must follow Python’s naming rules:\n",
"\n",
"Variable names can only contain letters, digits and underscores (_).\n",
"A variable name cannot start with a digit.\n",
"Variable names are case-sensitive (myVar and myvar are different).\n",
"Avoid using Python keywords (e.g., if, else, for) as variable names."
],
"metadata": {
"id": "IGzrKG5jj8M0"
}
},
{
"cell_type": "code",
"source": [
"age = 21\n",
"_colour = \"lilac\"\n",
"total_score = 90"
],
"metadata": {
"id": "-eqc070Cj8TL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Assigning Values to Variables**\n",
"Basic Assignment\n",
"Variables in Python are assigned values using the = operator."
],
"metadata": {
"id": "BeQj-OQIj8aU"
}
},
{
"cell_type": "code",
"source": [
"x = 5\n",
"y = 3.14\n",
"z = \"Hi\""
],
"metadata": {
"id": "DXDIqBmzj8hU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Dynamic Typing**\n",
"Python variables are dynamically typed, meaning the same variable can hold different types of values during execution."
],
"metadata": {
"id": "WhzUjOyyj8sb"
}
},
{
"cell_type": "code",
"source": [
"x = 10\n",
"x = \"Now a string\""
],
"metadata": {
"id": "llF8UFudj8z0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Multiple Assignments**\n",
"Python allows multiple variables to be assigned values in a single line.\n",
"\n",
"Assigning the Same Value\n",
"Python allows assigning the same value to multiple variables in a single line, which can be useful for initializing variables with the same value."
],
"metadata": {
"id": "ssqOeTaxj89c"
}
},
{
"cell_type": "code",
"source": [
"a = b = c = 100\n",
"print(a, b, c)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M2GHrpcbj9ET",
"outputId": "129e5651-67fb-45c7-c913-756b334a9842"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"100 100 100\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Assigning Different Values**\n",
"We can assign different values to multiple variables simultaneously, making the code concise and easier to read."
],
"metadata": {
"id": "6SH__ht5j9L7"
}
},
{
"cell_type": "code",
"source": [
"x, y, z = 1, 2.5, \"Python\"\n",
"print(x, y, z)"
],
"metadata": {
"id": "H7VeSHBsj9SU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Type Casting a Variable**\n",
"Type casting refers to the process of converting the value of one data type into another. Python provides several built-in functions to facilitate casting, including int(), float() and str() among others.\n",
"\n",
"Basic Casting Functions\n",
"int() - Converts compatible values to an integer.\n",
"float() - Transforms values into floating-point numbers.\n",
"str() - Converts any data type into a string."
],
"metadata": {
"id": "1xfgo9Z1j9Y1"
}
},
{
"cell_type": "code",
"source": [
"# Casting variables\n",
"s = \"10\" # Initially a string\n",
"n = int(s) # Cast string to integer\n",
"cnt = 5\n",
"f = float(cnt) # Cast integer to float\n",
"age = 25\n",
"s2 = str(age) # Cast integer to string\n",
"\n",
"# Display results\n",
"print(n)\n",
"print(f)\n",
"print(s2)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QwViE-U5j9ej",
"outputId": "9f777459-3c55-4ef2-b437-aa03953c7a84"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"10\n",
"5.0\n",
"25\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Getting the Type of Variable**\n",
"In Python, we can determine the type of a variable using the type() function. This built-in function returns the type of the object passed to it."
],
"metadata": {
"id": "eoZNVfqSj9lb"
}
},
{
"cell_type": "code",
"source": [
"# Define variables with different data types\n",
"n = 42\n",
"f = 3.14\n",
"s = \"Hello, World!\"\n",
"li = [1, 2, 3]\n",
"d = {'key': 'value'}\n",
"bool = True\n",
"\n",
"# Get and print the type of each variable\n",
"print(type(n))\n",
"print(type(f))\n",
"print(type(s))\n",
"print(type(li))\n",
"print(type(d))\n",
"print(type(bool))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Eosj7x1Jj9rD",
"outputId": "e5a6d62f-2dba-4197-a64f-6485e5ae93b2"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Delete a Variable Using del Keyword**\n",
"We can remove a variable from the namespace using the del keyword. This effectively deletes the variable and frees up the memory it was using.\n",
"\n",
"del x removes the variable x from memory.\n",
"\n",
"After deletion, trying to access the variable x results in a NameError, indicating that the variable no longer exists."
],
"metadata": {
"id": "b2_3Sj0bk3a_"
}
},
{
"cell_type": "code",
"source": [
"# Assigning value to variable\n",
"x = 10\n",
"print(x)\n",
"\n",
"# Removing the variable using del\n",
"del x\n",
"\n",
"# Trying to print x after deletion will raise an error\n",
"# print(x) # Uncommenting this line will raise NameError: name 'x' is not defined"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "utnh2Dhhk3jw",
"outputId": "aa2ee668-8829-4a7d-eaa9-a9c9726671bc"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"10\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "6rgHUu5Zk41Q"
},
"execution_count": null,
"outputs": []
}
]
}