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:
Built-In
Global
Enclosing
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)
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.
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
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
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?
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
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 )
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?