Variables
I have a value where do i store it in my computer?
Well that's where variables come handy.
Variables refer to
- A location in the memory that are reserved to store the values of my code
- A container that holds one value and has a label on it. From our definition we can visualise a variable as a gift box with similar characteristics like a container-like, has a label, stores something etc.
Syntax for declaring a variable in python is:
'variable_name' 'assignment operator' 'value'
name = 'john'
age = 20
option = True
Variables in python have a specific naming convention.
- Use snake_case or CamelCase naming convention. e.g my_name, MyName.
- Variable name can only contain alphanumeric character and underscores [A - Z, 0 - 9 and _].
- Variable name starts with a letter or the underscore character. e.g _name = 'john' or name = 'john'
- Variable name should not be a keyword. e.g in, for, while etc.
- Variable names should meaningful.
If a variable is defined(assigned a value) , trying to use it will give us specifically a NameError.
Multiple assignment in a single line
Supposed we want to define numerous variables using single line of code, we can do it by separating variable names and values with commas.
a, b, c = 1, "alphabet", True
The line above is similar to
a = 1
b = "alphabet"
c = True
Variables are key components for any programming language.
Top comments (0)