Comparing Python and Java

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Chapter 2: Using Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 2. Program Construction in Java. 01 Java basics.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Test 2 Review Outline.
Java Programming Fifth Edition
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Lecture 5: Some more Java!
Java for Android is specific
Type Conversion, Constants, and the String Object
MATLAB: Structures and File I/O
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Arrays, For loop While loop Do while loop
Java - Data Types, Variables, and Arrays
An Introduction to Java – Part I, language basics
T. Jumana Abu Shmais – AOU - Riyadh
Sridhar Narayan Java Basics Sridhar Narayan
Code Refresher Test #1 Topics:
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
A LESSON IN LOOPING What is a loop?
Arrays in Java.
PROGRAM FLOWCHART Iteration Statements.
Peer Instruction 4 Control Loops.
Unit 3: Variables in Java
Decision making and control functions
Java: Variables, Input and Arrays
Chapter 2 Variables.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Switch, Strings, and ArrayLists in Java
Class code for pythonroom.com cchsp2cs
Presentation transcript:

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

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

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.

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 ±3.40282347 × 10 +38 to ±1.40239846 × 10 −45 double ±1.79763931313486231570 × 10 +308 to ±4.94065645841246544 × 10 −324 char Single Unicode character All Unicode values from 0 to 65,535 boolean 1 or more bytes True or False

Java Data Types Data Types example:

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:

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

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

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

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

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

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){ }

For Loop – Basic Form Python Java Output:

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.

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

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

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

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