Python and Java are very different when it comes to coding within them. Python is a lot less structured, this file will go over similarities and differences between the two to hopefully help you guys out!
## Hello World
### Python
```python
print("Hello World")
```
### Java
```java
publicclassHelloWorld{
publicstaticvoidmain(String[]args){
System.out.println("Hello World!");
}
}
```
As you can see, Python is a lot simpler as you can just "start" coding without having to do all the surrounding method declarations and things like that.
You *can* have a main method in python, if you'd like, though that tends to be more useful for larger programs that use multiple files. Here's how that's done:
```python
defmain():
print("Hello World")
if__name__=="__main__":
main()
```
## Variable Declarations
Java:
```java
Stringname="Alice";
intage=30;
```
Python:
```python
name="Alice"
age=30
```
Note: In Python, variables are not explicitly typed. The interpreter infers the type dynamically.
## Basic Data Types
Strings: 'hello', "world"
Integers: 42, -1
Floats: 3.14, -0.001
Booleans: True, False
## Control Structures
### If-Else
Java:
```java
if(age>18){
System.out.println("Adult");
}else{
System.out.println("Minor");
}
```
Python:
```python
ifage>18:
print("Adult")
else:
print("Minor")
```
Note: Python uses indentation instead of curly braces to define blocks.
## Loops
For Loop (Java):
```java
for(inti=0;i<5;i++){
System.out.println(i);
}
```
For Loop (Python):
```python
foriinrange(5):
print(i)
```
While Loop (Java):
```java
inti=0;
while(i<5){
System.out.println(i);
i++;
}
```
While Loop (Python):
```python
i=0
whilei<5:
print(i)
i+=1
```
5. Functions
Java:
```java
publicstaticintaddNumbers(inta,intb){
returna+b;
}
```
Python:
```python
defadd_numbers(a,b):
returna+b
```
Note: Python functions start with the def keyword. No need to specify return types.
## Lists and Dictionaries
List (Similar to Java's ArrayList):
```python
numbers=[1,2,3,4,5]
```
Dictionary (Similar to Java's HashMap):
```python
person={"name":"Alice","age":30}
```
## Comments
Java:
```java
// This is a single line comment
/* This is a multi-line comment */
```
Python:
```python
# This is a single line comment
```
Note: Python does not have a native syntax for multi-line comments, but you can surround the comment in quoutes as a workaround