2-python
2-python
• Web Scraping
https://data-flair.training/blogs/python-vs-java/
4
PYTHON VS JAVA KEY DIFFERENCES
• Execution is immediate
• Multi-line comments can begin with """ and end with """
(/* and */ in Java)
• Example:
# initialize foo to 1
foo = 1
7
CODING EXAMPLE
10
CODING EXAMPLE
if (x < 5) { if x < 5:
// call do1 # call do1
do1();
} else if (x < 10) {
do1()
elif x < 10:
Indentation matters
// call do2 # call do2 - it determines the
do2(); do2()
} else { else: scope!
// call do3 # call do3
do3(); do3()
}
12
FOR
Java Python
15
METHODS / FUNCTIONS
Java Python
Major differences:
• Function DOES NOT need to occur in class
• Return type and input types are not declared
• Indentation defines body of function
16
CLASSES
Java (Foo.java) Python (Foo.py)
public class Foo { class Foo:
double a; // class attribute a = None # class attribute
// Constructor # Constructor
public Foo(double a) { def __init__(self, a):
this.a = a; self.a = a
}
# Method called bar
// Method called bar def bar(self, b):
double bar(double b) { return self.a * b
return a * b;
} def main():
} foo = new Foo(5.0)
print(foo.bar(2.4))
public static void main(String[] args) {
Foo foo = new Foo(5.0); if __name__ == “__main__”:
System.out.println(foo.bar(2.4)); main()
}
17
IN-CLASS EXERCISE #2
• Download exercise2.py from Canvas ->
Files -> Exercises -> Ex2
• Fill in the 5 functions by following the
specifications in the multi-line comments
(""" à """)
• Submit to Gradescope (Exercise #2)
• Closes 15 minutes after class ends today
18
GRADESCOPE AUTOGRADER TIPS
• Public tests will give you an idea of whether your output format is
correct, logic correctness is generally not exposed until after grading
19
EXERCISE #2: ADD_TWO
EXERCISE #2: CONVERT_C_TO_F
EXERCISE #2: SUM_LIST
EXERCISE #2: SUM_DICT
EXERCISE #2: IS_PRIME
HOMEWORK #1 ANNOUNCEMENT
• 4 questions