Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
544 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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. |
In this blog
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).
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 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).
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!
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!
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).
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!
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.