Variables
Comments in Python
Comment types (inline comment and multi-line)
Namespaces in Python
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.
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
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
The print statement works to print (especially very important in debugging the code)
*debug = determine the cause of an error
Possible to take input from users
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)
Reference For Further Reading
w3 Resource
Last updated
Was this helpful?