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
The string can be either single quotes or double quotes (String is double, char is single?)
There is a ternary operator, but the writing style is slightly different.
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