Learntofish's Blog

A blog about math, physics and computer science

Posts Tagged ‘Python’

Python 3: Inheritance and super()

Posted by Ed on April 23, 2016

Inheritance in Object Oriented Programming (OOP) means that you create a new class by extending an existing class. For example, suppose we have a class Shape with the attribute position. Then we can create a new class Circle by taking Shape and extending it by a new attribute center: Read the rest of this entry »

Posted in computer science, programming | Tagged: , , | Leave a Comment »

Awesome Pycon 2015 presentations

Posted by Ed on April 29, 2015

Watch the Pycon 2015 presentations here. I recommend the excellent talk Facts and Myths about Python names and values by Ned Batchelder. It clears misconceptions you may have regarding assignments, references and values in Python. I love this slide.

Posted in Uncategorized | Tagged: , | Leave a Comment »

How to write code the pythonic way

Posted by Ed on April 24, 2015

Check out this interesting article by Constantine Lignos called Anti-Patterns in Python Programming. It demonstrates with several examples how you should write code in Python.

Posted in computer science | Tagged: , | Leave a Comment »

Introduction to generators in Python 3.4

Posted by Ed on April 22, 2015

In this blog post I will explain how generators are used in Python 3.4. Read the rest of this entry »

Posted in computer science | Tagged: , , | 1 Comment »

How to write Python code the right way

Posted by Ed on September 18, 2014

Here is a wonderful talk by Raymond Hettinger with the title Transforming Code into Beautiful, Idiomatic Python

Slides can be downloaded here and viewed online here.

His Twitter where he regularly posts short Python code.

Posted in computer science | Tagged: | Leave a Comment »

Visualizing references and objects in Python

Posted by Ed on April 2, 2013

Pythontutor is a website that lets you visualize your Python code.

For instance, type the following Python code into the window:

x = [1, 2, 3]

# reference to the object that x points to
y = x

# create a copy of the list [1,2,3] and store its address at z
z = x[:]

# create a copy of the list [1,2,3] and store its address at z
a = x.copy()

# reference to the object that z points to
a = z

Then, change from Python 2.7 to Python 3.3 in the drop down menu below the window and click the button Visualize Execution.
Click on forward to process the code and observe how the graphics changes on the right.
Pretty neat, isn’t it!

You can also change some settings such as choosing between Python 2.7 and Python 3.3 and using the address id instead of the arrows.
Change the following settings:
inline primitives and nested object -> render all objects on the heap
draw references using arrows -> use text labels for references

Then, paste the following code into the window:

myString = "hello"

# observe how the id changes
myString += " world"
print(myString)

Observe how the id changes if you try to append the string " world" to myString.
That is because strings are immutable in Python such that Python first creates the new string object "hello world".
The addess of this new object is then assigned to the reference myString.

Posted in computer science | Tagged: , , , | Leave a Comment »

Test your Java and Python skills

Posted by Ed on November 8, 2012

CodingBat offers some nice problems to test your programming skills in Java and Python. They are particularly suited for beginners. Some harder problems deal with recursion.

Posted in computer science | Tagged: , , , | Leave a Comment »

Call by Object Reference (Call by Sharing)

Posted by Ed on January 9, 2012

In the following I want to talk about Call by Object Reference also known as Call by Sharing. I will use Python to illustrate the concept.

1. Strange behavior?

Let’s have a look at the following Python code:

def changeList1(x):
    x.append(41)

def changeList2(x):
    x = [41,42]

Save the code in a file callBySharing.py and run it in Read the rest of this entry »

Posted in computer science | Tagged: , , | 1 Comment »

Tutorial: Object Oriented Programming in Python – Part 10

Posted by Ed on December 26, 2011

This is the continuation of the Python OOP tutorial. Today I want to describe how you can use Python in Eclipse by installing PyDev. Here, I will assume you have Eclipse installed and some familiarity with it.

Eclipse is a wonderful IDE known to Java users. I like it because of its auto-completion feature, e.g. if you use the dot operator on Read the rest of this entry »

Posted in computer science | Tagged: , , , , | Leave a Comment »