Variable types in Python
Variable: meansvariablein English.
Now, we want you to understand variables as just places that are reserved in memory for storing data while a program is running.
In Python, the programmer is not responsible for defining the types of variables that he defines in his program.
Actually, when you define a variable and put any value in it, the Python interpreter will automatically determine the type of that variable based on the value you assigned to it at run time.
In Python, a value must be assigned to the variable during its definition.
first example
var = 5 # and its value is 5 var Here we have defined a variable named print(var) # var Here we have printed the value of the variable
• We will get the following result when running.
In Python, multiple variables of equal value can be defined simultaneously.
second example
x = y = z = 10 # 10 Here we have defined three variables whose value is
print('x = ', x) # x Here we have printed the value of the variable
print('y = ', y) # y Here we have printed the value of the variable
print('z = ', z) # z Here we have printed the value of the variable
• We will get the following result when running.
y = 10
z = 10
Knowing the type of a variable in Python
To find out the type of any variable you can use the type().
Remember: the type of a variable in Python is not fixed because it changes automatically depending on the type of value it is stored in.
Example
var = 10 # var here we put a number in the variable print(type(var)) # because it is an int number note that its type will be .var here we printed the value type of the variable var = 'harmash' # var here we put text in the variable print(type(var)) # because it is a text str note that its type will be .var here we printed the value type of the variable
• We will get the following result when running.
<class 'str'>
Variable types in Python
There are 7 types of variables in Python :
numbers(Numbers).
texts(Strings).
logical(Booleans).
Arrays that do not have a fixed size are calledLists.
Matrices whose size and values are fixed and immutableTuples.
Arrays do not have a fixed size, and their values cannot be deleted, and new values can be added to themSets.
Tables in which data is stored in a key manner(Keys)and rate(Values)It is said to herDictionaries.
Numbers in Python
When defining a variable and storing a number in it, the Python interpreter will automatically determine the type of that variable based on the type of numeric value that was assigned to it.
If you put an integer in it, it becomes its type int. And if you put a decimal in it,(i.e. contains a comma), becomes his type floatand so on.
Number types in Python are divided into 3 types as shown in the following table.
| Type | use it | Example |
|---|---|---|
int |
Used to store integers. | x = 3 |
float |
Used to store numbers containing a decimal point. | x = 1.5 |
complex |
It is used to store complex numbers(Complex Number)Which is often needed by engineers when performing complex computing operations. Note: Here you must put the letter Jor jimmediately after the number so that the Python interpreter knows that you mean a complex number and not a regular number. |
x = 4J |
In the following example, we have defined three variables, and each variable has a different numeric value in type and value. Then we presented the type of each variable of them.
Example
x = 3 # whose value is an integer, x here we have defined a variable named y = 1.5 # Its value is a decimal number, y here we have defined a variable named z = 4J # Its value is a complex number, z here we have defined a variable named print(type(x)) # x Here we have printed the value type of the variable print(type(y)) # y Here we have printed the value type of the variable print(type(z)) # z Here we have printed the value type of the variable
• We will get the following result when running.
<class 'float'>
<class 'complex'>
note
It is true that the Python interpreter selects value types automatically for you, but this does not mean that you are unable to convert number types to the type that suits you.
You will learn about special functions for dealing with numbers, and you will learn how to convert types of numbers in a special lesson later in this course.
Scripts in Python
To define a text in Python we use a symbol ', a symbol ", or a symbol """.
Is there a difference between these symbols?
As for the symbol 'and the symbol ", there is no difference between them. Either one can be used to define one-line text.
As for the symbol '''and the symbol """, there is no difference between them. Either one can be used to define a large text consisting of several lines.
In the following example, we have defined three variables containing text values. Note that we have defined each variable with a different symbol.
first example
# Here we have defined three variables containing text values
name = 'Mahamad'
job = "Programmer"
message = '''This string that will span across multiple lines. No need to use newline characters for the next lines.
The end of lines within this string is counted as a newline when printed.''''
# Here we display the values of the text variables in an ordered manner
print('Name: ', name)
print('Job: ', job)
print('Message: ', message)
• We will get the following result when running.
Job: Programmer
Message: This string that will span across multiple lines. No need to use newline characters for the next lines.
The end of lines within this string is counted as a newline when printed.
In the following example, we have defined a text that contains the same symbols that are used to identify texts.
second example
# Contains a text value Here we have defined a variable named text = """In this line we print 'single quotations' In this line we print "double quotations" """ # text Here we have shown the value of the variable print(text)
• We will get the following result when running.
In this line we print "double quotations"
note
There are special methods and functions for dealing with texts that we will explain in a special lesson later in this course.
Explanation of Booleans in Python
The type boolis usually used to set logical conditions or to know whether a particular command was successfully executed. When a value
is assigned to a variable, it becomes of type .TrueFalsebool
Technical information
Actually the value Trueis 1 and the value Falseis 0 .
In Python, it is preferable to use zero and one instead of using reserved values Trueand Falsewhen checking the value of a variable or what the function will return.
In the following example, we have defined a variable named checkand given it the value Trueand then used it in the setting of a condition.
Example
# True and its value check. Here we have defined its name
check = True
# True equals check The print command placed here will be executed if the value of the variable is
if check == True:
print('check = True')
# False If true is equal to check, the print command placed here will be executed if the value of the variable is not
else:
print('check = False')
• We will get the following result when running.
note
Instead of the command if check == True:, you could just type if check == 1:or type if check:and get the same result.
data storage inListin python
theListIt is an array of inconsistent size that can store values of different types simultaneously.
In Python we use the symbol [ ]to define an unary array(that is, one-dimensional)It does not have a fixed size.
In the following example we have defined 4 matrices.
first example
A = [] # Here we have defined an empty array B = [10, 20, 30, 40, 50] # Here we have defined an array containing only integers C = ['Mhamad', 'Samer', 'Abdullah'] # Here we have defined an array containing only texts D = [1, 'two', 'three', 4] # Here we have defined an array containing integers and texts
In the following example, we define an array consisting of 4 elements, then give it 4 values, then add a new element to it, then display its values and the number of its elements.
second example
# Here we have defined an array of texts consisting of 4 elements
languages = [str] * 4
# Here we put a value in each element in it
languages[0] = 'Arabic'
languages[1] = 'French'
languages[2] = 'English'
languages[3] = 'Spanish'
# Here we have added a new element to the array
languages.append('German')
# Here we have shown the values of the matrix and the number of its elements
print('Stored languages:', languages)
print('Number of stored languages is:', len(languages))
• We will get the following result when running.
Number of stored languages is: 5
note
You will learn special functions for dealing with theListsThe reason for using them and you will learn many ways to deal with them in a private lesson later in this course.
data storage inTuplein python
theTupleIt is an array of fixed size and immutable values that can store values of different types simultaneously.
In Python we use the symbol ( )to define an unary array(that is, one-dimensional)Its size and values are fixed.
In the following example we have defined 4 matrices.
first example
A = () # Here we have defined an empty array
B = (10, 20, 30, 40, 50) # Here we have defined an array containing only integers
C = ('Mhamad', 'Samer', 'Abdullah') # Here we have defined an array containing only texts
D = (1, 'two', 'three', 4) # Here we have defined an array containing integers and texts
In the following example, we define an array consisting of 4 fixed elements, and then display their values and number of elements.
second example
# Here we have defined an array that has no definite type and consists of 4 elements
languages = ('Arabic', 'French', 'English', 'Spanish')
# Here we have shown the values of the matrix and the number of its elements
print('Stored languages:', languages)
print('Number of stored languages is:', len(languages))
• We will get the following result when running.
Number of stored languages is: 4
note
You will learn special functions for dealing with theTuplesThe reason for using them and you will learn many ways to deal with them in a private lesson later in this course.
data storage inSetin python
theSetAn array that has no fixed size, immutable values, and can store values of different types simultaneously.
In Python we use the symbol { }to define an unary array(that is, one-dimensional)It has no fixed size and its values are immutable.
in matricestheSetThe data is stored randomly and not in the order as they were entered, the reason is that in this type of array no number is givenIndexSpecial for each item.
Also for this reason, you cannot access a specific item inSetDirectly because it basically doesn't have a numberIndex.
In the following example we have defined 4 matrices.
first example
A = {} # Here we have defined an empty array
B = {10, 20, 30, 40, 50} # Here we have defined an array containing only integers
C = {'Mhamad', 'Samer', 'Abdullah'} # Here we have defined an array containing only texts
D = {1, 'two', 'three', 4} # Here we have defined an array containing integers and texts
In the following example, we define an array consisting of 4 fixed elements, and then display their values and number of elements.
second example
# Here we have defined an array that has no definite type and consists of 4 elements
languages = {'Arabic', 'French', 'English', 'Spanish'}
# Here we have shown the values of the matrix and the number of its elements
print('Stored languages:', languages)
print('Number of stored languages is:', len(languages))
• We will get the following result when running.
Number of stored languages is: 4
note
You will learn special functions for dealing with theSetsThe reason for using them and you will learn many ways to deal with them in a private lesson later in this course.
data storage inDictionaryin python
When using theListorTupleYou deal with their elements through the numbers ofIndices.
ideaDictionaryIt is setting a key for each value. It then accesses the value of each element through the private key in it.
so theDictionaryA table in which data is stored in the form of keys(Keys)and rate(Values).
As for the type of data you store inside theDictionaryYou are free to store keys and values of any kind you want.
In Python we use the symbol { }to defineDictionary.
In the following example, we defineDictionaryIt consists of 5 elements, then we display the value of the third element through its private key.
Example
# Consists of 5 dictionary items here we have defined
dictionary = {
1: 'One',
2: 'Tow',
3: 'Three',
4: 'Four',
5: 'Five'
}
# Here we have shown the value of the element with key number 3
print(dictionary[3])
• We will get the following result when running.
note
You will learn special functions for dealing with theDictionariesThe reason for using them and you will learn many ways to deal with them in a private lesson later in this course.
Is everything in Python an object?
The answer is yes. Although we say a variable is an object that contains a numeric or text value, it is actually an object and not a regular variable as it is found in other programming languages.
If you are using a programPyCharmYou notice that when you write the name of any variable and put a dot immediately after it, it shows you that functions or properties in it can be called!
Note the following picture.
So if these variables were not in fact objects then nothing would appear to us when we put the point.