Python language basics 17: dictionaries

Introduction

In the previous post we discussed how to store a number of elements in list collections. We saw how to create a list, how to append a new element to it and how to access a certain element within the list.

In this post we’ll look at the basics of another type of collection called a dictionary.

What is a dictionary?

You’ll probably know what the English word dictionary means. If you open an English-German dictionary then you’ll be able to look up what a certain English word means in German. The English word you want to look up is called the “key” and the corresponding German translation is called the “value”. You can always find the value by its key. Normally a language dictionary only contains unique keys, i.e. the same English lookup word only appears once.

Dictionary collections in Python work in much the same way. They have keys and each key has a value. Each key is unique. An important difference is that the keys in language dictionaries are normally sorted in alphabetical order so that a human can quickly look up a word. Key-values in Python dictionaries are on the contrary not sorted in any way by default. However, that’s not an issue for the program itself because it will quickly find any given key.

Read more of this post

Python language basics 16: array sequences

Introduction

In the previous post we looked at some additional features of strings. We saw how the string constructor could be used to convert a certain type, like an integer, into a string. We also discussed how to access individual characters within a string using square brackets. Finally we looked into a couple of useful functions related to strings.

Sequences are collections of data in a container. Collections are very important facets of all mainstream programming languages. There are many different types of collections but as this is an intro course we’ll start with the easiest type of collection: the array. Arrays are also called lists.

Lists

A list collection is a list of elements in a container. This is how we construct an empty list in Python:

myFirstList = []

Read more of this post

Python language basics 15: string basics part 2

Introduction

In the previous post we saw how strings are represented and constructed in Python. We also mentioned other important terms such as string concatenation, immutability and character escaping.

In this post we’ll look at a couple of other important things regarding strings. Bear in mind that Python offers a very large library regarding strings. It could take the rest of the year to blog about all of it. Here we’ll only discuss a handful. Here are a couple of useful links to help you further:

Read more of this post

Python language basics 14: string basics

Introduction

You can think of a string as being a text. It is a combination of characters like “a” or “h”. Characters can just as well be non-Latin ones of course such as Greek, Cyrillic, Chinese or Japanese characters. Characters that make up a word, a sentence or a whole book are all examples of strings.

Creating strings in Python

There are various ways to create strings in Python. We’ve already seen some examples in this series. You just put the string within single or double quotes and you’ve constructed a string:

myString = "hello world, this is Python calling"
myOtherString = 'hello world, this is Python calling'

Read more of this post

Python language basics 13: our first Python application

Introduction

In the previous post we saw how to get the user’s input in a command window. In this post we saw how to construct a while loop in Python. In this post we’ll marry the two concepts and build our first “real” application which requires user input. We’ll put together a simple number-guessing game where the computer “thinks” of a number between two integers and the user has to keep guessing until they find the correct one.

Read more of this post

Python language basics 12: reading user input

Introduction

In the previous post we looked at how to construct a while-loop and what its purpose is. In this post we’ll quickly learn how read a user’s input.

As a reminder I use the free edition of the PyCharm IDE so all the printouts will be from that environment.

Read more of this post

Python language basics 11: while loops

Introduction

In the previous post in this series we looked at if statements in Python. We saw how to set up conditions and tweak our actions accordingly. An if-block is only carried out if the boolean expression results in true.

However, what if we want to carry out an action as long as some condition is true and stop as soon as the condition is false? Consider the following sentences:

If the weather is nice then we’ll play football.

vs.

While the weather is nice we’ll be playing football.

Read more of this post

Python language basics 10: conditional statements

Introduction

In the previous post we looked at relational operators in Python. We saw how to compare two integers using operators like ‘==’ or ‘<='. In this post we will look at a very important building block of any popular – and probably any not-so-popular – programming language: the if-statement.

The word ‘if’ is also an important element of the English language – and its translated version of any other spoken language. You can create one or more condition for your actions like in the following examples:

If the weather is nice then we will play football else we will go to the movie.
If everything goes well then I will save the world tonight else the world will crumble.

It works exactly the same way in programming. The segment between "if" and "else" is a boolean statement: “the weather is nice”, that is either true or false. In everyday life the result can be indecisive, like “it is cloudy but it is not raining”, but in programming the boolean statement will have a definite result. The weather is either good or bad depending on how you’ve defined “good” and “bad” in your program logic.

The if statement in Python

Not surprisingly the keyword ‘if’ is reserved for conditional statements. It’s important to recall the role of whitespace in Python as the indented rows below the if statement will be executed if the boolean condition is true. Check the syntax, the if statement must be closed with a colon ‘:’ :

val = 5
if val > 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')

All three statements will be printed on the standard output window.

Change the boolean condition…

if val < 4:

…and not surprisingly the if-block will be ignored.

OK, what about the ‘else’ section of our example sentences? Easy:

val = 5
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
else:
    print('The value is smaller than 4')
    print('Bye')

Guess which block is executed? The else block of course with two printouts. The ‘else’ statement is a catch-all statement which is executed if the boolean expression is false. However, we can refine our conditions further. Consider the following sentence:

If the sun is shining then we’ll play football, else if the rain is falling then we’ll stay inside, else if it starts snowing then we’ll start a snowball fight, else we’ll go shopping.

Note that only one of these statements will be carried out depending on the weather conditions. In reality it’s conceivable that the sun is shining and it’s raining at the same time so that the action we carry out might be inconclusive. That’s not that case in programming. The first expression that results in “true” wins regardless of any other outcomes. If all statements give false then the else statement wins. The “else if” bit is expressed by the “elif” keyword in Python:

val = 1
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
elif val < 3:
    print('Carrying our the first elif block')
elif val < 2:
    print('Executing the second elif block')
else:
    print('The value is smaller than 4')
    print('Bye')

Can you guess which block is executed? The first one as the first boolean condition results in true. The other branches are ignored even if they are also true.

Read all Python-related posts on this blog here.

Python language basics 9: relational operators

Introduction

In the previous post we looked at how to assign nulls to a variable using the None keyword. In this post we’ll look at Python’s relational operators. These are operators that help us compare two values.

Those of you who have experience with other object-oriented languages like C# or Java can safely skip this post. The relational operators are the same in Python: ==, !=, ,=.

Read more of this post

Python language basics 8: assigning nulls

Introduction

In the previous post we finished looking into how boolean types are represented in Python. This short post will show how nulls are represented. We use nulls to indicate the absence of any value in a variable.

None

If you have a C# or Java background then you’ll first guess will be that nulls are assigned by the keyword ‘null’, right? Let’s see:

hello = null

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.