# Variables

## Comments in Python

* Comment types (inline comment and multi-line)

```python
# This is inline comment in python

"""
This is also multiline comment

"""

'''

This is multiline comment
So, all of it within triple quoted part 
will be treated as a commented code

We also use this multi-line comment for variable declaration

'''
```

## Namespaces in Python

A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a [dictionary](https://realpython.com/python-dicts) in which the keys are the object names and the values are the objects themselves. Each key-value pair maps a name to its corresponding object.

In a Python program, there are four types of namespaces:

1. Built-In
2. Global
3. Enclosing
4. Local

These have differing lifetimes. As Python executes a program, it creates namespaces as necessary and deletes them when they’re no longer needed. Typically, many namespaces will exist at any given time.

<https://realpython.com/python-namespaces-scope/>

## Variables

* Variables can **store data of different types**, and different types can do different things
* We have **3 primitive data types**:&#x20;
  * Text (String)
  * Numerical (Integer, Float, Complex Number)
  * Boolean
* We have **3 secondary data types**:
  * Sequence (List, Tuple, Range)
  * Set
  * Mapping (Dictionary)

{% hint style="success" %}
Type-checking can be done at two stages -

* **Static -** Data Types are checked before execution.
* **Dynamic -** Data Types are checked during execution.

Python is an interpreted language; executes each statement line by line and thus type-checking is done on the fly, during execution. Hence, Python is a Dynamically Typed Language.
{% endhint %}

## Declaring a Variable

In the below discussion, if you don't understand the meaning of the type of variable, read this section again after going through Primitive Data Types and Secondary Data Types Section

{% hint style="success" %}

* It can only begin with an underscore or a character from A-Z or a-z.

* The rest of it can contain anything from the following: A-Z/a-z/\_/0-9.

* Python is case-sensitive, as we discussed in the previous question.

* Keywords cannot be used as identifiers. Python has many keywords as reserved ones, which can not be used to declare a variable
  {% endhint %}

* While assigning values to any variable, no need to declare a variable in advance

* Reassigning a different type to the same variable is possible&#x20;

```python
test_variable = 100 # without pre-declaration, we are assigning the variable
print(test_variable) # it will print 100
test_variable = "string" # re-assigning test_variable to string from int
print(test_variable) # it will print "string"
test_variable = True # re-assigning to Boolean
```

{% hint style="success" %}

#### Variable Names - How to write cleaner code?

Make sure that the variable names are descriptive and meaningful because it makes our code more maintainable

* It is good practice to name the variables in lower case with an underscore (Snake case)
* Put a space around "=" sign to make code more readable
* Write descriptive and relevant variable (and function) names
  {% endhint %}

{% hint style="success" %}
**Declaring variables**

* Camel Case (example: "camelCase" as variable name for camel case )

* Pascal Case (example: "PascalCase" for pascal case )

* Snake Case (example: "snake\_case" for snake case )
  {% endhint %}

* It is possible to check the type of any variable

```python
type(test_variable) # returns the type of it at the moment
```

* The print statement works to print (especially very important in debugging the code)

  \*debug = determine the cause of an error

```python
print(test_variable) # print the test_variable value
print(type(test_variable)) # it will print the variable data type
```

* Possible to take input from users

```python
x = input() # it will take input from user, type of variable will be string
```

* 5 and '5' are not equal or same, 5 is of integer type and '5' is of string type
* Variable type casting is possible (changing the variable type from one another)

```python
print(type(x)) # x taken as input will be of type string, assume input is 5

# here will be '5' (string type), we can type cast it to integer
x = int(x) # re-assigned x to 5 (which is int now)
x = bool(x) # will return True now

# type casting will happen only if it is possible, otherwise it will throw an error
y = 'string'
int(y) # it will throw an error since it can not be converted

```

## Reference For Further Reading

### w3 Resource

{% embed url="<https://www.w3resource.com/python/cgi-programming.php>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ankit-apdc.gitbook.io/python-1/part-1-basics/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
