Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python as First Langauge
#1
I do not know where to ask this. This forum is linked from Python.org. So it seems just right.

I am going to teach a first-year university course in programming next year for students of mathematics (Bachelor and High School Teaching). Through all the years, I have used Java as the first language. The reason for this is that Java is as fast as C and very close to the basic syntax of C, but cross-system and with a huge library for all sorts of programming scenarios. Python was always rejected because it is too high level and abstract, hiding the basic concepts from the user. Going from Java to C is easy, but not so from Python to C. Python is also not well suited for basic real-world or numerical programming because it is at least five times slower than Java, and often even more performance is lost.

Now I am aware that there are nice and useful things like Numpy or Matplotlib, and even libraries for machine learning. Those are C in the background, and they don't exist in Java in that form. This inclines me strongly towards Python. But, in the basic course, this will never be reached. And I think for someone that understands basic commands, data types, and objects, it is fairly easy to learn the Python concepts necessary to use these libraries.

So, here is my question: What good reasons can you give to convince me to use Python instead of something basic like C or Java?
Reply
#2
On reason - Simplicity of code for complex problems.
I learned C at Bell Labs in New Jersey. Used it for many years (~20).
Code that would take me a week to write in C can be written in an evening with python.
Reply
#3
Not to mention a huge base of freely available third party libraries covering about everything from finite state machines to astronomy or robotics.

Only a few lines of code are necessary to demonstrate programming a game or a web server or interprocess communication or networking, etc.
Reply
#4
What is the content of your course? I think for an introductory programming course, there's enough new stuff for the students that you want to avoid any extra complications. So, I agree with Python being a good choice precisely because it is quite high level. When they need low level, you can go there. There are also compilers for Python that will produce optimised machine code (Numba and Cython at least), but I can't claim to have any experience with them.
Reply
#5
Thanks for the answers. I hear the claim that it is easier to get results in Python.

For the level of this class, I have my doubts because I do not want to program games or UIs, nor do machine learning. This is for students in math taking the basic first-semester classes (I am a math prof at the University of Eichstätt). The course covers basic programming techniques like data types, loops, recursion, arrays, objects with examples like sorting, etc. However, I will recheck some of the aims and whether or not they are really easier in Python, both to grasp and to do.

Here are some of the more advanced examples that students might learn: Handle binary trees, learn and try sorting algorithms, read data from files, and handle them with basic statistics, maybe create a simple plot and a simple window interface.
Reply
#6
Here are a small number of packages, or tutorials for subjects you mention:
Packages related to binary trees: https://pypi.org/search/?q=%22binary+trees%22&o=
Sorting algorithm tutorial python: https://realpython.com/sorting-algorithms-python/
reading data from files: https://docs.python.org/3/tutorial/inputoutput.html
simple line plots python: https://jakevdp.github.io/PythonDataScie...plots.html
Or if you want to drill down to actually creating the plots yourself, do so using one of many available GUI packages.
Simple window interface (my own): https://python-forum.io/Thread-Perfectly...t=wxpython
Reply
#7
John Zelle strongly advocates python as a first programming language. His name has been known for many years in python forums because of the many students asking question about his graphics.py module. His book may contain an interesting approach to teaching python to beginners.
Reply
#8
My five cents about this subject.

Python and Java are both programming languages. There are different approaches how to teach any language (spoken, programming, VIM), simplified way could be to categorize them as 'vocabulary based' and 'grammar based'.

Vocabulary based is to make people express themselves right away despite mistakes they make. Grammar based is more like avoiding mistakes while expressing yourself (in sports term - there are ones who want to win and there are ones who want not to loose).

There is no 'one size fits all' but my experience is that vocabulary based approach engages students more and they will gain confidence by seeing and feeling their progress.

Python is more 'what to say' and Java is more about 'how to say' (my biased opinion).

High school students generally are pretty strong in math and abstractions it provides. There is no big leap to apply this knowledge to solve problems using Python. Based on that confidence they can move to 'grammar' i.e. Java/C.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
The Zeller reference is a good read. I have some arguments against this page, however. (By the way, the page should be formatted for an easier read.)

He is exaggerating the complexity of Java and hiding the complexity of Python. After all, A Java source file is also only a text file. The compilation step to a class is quite easy to get and the encapsulation is easy to understand even for starters. And Java has a "for x in v" too. Also, using "Print x" to show how simply Python is, defeats the advertising effect since Java 3 abandoned that.

