3 .The variables in computer science
3 .The variables in computer science
Variables are the key elements of a programming language. We conceive a variable like a drawer containing information. The value of the variable may be called to change. To refer to the variable, we give a name. The variable names obey the following usual rules :-
1 . Their name must begin with a letter, no accented letter or character special is allowed, except "_"
.
2 . You have to pay attention to upper and lower case letters. For example X
and x
are two names of different variables (this constraint is general to any programming HTML).
In JavaScript and Python, a variable is automatically created when he is assigned a value. For example, the order -
variableName = variableValue
assigns the value Variablevalue in the drawer named variableName.
3 .1 The assignment of variables in computer science
Warning, unlike the symbol = in mathematics, in a programming language the symbol = should only be read in one direction. Above, one must understand that the drawer called variableName contains the variableValue object. We must do well difference between the drawer (the name of the variable) and its contents (the value of the variable).
Thus, if in the content, we find the name of a variable, we must think that it is content of this variable. Here is an example that illustrates this phenomenon.
x = 5
x = x + 1
The first line creates the variable x (if it is not already created) and assigns it the value 5. Then the second line takes the value that is in the variable x, adds 1 and assigns the result in the variable x. Here is the mental image of the second line that must be:
Note
The variable assignments are in the computer memory. Unless you use a print command (document.write in JavaScript or print in Python)
, we do not do not see the assignments occur. In Python, there is an exception in the Python window Interpreter, where the x
command displays the value of x
.
3.2 Numeric variables
There are two types of numeric variables in JavaScript and Python: integers and floating-point numbers In Python, there is a third type: long integers (which can be arbitrarily large).
Conversion functions
The parseInt
and parseFloat
functions are JavaScript functions that allow to convert a variable to an integer or floating point respectively. Their Python equivalents are int
and float
.
Reminders about the entire division
When performing the integer division of two integers a and b, we obtain a remain r and a quotient q such that a = q · b + r and 0 6 r <| b |
.
For example, dividing 7 by 2 gives a quotient of 3 and a remainder of 1.
3.2.1 Mathematical Operators
For Python, use the import math command to access all commands below which start with math. (small caps). For JavaScript, it just enter Math. (with capital) before these commands.
Name | Maths | JavaScript | Python 2.6 |
---|---|---|---|
addition | a + b | a + b | a + b |
subtraction | a − b | a - b | a - b |
multiplication | a · b | a * b | a * b |
division | a/b | a / b | float(a) / float(b) |
power | ab | Math.pow(a,b) | math.pow (a, b) or a ** b |
whole part | ent(a) | Math.floor(a) | math.floor(a) |
entire division | |||
· quotient | ⌊ab⌋ | Math.floor(a/b) | a / b |
· rest | a (mod b) | a % b | a % b |
3.2.2 Mathematical functions
JavaScript | Python 2.6 | ||
---|---|---|---|
Math.abs(x) | Math.acos(x) | abs(x) | math.acos(x) |
Math.asin(x) | Math.atan(x) | math.asin(x) | math.atan(x) |
Math.ceil(x) | Math.cos(x) | math.ceil(x) | math.cos(x) |
Math.exp(x) | Math.floor(x) | math.exp(x) | math.floor(x) |
Math.log(x) | Math.pow(x,y) | math.log(x) | math.sin(x) |
Math.round(x) | Math.sin(x) | math.round(x) | math.cos(x) |
Math.round(x) | Math.sin(x) | math.round(x) | math.cos(x) |
Math.sqrt(x) | Math.tan(x) | math.sqrt(x) | math.sqrt(x) |
3.2.3 Mathematical constants Operators
JavaScript | Python 2.6 | ||
---|---|---|---|
Math.PI | Math.E | math.pi | math.e |
3.3 Strings of characters
A variable can be assigned a string of characters. This is a text surrounded quotation marks ("or", without mixing
).
| text = "it's great!" | text = 'This is awesome! |
In JavaScript, we must use the special character \ to display characters that are confusing (\ 'for the apostrophe and \\
for displaying \
). In Python, you also need use \
'for the apostrophe.
3.3.1 Concatenation of strings
The + operator also concatenates two strings. for example "good"
+"day"
gives "hello"
.
In JavaScript, note that 21+ "apples"
gives "21 apples"
because it transforms the number 21 as a string. On the other hand in Python, such an operation causes an error. We must first transform the number 21 as a string using the str function. The command so will str (21) + "apples"
.
3.3.2 Display of accented and special characters
In Python, you have to start the program with the following line, so that the characters accented and special are well recognized.
# -*- coding: cp1252 -*-
# -*- coding: cp1252 -*-
3.3.3 Line breaks and spaces
In JavaScript, we jump to the line using <br />
(or <br>
which is not not the last standard but still working). In JavaScript, when there are several spaces after, only a space is reported (in fact this comes from the HTML language), but non-breaking spaces are added using & nbsp;
(another HTML command).
In Python, a line break is made on the display using \ n
. We can perform a line break in the code using \
. The \ n \
command allows you to jump line in the display and in the code. In addition, spaces in strings are all shown on the display. In Python, the following code -
Hi = "This is a rather long string \ n \
containing several lines \
text (This works \ n the same way in C / C ++). \ n \
Note that blanks at the beginning \ n of line are significant. \ N "
print (Hi)
give this to the display -
This is a rather long chain
containing multiple lines of text (This works
in the same way in C / C ++).
Note that the whites at the beginning
line are significant.
Tutorial Continued in next post.
Thanks For Reading.....................................
Posted on Utopian.io - Rewarding Open Source Contributors
Enjoy the vote and reward!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Your contribution cannot be approved yet, because it is in the wrong category. The correct category for your post is
Blog
. See the Utopian Rules. Please edit your post to use the right category at this link, as shown below:You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @meblogger I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit