Docsity

1. Java Versus Python 1. Hello World Java Python, Lecture notes of Artificial Intelligence

Toggle sidebar
bg1
CSL-411: Artificial Intelligence Lab
BS(CS)
Semester 06 (Spring 2022)
Lab 02: Introduction to Python Programming
Introduction to :
1. Java Versus Python (Side by Side Comparison)
2. Control Structures(if, if else, switch, for, while and do while)
3. Arrays
4. Functions and Recursion
1.Java Versus Python
1. Hello World
Java Python
public class HelloWorld{ print("Hello world!")
public static void main(String[] args){
System.out.println("Hello world!");}
}
2. Statements
Java Python
Statements in Java always end with a
Python is line-oriented: statements end at the end
semicolon (;). It is possible for a statement to
of a line unless the line break is explicitly
run over more than one line, or to have
escaped with \. There is no way to put more than
multiple statements on a single line. Examples:
one statement on a single line. Examples:
this is a statement; this is a statement
this is another statement; this is another statement
this is a long statement that this is a long statement that\
extends over more than one line; extends over more than one line
3. Use of Variables
Java Python
Java is statically typed: Python is dynamically typed:
int someVariable; someVariable = 42
int someVariable = 42;
• A variable that has been declared to be of a
• A variable that has been assigned a value of
pf3
pf4
pf5

Partial preview of the text

Download 1. Java Versus Python 1. Hello World Java Python and more Lecture notes Artificial Intelligence in PDF only on Docsity!

CSL-411: Artificial Intelligence Lab

BS(CS)

Semester 06 (Spring 2022)

Lab 02: Introduction to Python Programming

Introduction to :

  1. Java Versus Python (Side by Side Comparison)
  2. Control Structures( if, if else, switch, for, while and do while )
  3. Arrays
  4. Functions and Recursion

1.Java Versus Python

  1. Hello World

Java Python

public class HelloWorld{ print("Hello world!")

public static void main(String[] args){

System.out.println("Hello world!");}

  1. Statements

Java Python

Statements in Java always end with a Python is line-oriented: statements end at the end

semicolon (;). It is possible for a statement to of a line unless the line break is explicitly

run over more than one line, or to have escaped with . There is no way to put more than

multiple statements on a single line. Examples: one statement on a single line. Examples:

this is a statement; this is a statement

this is another statement; this is another statement

this is a long statement that this is a long statement that\

extends over more than one line; extends over more than one line

  1. Use of Variables

Java Python

Java is statically typed: Python is dynamically typed:

int someVariable; someVariable = 42

int someVariable = 42;

  • A variable that has been declared to be of a • A variable that has been assigned a value of

particular type may not be assigned a value of a given type may later be assigned a value of

a different type. a different type.

someVariable = 42

/ Divide x / y / Divide x / y

% Modulus x % y % Modulus x % y

(remainder of (remainder of x/

x/y) y)

// Floor division x // y

** Exponent x**y (x to the

power y)

  1. Comparison Operators

Java Python

Operato Meaning Exampl Operato Meaning Exampl

r e r e

Greater that x > y > Greater that x > y

< Less that x < y < Less that x < y

== Equal to x == y == Equal to x == y

!= Not equal to x != y != Not equal to x != y

= Greater than or x >= y >= Greater than or equal x >= y

equal <= Less than or equal to x <= y

<= Less than or equal to x <= y

  1. Logical Operators

Java Python

Operato Meaning Exampl Operato Meaning Exampl

r e r e

&& True if both the x && y and True if both the x and y

operands are true operands are true

|| True if either of the x || y or True if either of the x or y

operands is true operands is true

! True if operand is! x not True if operand is not x

false (complements false (complements

the operand) the operand)

  1. Assignment Operators

Java Python

Operato Example Equivatent to Operato Example Equivatent to

r r

= x = 5 x = 5 = x = 5 x = 5

+= x += 5 x = x + 5 += x += 5 x = x + 5

-= x -= 5 x = x - 5 -= x -= 5 x = x - 5

*= x *= 5 x = x * 5 *= x *= 5 x = x * 5

/= x /= 5 x = x / 5 /= x /= 5 x = x / 5

%= x %= 5 x = x % 5 %= x %= 5 x = x % 5

      • //= x //= 5 x = x // 5
      • **= x **= 5 x = x ** 5

&= x &= 5 x = x & 5 &= x &= 5 x = x & 5

|= x |= 5 x = x | 5 |= x |= 5 x = x | 5

^= x ^= 5 x = x ^ 5 ^= x ^= 5 x = x ^ 5

= x >>= 5 x = x >> 5 >>= x >>= 5 x = x >> 5

<<= x <<= 5 x = x << 5 <<= x <<= 5 x = x << 5

  1. Conditional Statements

Java Python

IF-Statement IF-Statement

if (x < 0) { if x < 0:

something; something

IF-Else Statement IF-Else Statement

if (x < 0) { if x < 0:

something; something

} else { else:

somethingdifferent; somethingdifferent

IF-Else If Statement IF-elif Statement

if (x < 0) { if x < 0:

something;

something

} elseif (x > 0) {

elifx > 0:

somethingdifferent;

somethingdifferent

} else {

else: yetanother thing;

} yetanother thing

  1. Switch Statement

Java Python

switch(expression){ Not availabe in python.

case 1: dosomething();break;

case 2: dosomething();break;

default: dosomething()

  1. Loops

Java Python

For Loop For Loop

for(int i = 1; i < 10; i ++){ for i in range(1, 10):

something; something

} something

(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9) (i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)

for (int i = 1; i < 10; i += 2){ for i in range(1, 10, 2):

System.out.print(i); print(i)

[m:n:step] (exclusive) with an s[3:] ⇒ 'lo'

optional step size. s[:‐2] ⇒

Default m is 0, n is

'Hel'

len‐1.

s[0:5:2] ⇒

'Hlo'

s.index(“o”)

Obj.index(str Returns the index ⇒ 4

) of string