Math Operators and Expressions in Python and Java

 
M
o
d
u
l
e
 
7
 
 
P
a
r
t
 
2
 
Operators and Expressions
 
Math operators - Addition
 
In both languages:
integer + integer = addition, resulting in an integer
float + (integer or float) = addition, resulting in a float
string + string = concatenation between two strings
Python:
A number and a string cannot be added
Java:
A number and a string can be added
The operation is concatenation
The result is always a string
 
Math operators - Addition
 
Java
 
Python
 
Math operators - Addition
 
Beware of the order of operations.
Addition and concatenation have the same order of precedence
As such, they are always resolved left to right
 
Math operators - Multiplication
 
In both languages
Multiplying two numbers gives you their product
If both numbers are integers, the result is an integer
If one of the numbers is a float, the result is a float
Python
You can multiply a string by an integer
The result is that string, repeated that many times
print(
“Alice”
 * 3) # prints 
“AliceAliceAlice”
Java
You may not multiply strings with numbers
 
Math operators - Multiplication
 
Java
 
Python
 
Math operators - Division
 
Python
Dividing two numbers always gives you a float
Java
Dividing two integers always gives you an integer, rounded down
Dividing an integer and a float always gives you a float
 
Math operators - Division
 
Java
 
Python
 
Math operators - Power
 
Python
We have a power operator
  
print(2 ** 3) # prints 6
Java
There is no power operator
Must use a Math method
  
System.out.println(Math.pow(2,3)); // prints 6
 
Math operators – Floor division
 
Python
We have a floor division operator
  
print(7 // 2.0) # prints 3.0
Java
There is no floor division operator
Must use a Math method
  
double result = 7 / 2;
  
System.out.println(Math.floor(result)); // prints 3.0
 
Comparison operators
 
Available in both languages
Equality (==)
Inequality (!=)
Greater than (>)
Less than (<)
Greater than or Equal (>=)
Less than or Equal (<=)
 
Not available in Java
in, not in, is, is not
 
Checking strings for equality in Java
 
Do not use == to check if two strings are equal in Java
Use the equals() method instead
 
Logical operators
Slide Note
Embed
Share

Explore the operations like addition, multiplication, division, power, and more in Python and Java. Learn how these operations work with integers, floats, and strings to enhance your understanding of mathematical expressions in programming.

  • Math Operators
  • Expressions
  • Python
  • Java
  • Programming

Uploaded on Jul 02, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Module 7 Module 7 Part 2 Part 2 Operators and Expressions

  2. Math operators - Addition In both languages: integer + integer = addition, resulting in an integer float + (integer or float) = addition, resulting in a float string + string = concatenation between two strings Python: A number and a string cannot be added Java: A number and a string can be added The operation is concatenation The result is always a string

  3. Math operators - Addition Java Python

  4. Math operators - Addition Beware of the order of operations. Addition and concatenation have the same order of precedence As such, they are always resolved left to right

  5. Math operators - Multiplication In both languages Multiplying two numbers gives you their product If both numbers are integers, the result is an integer If one of the numbers is a float, the result is a float Python You can multiply a string by an integer The result is that string, repeated that many times print( Alice * 3) # prints AliceAliceAlice Java You may not multiply strings with numbers

  6. Math operators - Multiplication Java Python

  7. Math operators - Division Python Dividing two numbers always gives you a float Java Dividing two integers always gives you an integer, rounded down Dividing an integer and a float always gives you a float

  8. Math operators - Division Python Java

  9. Math operators - Power Python We have a power operator print(2 ** 3) # prints 6 Java There is no power operator Must use a Math method System.out.println(Math.pow(2,3)); // prints 6

  10. Math operators Floor division Python We have a floor division operator print(7 // 2.0) # prints 3.0 Java There is no floor division operator Must use a Math method double result = 7 / 2; System.out.println(Math.floor(result)); // prints 3.0

  11. Comparison operators Available in both languages Equality (==) Inequality (!=) Greater than (>) Less than (<) Greater than or Equal (>=) Less than or Equal (<=) Not available in Java in, not in, is, is not

  12. Checking strings for equality in Java Do not use == to check if two strings are equal in Java Use the equals() method instead

  13. Logical operators Operator name Python syntax Java Syntax Logical NOT not ! Logical AND and && Logical OR or ||

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#