Variables in python can be used without the data types. The type of variable is decided at the run time depending upon the data stored. Variable in python can not be used without assigning the values. In python, a number of bytes allocated for a variable is not fixed. It depends upon the amount of data stored in the variable. In another programming language, the number of bytes for a variable is fixed. As a result of this, we can store the data within the specified range only.
Variable in python | Variable in c, C++, Java |
a = 10 | int a; a =10; |
a = [10,20,30] | int a[3]; |
Rules to name a variable.
- A variable name can either start with an underscore or a character.
- The character in the variable can either be a-z, A-Z or 0-9.
- White-space/wide-space are not allowed in variables.
- Variables names cannot begin with a digit.
- Variables are case sensitive.
- Keyword cannot be used as a variable names.
Valid Variable Names | Invalid Variable Names |
_ramu | 9Ramu, 9ramu |
ramu | Rama Singh(in middle there is a space) |
Ramu | Rama$Singh |
Ramu9 | 876U |
Ram8u | class, for, in, is (keyword in python) |
RAMA, rama (two different variables due to case sensitive property) | 8Ramu9 |
Various data types in python
Data Types | Category | Examples |
int | Integers | a = 10, b = 50, c = 5000000000 |
float | Floating point | a = 10. 002, b = 40.0 c = 80.000001 |
complex | Complex number | a = 2 + 1J (need to give coefficient of imaginary part else error) |
bool | Boolean | True, False |
str | Strings | “Ramu”, “Rama”,devqwuu897@$%” |
list | List or array | [10,’10’,’rama’] (heterogeneous array/list), [10,20,90] (homogeneous list/array) |
dict | Dictionary | {“name”:”Rama”,”age”:18, “course”:”BE”} |
tuple | Tuple | (10,), (10,20,30) |
set | set | {“apple”,”Mango”} |
Contributed by – Devanshu Kumar
Contribute your article – contribute.articles@tech-bloggers.in