Differences in syntax between Python and Java

I understand Java a little, but it's been 2 hours since I learned Python. About the difference in syntax etc.

Reference I wrote a class in Python3 and Java

Numbers and letters+And letters"13"Will result in an error


#!/usr/bin/env python
a=1
b=2
print(a+b)

str="3"
print(a+str)

Output result


Traceback (most recent call last):
3
  File "C:/Data/project/201806_python_1/venv/hellloWorld.py", line 7, in <module>
    print(a+str)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

1 and"1"Are not judged to be the same by comparing


#!/usr/bin/env python
i=1
if i==1:
    print("true")
else:
    print("false")

str="1"
if i==str:
    print("true")
else:
    print("false")

Output result


true
false

_! !! important! !! _ It is confusing to accidentally name variables such as _str. str is a built-in function! _

Reference About'==' and'is' in Python, and'==' and'equals' in Java

Reference Java vs Python --Syntactic Difference Championship (Super Incomplete Edition)

python


#!/usr/bin/env python
print("If 0:" + ("true" if 0 else "false"))
print("In case of 1:" + ("true" if 1 else "false"))
print("In case of 2:" + ("true" if 2 else "false"))
print("-In case of 1:" + ("true" if -1 else "false"))
print("Is 0 False?:" + ("true" if 0==False else "false"))
print("Is 1 True?:" + ("true" if 1==True else "false"))

Output result


If 0:false
In case of 1:true
In case of 2:true
-In case of 1:true
Is 0 False?:true
Is 1 True?:true

Python


print(("true" if 0 == 1 else "false"))

Java


System.out.println((0 == 1 ? "true" : "false"));
print((0==0))
print((0==1))

Output result


True
False

Recommended Posts

Differences in syntax between Python and Java
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between java and python (memo)
Difference between list () and [] in Python
Differences between Python, stftime and strptime
I tried to enumerate the differences between java and python
Overlapping regular expressions in Python and Java
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Summary of the differences between PHP and Python
[python] Difference between variables and self. Variables in class
I wrote a class in Python3 and Java
About the difference between "==" and "is" in python
Difference between Ruby and Python in terms of variables
I compared Java and Python!
Calculate Pose and Transform differences in Python with ROS
Ruby and Python syntax ~ branch ~
Stack and Queue in Python
Mutual conversion between JSON and YAML / TOML in Python
Difference between return, return None, and no return description in Python
Unittest and CI in Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and “+” operator
Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java
I tried programming the chi-square test in Python and Java.
Remove leading and trailing whitespace in Python, JavaScript, or Java
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Difference between Ruby and Python split
Detect and process signals in Java.
Differences between Windows and Linux directories
MIDI packages in Python midi and pretty_midi
Differences between yum commands and APT commands
Java and Python basic grammar comparison
View photos in Python and html
Sorting algorithm and implementation in Python
Differences between symbolic links and hard links
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Compiler in Python: PL / 0 syntax tree
Cooperation between python module and API
Check and move directories in Python
Difference between python2 series and python3 series dict.keys ()
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
[Python] Difference between function and method
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
Differences in identity, equivalence, and aliases