Comments In Python
Comments are a major part of any programming language because they:
It isolates the code that you have written and it will be ignored during the Run process of the program. It is used as an explanation for the idea of the code.
For example, if the programmer has written a program that contains thousands of lines, and needs to make a specific modification, how will he remember at a later time what he has done ?!
Here the comments help us to understand the code by any Python programmer.
We write a comment using # ... or
........ * /
or
(/ * ......... * /)
Variables in Python are a space that is reserved in memory until they are used and given different values.
In most programming languages such as C Sharp, we specify the type of the variable such as
int num = 5;
Meaning that this variable is an integer numeric type that contains numbers only and does not allow to give it a text value, fractions or a date, and it is not allowed to assign a value to it other than integer numbers.
But variables in Python do not specify the type of the variable, we just write the name and the value provided to it
num = 5
The presented value of the variable can be reset and the variable type completely changed
num = "Python"
Try this example:
num = 5
print (“num”)
num = “Python”
print (num)
You will notice that the type of the blocked value of the variable num has changed. First it prints 5 and then prints the word python
How can you identify the type of a specific variable without resorting to printing the value presented to it in the variables in Python?
There is a ready-made function in Python that can recognize the type of variable called type ()
num = 5
print (type (num))
num = "python"
print (type (num))
num = 4J
print (type (num))
The result will be "OutPut" at the beginning. Type 5 will appear, which is an integer numerical variable, then for python, which is a String text variable, and then appears for 4J, which is a complex variable
<class' int’>
<class' str’>
<class 'complex'>
We will find that class str, class int, or class complex has been written, because in the python3 language it defines variables as an object, and this object is due to the class type depending on the value of the variable.
Giving values to more than one variable in the same line
var1, var2, var3 = 1, 2, 3
That is, the values were assigned to the three variables in this way var1 = 1, var2 = 2, var3 = 3, respectively.
How to print more than one variable in the print function at one time?
print (var1, var2, var3)
And the OutPut will be in order 1,2,3
- Switching values for the variables (Swap) = >> by switching the values of two variables
var1, var2 = 1, 2
print (var1, var2)
var1, var2 = var2, var1
print (var1, var2)
The result will be var1 = 2, var = 1 and thus the values of the two variables are swapped.
Concatenation between the variables in the typing statement
var1, var2 = 5, 6
print (var1 + var2)
And the result is 11 because the addition is done inside the print function.
var1, var2 = 5,6
print (“value =” + var1)
The result here will be valued = 5
And this has been an experiment
Concatenate between 2 numbers Combine text and numeric variable concatenate between string and integer
But it is not allowed to combine two variables, one of which is a text and the other is a number, because the sign that is adding (+) is an arithmetic sign. How can you combine a text with a number and so it will show this error
TypeError: can only concatenate str (not “int”) to str
In other words, this example is completely rejected
var1 = 5
var2 = "Value"
print (var2 + var1)
How can we combine a text variable and a numeric variable?
The answer:
We convert the numeric variable to a text variable, and vice versa is not true.
This is done with the str () function
Example:
Age = 18
Name = "ahmed"
Right >> Print (Name + str (age))
wrong >>Print (Name + age)
Important notes to take into account when writing variables in Python
The variable name must begin with a letter or _ The variable name cannot start with a number. Only letters and numbers are allowed and _ in the variable name (0-9, az, _). Python is case-sensitive, meaning that this variable num is not that NUM. Writing must be mindful.
Types of variables in Python:
- integer ..--> var = 5
- float -> var = 12.65
- complex -> var = 4J
- list -> [1, 2, 18, 6] or ['apple', 'berry', 'happy']
- tuple -> tuple ('scorp', 'tech', 8, 44)
- dictionary -> (32, 41,12).