Variables
Last updated
Was this helpful?
Last updated
Was this helpful?
Watch on | Read the
Variables are used to store values in memory. When using variables in Python, we do not have to declare the datatype of the variable. Python is a dynamic typing language, which means that when the interpreter will execute the code, it will figure out the data type of the variables on it's own.
Every variable has these things:
Name: The variable name has to start with any valid unicode letter except those that have special significance like \
or numbers.
Value: The content which is being stored in the variable.
Address: The location of the variable in the memory.
There are restrictions about the variable name,
They can't start with numbers or special characters. We can not name a variable 0b
or \a
.
They can not use reserved keywords (try, catch, if, for, while, except, in among others).
If you want to create a variable i
with the value 1, type this in the interpreter:
This will create a variable i
which:
Has the value 1.
Is of the data type integer.
Is located in some memory location.
To find out the address of the variable, we use the function.
405911019 is the memory location of the variable i
.
The type() function takes an argument and returns the data type of the argument.
At times, we want to take input from the user, we can use the input()
function to do so.
This will take the name of the user as input and store it in variable name
. input
takes a string argument which is the message it should print while taking the input.
By default, input
returns a string. We have to convert a variable from string to integer by using the int()
function. Each data type provides a function for data type conversion, for instance, str()
is used to convert a value from other data type to string.
When we do age = int(age)
, this is not a syntax error, because the interpreter is going to create a new integer variable with the name of age (and delete the old string variable).
Take the user's name, age and height and print it to the terminal.
Take a number from the user and print it.
String validity
The location of the ending quotes doesn't matter, the only thing which matters is that it should exist. The value of a string is in between the starting and ending quotes, either single, double or triple.
'this is a string', "this is a string", "'this is a string", '"this is a string', """ this is 'a string '""", '"""hi'
All are valid strings, they start and end with the same quote. If a string starts and ends with a single quote, then any number of double quotes can be part of the string, the same is true vice versa.
'"python"
and "python''
are invalid strings. Both of them do not start and end with the same quote.
True
and False
are special values in Python3. In previous version of the language, we were allowed to create a variable of name True and False, but now they are reserved.
List stores multiple values of any data type.
Tuples are lists from which elements can't be added/deleted/modified.
Dictionaries are a collection of key value pairs, lists/tuples/sets are indexed sequences, dictionaries aren't.
Here, 'IN' and 'US' are the keys and 'India', 'United States' are the values. The values can be any Python data type, but keys can only be hashable data types (basic data types, int/float/string/complex).
Output:
Sets are same as lists, but they don't allow duplicates. Only hashable elements are allowed as their members (int, float, string, complex, tuples).
When a data type is said to be immutable, the data type doesn't allow modifications.
Hashing is a complicated process, just remember, hashable data types = immutable data types.
Links
type
is a builtin function which returns the data type of the argument passed to it. Read the
Read the .
Read the
Read the
Read the
Read the
Read the
Read the