0
What is a variable.....what is the difference between variable and data type
3 ответов
+ 4
int Carrie=10 //Carrie is a variable with int data type and holds 10.
double pi=3.14 //pi is a variable with double data type and holds 3.14.
https://www.sololearn.com/learn/CPlusPlus/1606/
+ 3
A variable is associated with its type and is only capable of storing values of that type, they work like storage box's specifically made to store a certain type of item, say in real life you tried to store a big microwave in a box that only stores small cups the box would be the variable of item "type smallcups" it has no idea how to store a microwave which is a diffrenet type of item.
+ 3
very good description from D_Stark. I want to add a comment to special variables, that are data structures. compared to variables, data structures can contain more than one value. So instead of using 10 different variables for 10 names, we can use a data structure to hold all of them. Date structures can be lists, tuples, sets or dictionaries. Each of them can contain basic data types like string, int, float as well as other objects like data structures. These can be mixed like shown here:
my_list = [1, 'hello', 27.13, [1, 2, 3], (10, 15), {'a': [100, 255]}]
The advantage of data structures that you can iterate them by using a for loop.