Introduction to programming in Python and JavaScript - Issue#4

in #utopian-io7 years ago (edited)

Previous post of this article :-

Python-JavaScript.jpg
image Source

4.2.2 The for command

For means for. This command depends on a variable that is initialized at the beginning of the order. Then, as long as a certain condition is true, we execute the code, then the variable is incremented.

JavaScript Code -
// in this for loop, the variable used will be called k
for (k = start value, k <end value, k = k + increment)
{
// code that will be executed as long as
// the variable k did not reach the end value
// the value of k can be used in this code
// at each pass, the value of k increases by increment
};
Python Code -
## in this for loop, the variable used will be called k
for k in range (start value, end value, increment):
## the indented code will be executed as long as
## the variable k did not reach the end value
## the value of k can be used in this code
## at each pass, the value of k increases by increment
Example -

Here is another way to construct the sum of the numbers from 1 to 10, excluding 10.

sum = 0
for (k = 1; k <10; k = k + 1)
{
sum = sum + k
}
document.write ("the sum is worth" + sum)
sum = 0
for k in range (1,10,1):
sum = sum + k
print ("the sum is worth", sum)

5 . The functions


A function is a piece of code, possibly depending on certain variables,which can be executed directly using his name. This allows -
1 . not to re-type code snippets that would be very similar;
2 . to clearly break down the main program into several parts.
The functions can be made available to other programs, or even other programmers. Their name must begin with a letter, no accented letter or special character is allowed, except "_". Be careful to distinguish the capital letters tiny ones.

JavaScript Code -
function functionName (argument1, argument2, ...)
{
// lines of code
return output // optional command:
// the contents of the output variable
// can be used directly
}
Python Code -
def functionName (argument1, argument2, ...):
## lines of code that must be indented
return output ## optional command:
## the contents of the output variable
## can be directly used

If the function has no arguments, it must still have parentheses.

A first example-

Here is a function without arguments that displays an alert message.

function danger ()
{
document.write ("danger function executed")
}
def danger ():
print ("danger function performed")

We note that parentheses must be present, even if there is no argument inside.The only way to use this feature is to call it directly to display the message on the screen.

danger () will print the message "danger function executed" on the screen.

A second example -

Here is a function that returns three times the number passed as an argument.

function triple(x)
{
t=3*x
return t
}
def triple(x) :
t=3*x
return t

In the code above, the variable t is a local variable: it is created during the execution of the function and destroyed as soon as the function has finished its execution.
Here are two uses of the triple function if x is a variable to which a value is already assigned:
1 . result = triple (x) will store the triple of the contents of the variable x in the variable result.
2 . document.write (triple (x)) (JavaScript) or print (triple (x)) (Python) will print triple the contents of the variable x on the screen.

Basic rule for function arguments -

Unless you're doing advanced programming, you do not want to change the arguments passed into parameters in a function. It may be useful to assign the values ​​of parameters in local variables.


6 . Importing functions or plugins


The scripts can be put in a separate file as follows. The advantage this way of proceeding is that the script can be used in other scripts of simple and effective way. This simplifies the reading of the scripts and it also makes it possible to change only one file in case the code is improved!

JavaScript Code -

The original file is an HTML file. Scripts are stored directly in JavaScript
apart: this avoids using HTML tags whenever you program
in JavaScript.

Here is the external file.js: document.write ("this script is external").
Here is the HTML code: -

<html>
<body>
<script src="externe.js"></script>
</body>
</html>
Python Code -

To tell Python to use the code in the external.py file, it simply insert the following line at the beginning of the main program.

from external import *


7 . Interaction with the user


A program often requires information from the user. The Next code (JavaScript on the left and Python on the right) asks the user a question.
The answer to this question will be stored as a string in the variable answer.

reponse = prompt("question")reponse = raw_input("phrase")

If we want to transform the answer into an integer, we use the commands -

parseInt(reponse)(in JavaScript)int(reponse)(en Python)

If you want to transform the response into a floating-point number, you use the orders-

parseFloat (reponse) (in JavaScript)float (reponse) (in Python).
Discussion Continued in next post...........

Thanks For Reading



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Sorry, your contribution cannot be approved because it does not follow the Utopian Rules.

  • Formatted with "X thing for Y purpose" (buzzfeed style) should be only accepted if the post gives detailed content on each listed open-source project/application.
  • Seems more like tutorial category

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

What is X thing for Y purpose" (buzzfeed style)?

Hey @missaelcastillo, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Upvote and Follow you. Follow me and upvote all my posts. Comment 'something' on any of my post and i'll do the same. Come lets earn together @byltc