Some information is not correct. Python does no more pass by value than Java or any other language. It makes pointer implicit just a Java does. To understand the difference of "f(x)" for variables x and lists x is as hard as in any other language. What is printed in the following code and why?

v = list(range(5))
def f(v):
    v[4]=9
f(v)
print(v)

w = list(range(5))
def f(w):
    w=8
f(w)
print(w)
The code creates a warning message on "w=8", indeed, which is good. But why am I able to redefine f()? Is that a good idea to enforce clear and clean code?

Advocating for the lack of "the necessity to discuss the complex visibility and hiding modes of Java" is not something I support. It is indeed very easy to hide code errors by not being aware of those "complex" things and get programs that are very difficult to understand or maintain. Anyway, in this first course, the object-oriented approach will only on a basic level. But not understanding those things from the start may backfire later.

Finally, let me admit that the simplicity of the data declaration in Python attracts me. Lists in Java are just not that easy to create. And tuples do not exist. You will have to use lists for this Java code.

v = (2, 4.5, "ape")
for o in v:
    print(o)
Translates to:

		Object v[] =
		{
			3, 4.5, "ape"
		};
		for (Object o : v)
			System.out.println(o);
I also admit that the simpler syntax is easier for beginners. It is also nice to enforce a strict structure in the code. I have seen examples of unreadable Java from beginners. But modern IDEs reformat the code, so this is not that much of an issue.

I am not so convinced about the lack of compile-time checking and variable declarations even for beginners. It is so easy to write "i=..." instead of "j=...", and the resulting errors are very hard to find. In typed languages, "j" would be simply undefined. The Spyder IDE not even claims about unused but assigned variables in a Python file besides in functions.

I am writing all this not because I don't not like the ideas behind Python. Partly, I am doing this to create a discussion, but mostly, to convince myself that there could be some reasoning behind sticking with Java.
Reply
#10
(Jul-04-2020, 02:33 PM)mga010 Wrote: The code creates a warning message on "w=8", indeed, which is good. But why am I able to redefine f()? Is that a good idea to enforce clear and clean code?
There is no Warning message in that code,and there is no return in either function.
The second dos nothing and will return None when do f(w),but will not see that as you only print the list print(w).
To write it so it make some sense.
def f1(v):
    v[4] = 9
    return v

def f2(w):
    w = 8
    return w

if __name__ == '__main__':
    v = list(range(5))
    w = list(range(5))
    print(f1(v))
    print(f2(w))
Output:
[0, 1, 2, 3, 9] 8
So function f2 output 8 as it should,as argument w is never use in that function.

mga010 Wrote:I am not so convinced about the lack of compile-time checking and variable declarations even for beginners.
It is so easy to write "i=..." instead of "j=...", and the resulting errors are very hard to find. In typed languages, "j" would be simply undefined.
The Spyder IDE not even claims about unused but assigned variables in a Python file besides in functions.
You could use Type Annotations as a instruction to types in Python.
It's also good to have this knowledge if they start in larger company's as eg DropBox or Instagram,as using Type Annotations is now is almost mandatory for them.
Our journey to type checking 4 million lines of Python
Instagram has made MonkeyType to add type annotations to code where is not automatically.
So can give a example using code over.
from typing import List, Tuple

def f1(v: List[int]) -> List:
    v[4] = 9
    return v

def f2(w: List[int]) -> None:
    w = 8
    return w

if __name__ == '__main__':
    v = list(range(5))
    v.append('a')
    w = list(range(5))
    print(f1(v))
    print(f2(w))
So we see that function f1 shall take a list of integers as argument,and the return value will be a list.
Have added a problem bye adding string a,but running normal Python will not catch this.
Using a static type checker like mypy will catch it.
So a run look alike this.
E:\div_code
λ python func_1.py
[0, 1, 2, 3, 9, 'a']
8
With mypy it catch all the problems,also all problem as mention with function f2.
E:\div_code
λ mypy func_1.py
func_1.py:8: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]")

func_1.py:9: error: No return value expected
func_1.py:13: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
func_1.py:16: error: "f2" does not return a value
Found 4 errors in 1 file (checked 1 source file)
mga010 Wrote:Anyway, in this first course, the object-oriented approach will only on a basic level. But not understanding those things from the start may backfire later.
As a advice try to learn how Python dos OOP,and not just write as it's done it Java.
Python Is Not Java.
Here a rewrite i did about 4-year ago,someone had written Python as in Java,as they had learn in class.
See the left side is about 3 times shorter Wink
[Image: sHoNd1.jpg]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020