Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comparing Python and Java

Similar presentations


Presentation on theme: "Comparing Python and Java"— Presentation transcript:

1 Comparing Python and Java
CS102 Dean Zeller and Robert Carver Spring 2018

2 Hello World – Python & Java
print() prints the supplied content to the user via console output. Console output to the user:

3 Data types & Variables Python Java Variables need no declaration.
Variables can change type at any time. All variables must be declared in order to be used. Data type cannot change once declared.

4 Java Data types Reminder: all variables must be defined once and only once. Type Name Kind of Value Memory Used Range of Values byte Integer 1 byte -128 to 127 short 2 bytes -32,768 to 32,768 int 4 bytes -2,147,483,648 to 2,147,483,648 long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 float Floating-point ± × to ± × 10 −45 double ± × to ± × 10 −324 char Single Unicode character All Unicode values from 0 to 65,535 boolean 1 or more bytes True or False

5 Java Data Types Data Types example:

6 Array/List indices start at 0
Arrays Python Java Uses Lists Define an empty list Add elements using append Variable length Uses arrays Arrays must be defined Add elements more complex Length must be specified Array/List indices start at 0 Python Example: Output:

7 Arrays Java - Example Assigns the array slot at the provided index to the given value. Output:

8 Mathematical Expressions
Most operators are the same between Java and Python. Operation Python Java Usage Example Addition + a+b Same Subtraction - a-b Multiplication * a*b Division / a/b Power ** a**b Math.pow(a,b) Parenthesis () 3*(a+b) Assignment = c=a+b

9 Statement Blocks & Indentation
Python Java Uses curly braces {} to denote blocks of code. All lines are ended using semi-colons (;). Uses indentation to denote blocks of code. Blocks start with a colon on the defining line. Python example: Denotes start of block Code Blocks

10 Java Example Blocks are set off by lines without semicolon
Code blocks are enclosed with {} Blocks are set off by lines without semicolon Lines end with a semicolor

11 Loops – While Executes as long as the While condition is met.
Python Java Output:

12 Do-While Loop Similar to a while loop, but the loop is always executed at least once. No Python counterpart. Do Loop Form do { body-statements; } while (test); While Loop Form while (test){ }

13 For Loop – Basic Form Python Java Output:

14 For Loop – Java for(int i = 0; i < 10; i++) { }
Increment: Statement executed every time the loop executes. Initialization: Done once, at beginning. Condition: Boolean condition to continue execution of loop.

15 For Loop – Other Forms Python Java Start, End Start, End, Increment
Output: Loop 1 Loop 2 Loop 3 1 2 3 4 5 5 6 7 8 9 10 20 30 40 50 60 70 80 90

16 For and While Loop Comparison
(initialization) while(condition) { (statements) (increment) } for((initialization),(condition),(increment)) { (statements) }

17 For each loop Python Java
Both Python and Java provide a form of the for-loop in which the only parameter is a list. The loop then executes for each item in the list, using the for-loop variable. Python Java Output: Hello Joe Hello Bob Hello Kathy Output: Hello Joe Hello Bob Hello Kathy

18 Credits Script outline and final editing by Dean Zeller
Content and visual organization by Robert Carver Reference: Java An Introduction to Problem Solving, (8th edition) by Walter Savitch


Download ppt "Comparing Python and Java"

Similar presentations


Ads by Google