Browse 547 attributed reviews, viewable separately for our classroom and online training
Five differences between Python and other languages
How does Python compare with other programming languages like C#, VB or SQL? This blog looks at the differences we've come across in preparing our Python courses and videos.

Posted by Andy Brown on 31 August 2021

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Five differences between Python and other languages

It's been an interesting journey creating our first Python course (and starting a new Python video series).  Here are the 5 things about Python which stand out for me (particularly in comparison to other programming languages).

Everything is so easy (there's a but coming ...)

Python makes it astonishingly easy to do some things,  For example, here's you declare a variable to hold the meaning of life in 4 programming languages:

Declaring variables

Declaring a variable in C#, SQL, JavaScript and VB.

 

Here's how you declare the same variable in Python:

# create an integer variable

answer_to_life = 42

You'll notice the difference - you don't need to declare the data type.  But unlike (say) JavaScript, it's not that there isn't a data type for variables - it's just that Python infers this from the context (which on the whole is I think a good thing).

Another example is indentation - here's how to count to 10 in Python:

# count to ten

for counter in range(1,11):

print(counter)

No weird brackets, no before/end statements: just two lines of code, using indentation to say when the loop begins and ends.

The only downside to this approach is that it can become hard to keep track of which loop or condition you're in when you have multiple levels of indentation, but that's true in every other language too (apart from VB/VBA, which have readable end statements).

The but: it's hard to get started

Being an open-source language, there's too much choice to begin with.  Which IDE (Integrated Development Environment) should you learn?

Interpreter Pros/cons
Idle Built in to Python, but not very powerful or user-friendly.
Visual Studio Code A pain to set up, although nice once you get going.
PyCharm Dedicated to programming in Python, this is (apparently) slow, and you may need to pay to upgrade at some stage.
Visual Studio The big beast, but is it a sledgehammer to crack a nut?
Spyder, Jupyter Notebooks ... Etc, etc!

Even when you do choose an IDE (and learn its foibles - I went for Visual Studio Code), you still have to learn how to set up Python.  You're going to have to learn about commands like pip and venv, and learn more about paths and virtual environments than you ever wanted to.

In many ways learning Python is like rolling back the years to the days when every developer had a working knowledge of DOS batch commands, but I resent having to use this knowledge in 2021!

Modules are powerful (but have silly names)

A hidden secret about Python: it's just like .NET (instead of namespaces you have modules, but the principle is exactly the same).  But whereas .NET namespaces can be hard to learn, Python modules are in general gloriously easy to understand, as this example shows:

# start a list of external links

external_links = []

 

# get all links

links = soup.find_all("a")

for lk in links:


# for each one, get the link

url = lk.get("href")


# if this is external, add it to list

if not str(url).startswith("/"):

external_links.append(url)

 

# show all links

print("\n".join(external_links))

This converts the HTML from a web page into a list of links, and prints out the external ones (not all of the code needed is shown).  To make it work, you need to reference two modules:

# use Requests and Beautiful Soup to parse HTML

import requests

from bs4 import BeautifulSoup

These two modules allow you to get at and parse the HTML from any web request, but BeautifulSoup?  Seriously?

In general as a Microsoft user I've been very impressed with the quality of the open-source modules that you can reference from within Python, although I do continually wonder who puts the effort into making them so reliable and easy to use!

Working with an interpreted language is strange

Here's a Python function to join the two parts of an actor's name together:

# *******************************

# function to return actor name

# *******************************

def get_name(actor_record: str) -> str:

 

# eg "Cruise,Tom" would return "Tom Cruise"

 

# split the name at the comma

parts = actor_record.split(',')

 

# reconstruct and return

return parts[1] + " " + parts[0]

So it looks like this function takes in a string of text (str) and returns a string of text, but the extra labels are only there by convention.  The function could just as easily begin:

def get_name(actor_record):

There's nothing to stop you passing anything you like to this function: it's up to you to have the discipline to ensure that the type of thing you pass to it will processed correctly! 

I suspect many purists will hate the fact that Python is so forgiving when it comes to coding, but I quite like it (I just have to keep remembering that Python is reading the lines as they're processed, rather than compiling them into a single library first).

Lists and comprehensions are amazing

Here's a short program to list out the first 100 squares in Python:

# number of items to sum

num_terms = 101

 

# get a list of first N squares

squares = [i * i for i in range(1,num_terms)]

 

# print the sum of the first N terms

print("Sum of first {0} squares is {1}".

format(num_terms,sum(squares)))

The variable squares is set to hold a list of the squares of the integers in the range from 1 to 100 (it uses a comprehension because not only does it create a list of the numbers from 1 to 100, but it then transforms each into its square - experienced programmers may recognise this as a substitute for using a lambda function, which you can also do in Python).

If you like the elegance of this short program, you'll like Python; you can use lists to do almost everything!

This blog has 0 threads Add post