In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python and JavaScript in the use of what is the difference, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
JavaScript and Python are very popular and powerful languages, but they differ in the use of some syntax.
Python VS JavaScript: The difference in application
Python
Due to its power and versatility, Python has become an important tool in almost all scientific applications worldwide. It is a general purpose programming language that supports different programming paradigms.
It is used in a wide range of scientific and professional applications, including data science, artificial intelligence, machine learning, computer science education, computer vision and image processing, medicine, biology, and even astronomy.
Python is also used for Web development, although it is primarily used for back-end development, such as the server-side of applications.
JavaScript
Python can be used to develop the back-end portion of a Web application, but JavaScript can be used to develop both the back-end and the front-end of a Web application.
The front end is the part of the application that users see and interact with. Every time you see or interact with a website or Web application, this part of the functionality is what JavaScript provides "behind the scenes."
Similarly, when you interact with mobile apps, you may use JavaScript because frameworks like React Native allow us to write apps that adapt to different platforms using JavaScript.
JavaScript is so widely used in Web development because it is a versatile language that gives us the tools we need to develop Web application components.
Differences between Python and JavaScript applications
Simply put, from an application perspective, developers use Python for developing scientific applications, while JavaScript is used for Web development and user-facing feature and server development.
Python VS _JavaScript: syntax differences
Now that we know the difference in their use at the application level, let's take a look at what differences there are in their writing and syntax. Below we will pass the differences in the following main elements.
code block
variable definition
variable naming protocol
constant
data type, and value
annotation
Built-in data structures
operator
input/output
conditional statements
For loop and While loop
function
object-oriented programming
Code blocks in Python and JavaScript
Each programming language has its own style for defining code blocks. Let's look at the differences between Python and JavaScript:
How Python defines code blocks
Python relies on indentation to define blocks of code. When a series of consecutive lines of code are indented at the same level, they are considered part of the same block of code.
We use it to define conditions, functions, loops, and all compound statements in Python.
Here are some examples:
Use curly braces to define code blocks in JavaScript
Variable definitions in Python and JavaScript
Assignment statements are one of the most basic statements in any programming language. Let's see how to define a variable and assign values to it in Python and JavaScript.
How to define variables in Python
To define a variable in Python, we write the variable name followed by the equal sign (=) and the value to assign to the variable.
1
=
for example
1
x = 5
How to define variables in JavaScript
The syntax is very similar in JavaScript, but we just need var to precede the variable name with a keyword and end with a semicolon (;).
1
var = ;
When a variable is defined using var, the variable has function scope.
1
var x = 5;
We can also use the keyword let:
1
let = ;
For example:
1
let x = 5;
When variables are defined using let, they have block-level scope. It will only be recognized in the code block that defines it.
Variable naming conventions in Python and JavaScript
Python and JavaScript follow two different variable naming conventions.
How to name variables in Python
Python recommends snake_case.
According to Python Style Guide:
Variable names follow the same conventions as function names.
Function names should be lowercase, with underscores separating words if necessary to improve readability.
Thus, typical variable names in Python are as follows:
1
first_name
How to name variables in JavaScript
In JavaScript, however, we should follow the lowerCamel Case naming style, which starts with a lowercase letter and then starts each new word with an upper-case letter.
JavaScript guidelines for Web documentation according to MDN Article:
For variable names, use lowerCamelCasing, and use concise, easy-to-understand semantic names where appropriate.
Therefore, a typical variable name in JavaScript would look like this:
1
firstName
Constants in Python and JavaScript
Let's talk about constants. A constant is a value that cannot be changed during program execution.
How to define constants in Python
In Python, we rely on naming rules to define constants, because there are no strict rules in the language that prevent changing their values.
According to Python Style Guide:
Constants are usually defined at the module level and written in all caps, with underscores separating words.
This is how we name constants in Python:
1
CONSTANT_NAME
For example:
1
TAX_RATE_PERCENTAGE = 32
Such naming conventions are conventions for themselves and other developers, and the value should not be changed in the program, but technically it can be changed.
How to define constants in JavaScript
In JavaScript, we can define constants that cannot be changed in the program, and variable identifiers cannot be reassigned.
This does not mean that the value itself cannot be changed.
According to this article const MDN Web documentation:
The const declaration creates a read-only reference to a value, which does not mean that the value it owns is immutable--just that variable identifiers cannot be reassigned. For example, in case the content is an object, this means that the content of the object (e.g. its properties) can be changed.
To define constants in JavaScript, we const add the keyword before the variable name:
const TAX_RATE_PERCENTAGE = 32;
If you try to change the value of a constant, you will see this error:
undefined value
In JavaScript, we have a special value that is automatically assigned when we declare a variable without assigning an initial value, which is undefined.
Here are some examples:
multiline comments
In Python, to write a multiline comment, we prepend each line with a #to identify the comment.
In JavaScript, multiline comments start with/* and end with */. All characters between these symbols are considered part of the comment.
List and Array
In Python, Lists are used to store a series of values in the same data structure. It can be modified, indexed and used in programs.
In JavaScript, a data structure that performs similar functions is called an Array.
divisible
Although most arithmetic operators work and are used exactly the same between Python and JavaScript, they differ slightly when it comes to division.
In Python, divide exactly with a double slash (//).
In JavaScript, there is no specific divisible operator. But we can round the result to the nearest whole number by calling Math.floor().
Compare values and types
In Python, we use the == operator to compare two values and their data types for equality.
1
2
3
4
5
6
# Comparing Two Integers
>>> 0 == 0
True
# Comparing Integer to String
>>> 0 == "0"
False
In JavaScript, the == operator works by converting two objects to the same type before comparing them.
If we use JavaScript (0 == "0") to check the result of the previous example's "integer vs. string" comparison, the result is True rather than False because the values are converted to the same data type before the comparison:
In JavaScript, to check whether values and data types are equal, we need to use another operator ===(three equals).
type operator
In Python, to check the type of an object, you can use the type() function.
In JavaScript, we use the typeof operator.
The following prompt will be displayed
Conditional statements in Python and JavaScript
Conditional statements allow us to select the parts of the program that will be executed later based on certain conditions.
if statement
In Python, we rely on indentation to indicate which lines of code are conditional.
In JavaScript, you must enclose conditions in parentheses and code in curly braces. The code should also be indented.
multiple conditional statement
In Python, we write the keyword elif after the condition. After the condition, we write a colon (:), indenting the code one line below.
In JavaScript, we write the keyword Else if followed by a condition (enclosed in parentheses). After the condition ends, we write braces and indent the code inside the braces.
For and While Loops in Python and JavaScript
Let's look at how different types of loops are defined in Python and JavaScript and their main differences.
cycle
The syntax for defining a for loop in Python is relatively simple compared to JavaScript.
In Python, we write the name of the loop variable after the keyword for, the keyword in, and call the range() function, specifying the necessary parameters. Then, we write a colon (:) followed by an indented loop body.
In JavaScript, we have to specify several values explicitly. We start with the keyword for, followed by parentheses. In these brackets, we define the initial value of the loop variable, the conditions under which it must be False to stop the loop, and how to update the variable at each iteration. Then, we create a block of code by writing braces, and within the braces we write the body indentation of the loop.
While loop
In Python, we write the conditional, colon (:) after the keyword while, and then write the body of the loop (indented) on a new line.
In JavaScript, the syntax is very similar. The difference is that we must enclose the condition in parentheses and the loop body in curly brackets.
do.. while loop
In JavaScript, there is also a loop type that Python doesn't have.
This type of loop is called do.. while loop because it performs at least one operation and continues to run True when the condition is.
1
2
3
do {
// Code
} while (condition);
Functions in Python and JavaScript
Functions are important for writing programs that are concise, maintainable, and readable. The syntax in Python and JavaScript is very similar, but let's analyze their main differences:
In Python, we write the name of the function after the keyword def and the argument list in parentheses. After this list, we write a colon (:) and the body of the function (indented).
In JavaScript, the only difference is that we use the function keyword to define functions and put curly braces around the body of the function.
Number of function parameters
In Python, the number of arguments passed to a function call must match the number of arguments defined in the function definition. If not, an exception will occur.
In JavaScript, this is not required because parameters are optional. You can call a function with fewer or more parameters than defined in the function definition. Default parameters are assigned undefined values, and additional parameters can be accessed through the Arguments object.
Constructors and properties
A constructor is a special method that is called when a new instance of a class (a new object) is created. Its main purpose is to initialize the properties of an instance.
In Python, the constructor init (with two leading and trailing underscores) that initializes the new instance is called. This method is called automatically when you create an instance of a class to initialize its properties. Its parameter list defines the values that must be passed to create an instance. The list begins with the first argument of self.
In JavaScript, a constructor method is called, constructor, and it also has a list of arguments.
Methods in Python and JavaScript
In Python, we define a method using the def keyword, its name, and a list of arguments in parentheses. This parameter list begins with parameters and self with a reference to the instance that is calling the method. After this list, we write a colon (:) and indent the body of the method.
In JavaScript, a method is defined by writing its name followed by a list of arguments and curly braces. Within curly brackets, we write the body of the method.
examples
To create instances of classes:
In Python, we write the name of the class and pass parameters in parentheses.
1
my_circle = Circle(5, "Red")
In JavaScript, we need new to add the keyword before the class name.
1
my_circle = new Circle(5, "Red");
Python and JavaScript are both very powerful languages because they have different practical use cases, Python can be used for Web development and a wide range of applications, including scientific uses. JavaScript is primarily used for Web development (front-end and back-end) and mobile app development. Therefore, there is no difference between the two languages.
Although there are some differences in grammar and usage, there is no essential difference in usage and mastery as a development language itself. If there is, it is only the influence brought by the user's own proficiency.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.