
Play Store Application link – Java to Python in 17 Steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Python_step2
Learning Python as a Java developer is like moving from a manual transmission car (Java) to an automatic one (Python). The core concepts remain similar, but the effort required reduces significantly. Let’s break it down step by step.
Python Indentation vs. Java Braces
In Java, code blocks are enclosed within curly braces {}
. In Python, indentation replaces braces to define code structure. Indentation is not just a style preference in Python; it’s mandatory.
Java Example:
if (true) {
System.out.println("Hello, Java!");
}
Python Equivalent:
if True:
print("Hello, Python!")
- Notice there are no braces in Python. Instead, consistent indentation is used to signify blocks.
- The colon
:
is mandatory after control statements likeif
.
Real-world analogy: Think of Java braces as gates and Python indentation as smooth pathways; both guide you, but Python makes it more seamless.
Variables and Data Types (Dynamic Typing)
Java requires explicit declaration of variable types, while Python uses dynamic typing, allowing you to assign any type to a variable without declaring it upfront.
Java Example:
int number = 10;
String name = "Alice";
Python Equivalent:
number = 10 # Integer
name = "Alice" # String
- In Python, the type of a variable is determined at runtime.
- This makes Python more flexible but requires careful handling to avoid type errors.
Real-world analogy: Java is like ordering at a formal restaurant (declare everything), while Python is like grabbing food at a buffet (choose what you need on the go).
Input and Output in Python
Input:
In Java, you typically use Scanner
to read user input. In Python, it’s much simpler with the input()
function.
Java Example:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
Python Equivalent:
name = input("Enter your name: ")
print(f"Hello, {name}!")
- The
input()
function directly reads input as a string. - String interpolation (
f"{name}"
) replaces Java’s+
for concatenation.
Output:
Python uses print()
for output, eliminating the need for format specifiers like %d
or %s
in Java.
Java Example:
System.out.println("The result is: " + 42);
Python Equivalent:
print("The result is:", 42)
Comments in Python
Comments help explain code. Python offers single-line and multi-line comments.
Single-line Comment:
# This is a single-line comment
Multi-line Comment:
"""
This is a multi-line comment.
It can span multiple lines.
"""
Comparison with Java:
- Single-line:
// This is a comment
- Multi-line:
/* This is a comment */
Reserved Keywords in Python
Python, like Java, has reserved keywords that cannot be used as variable names. Examples include if
, else
, for
, and while
.
How to view Python keywords:
import keyword
print(keyword.kwlist)
This will display all the reserved keywords in Python.
Comparison with Java: Both languages share many common keywords, but Python’s list is smaller due to its concise syntax.
Complete Python Program
Here’s a program combining these basics:
# A Python program demonstrating syntax and basics
def main():
# Input and Output
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert input to integer
# Conditional Statement
if age >= 18:
print(f"Hello, {name}! You are an adult.")
else:
print(f"Hello, {name}! You are not yet an adult.")
# Loop Example
print("Here are the numbers 1 to 5:")
for i in range(1, 6):
print(i)
if __name__ == "__main__":
main()
How to Run:
- Save the code in a file named
syntax_basics.py
. - Run it using the terminal:
python syntax_basics.py
Sample Output:
Enter your name: Alice
Enter your age: 20
Hello, Alice! You are an adult.
Here are the numbers 1 to 5:
1
2
3
4
5
This program covers indentation, input/output, variables, and control flow—everything you need to start thinking in Python while leveraging your Java knowledge. Stay tuned for the next tutorial on control structures!