Variables
Watch on YouTube | Read the docs
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.
Finding address of a variable.
To find out the address of the variable, we use the id() function.
405911019 is the memory location of the variable i
.
Finding data type of a variable.
type
is a builtin function which returns the data type of the argument passed to it. Read the docs
The type() function takes an argument and returns the data type of the argument.
Taking input from the user.
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).
Exercise
Take the user's name, age and height and print it to the terminal.
Take a number from the user and print it.
Variable types
Numeric
Read the docs.
Integer
Float
Complex
String
Read the docs
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.
Boolean
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.
Read the docs
Sequence type
Read the docs
List
List stores multiple values of any data type.
Tuples
Read the docs
Tuples are lists from which elements can't be added/deleted/modified.
Dictionary
Read the docs
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:
Set
Read the docs
Sets are same as lists, but they don't allow duplicates. Only hashable elements are allowed as their members (int, float, string, complex, tuples).
Immutability
When a data type is said to be immutable, the data type doesn't allow modifications.
Hashing
Hashing is a complicated process, just remember, hashable data types = immutable data types.
Links
Last updated