Introduction to programming in Python and JavaScript - Issue#3

in #utopian-io6 years ago (edited)

Previous post of this article :-

Python-JavaScript.jpg
image Source

3.4 Lists (also called tables)

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 -

JavaScriptPython
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.


3.5 Comparison Operators and Boolean Variables

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).

NameJavaScriptPython
is equal toa == ba == b
is not equal toa != ba != b
is taller thana > ba > b
is smaller thana < ba < b
is bigger or equal toa >= ba >= b
is smaller or equal toa <= ba <= b
Logical ANDa && ba and b
OR logicala / ba or b
negation (NOT)!anot a
The "exclusive or"

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 ^.

Special cases

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 structures


In 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.


4.1 The command "if. . ., so . . . "

4.1.1 Version simple

if (condition) then (block of instructions).

Code JavaScript -
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
}
Code Python -
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"
Example:-
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")
4.1.2 Usual version

if (condition) then (1st block of instructions), otherwise (2nd block of instructions).

Code JavaScript
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
}
Code Python
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
Example:-
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")
4.1.3 Sophisticated version

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).

Code JavaScript:-
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
}
Code Python
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
Example in JavaScript -
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")
Example in Python -
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")

4.2 The loops

The loops are used to execute several instructions a certain number of times,predefined or not.

4.2.1 The while command

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.

JavaScript Code -
while (condition) // mandatory parentheses
{
// code that will be executed as long as the condition is true
}
Python Code -
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"
Example

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...........

Thanks For Reading



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved yet. See the Utopian Rules. Please edit your contribution to reapply for approval.

  • You have two utopian signature. Please remove any one of them
    You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Thanks Sir, For review my post. I do the desire changes as described by you. Please check and verify it again.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @meblogger I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.030
BTC 67655.95
ETH 3799.02
USDT 1.00
SBD 3.53