Previous post of this article :-
A variable can be assigned a list of elements. This is an enumerationof elements separated by a comma and framed by brackets.
list = ["Alain", "Olivier", "Patrick", "Paul"]
You can display the first item in the list using the command -
| JavaScript | Python |
|---|---|
| document.write(liste[0]) | print(liste[0]) |
Python allows you to create lists of numbers with the range command. Here are three examples of such lists.
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
list2 = [20, 30, 40, 50, 60, 70, 80, 90]
list3 = [99, 97, 95, 93, 91, 89, 87, 85, 83, 81, 79, 77, 75, 73, 71]
In JavaScript, you have to program the range function yourself. In addition, before edit a list, called list, item by element (rather than with brackets) it type the list line = Array (n) where n is the number of items in the list.
A comparison operator associates a proposition with a truth value: (true for true and false for false). This truth value can be assigned to a variable, called Boolean. The negation operator overturns the truth values (true becomes false and vice versa).
| Name | JavaScript | Python |
|---|---|---|
| is equal to | a == b | a == b |
| is not equal to | a != b | a != b |
| is taller than | a > b | a > b |
| is smaller than | a < b | a < b |
| is bigger or equal to | a >= b | a >= b |
| is smaller or equal to | a <= b | a <= b |
| Logical AND | a && b | a and b |
| OR logical | a / b | a or b |
| negation (NOT) | !a | not a |
In JavaScript the XOR (or exclusive) seems missing, but we can use that book 1 (instead of true, but which is considered as such) or 0 (instead of false, but which is considered as such).
In Python, the XOR (or exclusive) is given by the operator ^.
For JavaScript, the test 2 == "2" gives true. By cons 2 === "2" gives false.
For Python, the test 1 == 1.0 gives true. By cons 1 is 1.0 gives false.
4 Control structuresIn programming, one needs to perform different actions depending on the current state of the execution of the program. The control structures are there to solve these difficulties.
if (condition) then (block of instructions).
if (condition) // mandatory parentheses
{
// code that will be executed if the condition is true
// no need to indent (shift right), but
// braces are needed if there is
// several lines of code
}
if condition: ## mandatory double-points
## code that will be executed if the condition is true
## this code must be indented (shifted right)
## as soon as the indentation stops, we leave the "if"
x = 5
if (x == 5)
{
document.write ("x is 5.")
document.write ("If, if!")
}
document.write ("Out of the yew")
x = 5
if x == 5:
print ("x is 5.")
print ("Si, si!")
print ("Out of the yew")
if (condition) then (1st block of instructions), otherwise (2nd block of instructions).
if (condition) // mandatory parentheses
{
// code that will be executed if the condition is true
}
else
{
// code that will be executed if the condition is false
}
if condition: ## mandatory double-points
## code that will be executed if the condition is true
else:
## code that will be executed if the condition is false
x = 5
if (x == 5)
{
document.write ("x is 5.")
}
else
{
document.write ("x is not worth 5.")
}
document.write ("Out of the yew")
x = 5
if x == 5:
print ("x is 5.")
else:
print ("x is not worth 5.")
print ("Out of the yew")
If (C1), then (1st block of instructions), if not (C2), then (2nd block of instructions),
otherwise (3rd block of instructions). We can of course add other conditions "if not", this which does not change the scheme (except to extend it on the right).
if (condition1)
{
// code that will be executed if condition1 is true
}
else if (condition2)
{
// code that will be executed if condition1 is false
// and if condition2 is true
}
else
{
// code that will be executed when all the conditions
// previous are false
}
if condition1:
## code that will be executed if condition1 is true
elif condition2:
## code that will be executed if condition1 is false
## and if condition2 is true
else:
## code that will be executed when all conditions
## previous are false
x = 5
if (x == 5)
{
document.write ("x is 5.")
}
else if (x == 6)
{
document.write ("x is 6.")
}
else
{
document.write ("x is neither 5 nor 6.")
}
document.write ("Out of the yew")
x = 5
if x == 5:
print ("x is 5.")
elif x == 6:
print ("x is 6.")
else:
print ("x is neither 5 nor 6")
print ("Out of the yew")
While means that. So the following command executes the code as long as the condition is true. If initially, the condition is false, then the code is not executed.
while (condition) // mandatory parentheses
{
// code that will be executed as long as the condition is true
}
while condition: ## mandatory double-points
## code that will be executed as long as the condition is true
## this code must be indented (shifted right)
## as soon as the indentation stops, we leave the "while"
In this example, we construct the sum of numbers from 1 to 10, excluding 10.
sum = 0
k = 1
while (k <10)
{
sum = sum + k
k = k + 1
}
document.write ("the sum is worth" + sum)
sum = 0
k = 1
while k <10:
sum = sum + k
k = k + 1
print ("the sum is worth", sum)
Tutorial Continued in next post...........