Variables

Comments in Python

  • Comment types (inline comment and multi-line)

# 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 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:

    • Text (String)

    • Numerical (Integer, Float, Complex Number)

    • Boolean

  • We have 3 secondary data types:

    • Sequence (List, Tuple, Range)

    • Set

    • Mapping (Dictionary)

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

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

  • Reassigning a different type to the same variable is possible

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

Variable Names - How to write cleaner code?

  • It is possible to check the type of any variable

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

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

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)

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

Last updated

Was this helpful?