



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CSL-411: Artificial Intelligence Lab
BS(CS)
Semester 06 (Spring 2022)
Introduction to :
Java Python
public class HelloWorld{ print("Hello world!")
public static void main(String[] args){
System.out.println("Hello world!");}
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
Java Python
Java is statically typed: Python is dynamically typed:
int someVariable; someVariable = 42
int someVariable = 42;
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)
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
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)
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
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
Java Python
switch(expression){ Not availabe in python.
case 1: dosomething();break;
case 2: dosomething();break;
default: dosomething()
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