Python language basics 27: value and reference equality basics

Introduction

In the previous post we started discussing the most important cornerstone of object-oriented languages, i.e. objects. We saw how every variable assignment created an object reference in memory with a key, a value and a pointer which points from the key to the value. We also discussed what happens in memory when assigning a value to a variable.

In this post we’ll go through the absolute basics of equality in Python. The notions of value and reference equality are closely related to objects.

Read more of this post

Python language basics 26: objects

Introduction

In the previous post we looked at how to comment your Python code so that it becomes more understandable. We saw a couple of techniques to write comments and document your code.

In this post we’ll discuss the probably most important ingredient of object-oriented programming languages: objects.

Read more of this post

Python language basics 25: commenting your code

Introduction

In the previous post we saw how to organise your code so that you can pass in an argument to your main method from the command line. We saw how the command line arguments were included in the “args” property of the “sys” object. Args is a string array so we could simply use square brackets to extract the player name argument in our number guessing game.

In this post we’ll look at a topic that’s usually not very dear to programmers: documentation.

Read more of this post

Python language basics 24: adding a command line argument

Introduction

In the previous post we investigated the role of the global __name__ variable in Python and how it could be used to execute a function from the command line. The __name__ variable evaluates to “__main__” in case the Python file is not merely imported into another one but instead is the main file being called from the command line.

In this post we’ll discuss how to execute a Python file and pass in an argument from the command line.

Read more of this post

Python language basics 23: using the global __name__ attribute

Introduction

In the previous post we saw one way to break up the number guessing game into short functions. We saw examples of void methods and functions with and without parameters. We also said that there are usually several possible ways to organise the code into functions.

However, we also saw that calling the number guessing game from the command line didn’t execute anything. Python has no way of knowing what you want to do with the code, it only sees a collection of functions.

The solution is easy but we need to learn a new language construct first.

Double-underscore variables

Python has a small number of built-in variables surrounded by double underscores ‘__’. These are populated by Python during code execution, i.e. the Python runtime, and can be accessed in code if you need them.

One such variable is…

Read more of this post

Python language basics 22: organising code into functions

Introduction

In the previous post we looked at functions which are one of the most important building blocks of any serious programming language. We saw how to use the ‘def’ keyword to define a function. We also discussed the difference between void functions – methods – and functions that have a return value.

In this post we’ll organise our previous number guessing game into functions as an exercise.

Note that there’s most often no single solution when you break up your code into functions. The one presented here is only one solution out of many.

Starting point

As a reminder here’s the number guessing game code:

Read more of this post

Python language basics 21: functions in Python

Introduction

In the previous post we took a break from going through the Python language features and looked at how to execute a Python source file from the Windows command prompt. We saw how easy it was to execute a file without having to go through any steps before such as code compilation like in Java with the “javac” tool.

In this post we’ll return to Python language constructs and see how functions are built.

Functions and methods

Functions are reusable code blocks that execute the code within the function body.

Consider the following code:

Read more of this post

Python language basics 20: running a Python file from the command line

Introduction

In the previous post we looked at how for-loops are constructed in Python using the range function. They are in fact practically the same as foreach-loops as we iterate through a range of integers and we perform something with each integer. However, that is the closes approximation of the more traditional integer-based for-loops of other programming languages like Java:

for (int i = 0; i < 10; i++)
{
}

Read more of this post

Python language basics 19: for-loops

Introduction

In the previous post we saw how we could iterate through a collection using the foreach-loop in Python. Foreach-loops are ideal when looping through collections and we looked at how lists and dictionaries can be iterated.

In this post we’ll look at the more traditional version of looping, called for-loops.

For-loops in Python

Read more of this post

Python language basics 18: looping through collections with foreach-loops

Introduction

In the previous post we looked at the dictionary data structure in Python. We saw how a dictionary could be used to order a value to a key just like in a real language dictionary. In the post before that we discussed the list collection which is another important collection type in Python.

In this post we’ll see how to loop through each of the two collection types.

Looping through lists with a foreach-loop

By ‘looping’ we mean visiting each member of a collection one by one with the goal of performing some action on them. Another term often used in place of looping is iterating through a collection.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.