Python language basics 7: boolean values

Introduction

In the previous post we looked at the difference between integer and float-point number division in Python. In this post we’ll look at the third built-in primitive type besides integers and floats: the boolean type.

Read more of this post

Python language basics 6: integer division vs. float division

Introduction

In the previous post we looked at support for floating point numbers in Python.

In this post we’ll take a look at integer vs. floating point division.

Read more of this post

Python language basics 5: floating point numbers

Introduction

In the previous post we took up the integer built-in data type in Python. In this post we’ll continue with how floating point numbers are declared and used in Python.

As a reminder let’s restate that an important characteristic of Python is duck-typing. In practice it means that we don’t need to declare the type of a variable. It also means that you don’t need to look for keywords like “float” or “double” like in Java or C#, they don’t exist in Python.

Floating point numbers

Like most other programming languages Python supports floating point numbers. I’m not even aware of a single serious language out there which doesn’t support this data type. Floating point numbers in Python are signed and are represented as 64 bit double precision numbers. On this page you can read more about the degree of precision of floats in Python – it’s actually the same as how the double data type is supported in C# and Java.

As mentioned in the post on integers referenced above it’s not even possible to declare a variable like…

float x = 4

…in Python, it will give you a compilation error. Also, there’s no distinction between the different types of floating point numbers like “floats” and “doubles”. Here’s a very simple declaration of a float in Python:

d = 7.344

Scientific notation is represented by “e”. The following expression…:

scientific = 4e10
print(scientific)

…means 4 x (10 to the power of 8), i.e. 40000000000.0.

For small numbers we can also use scientific notation:

verysmall = 0.543e-2

…which reads 0.543 times 10 to the power of -2 and gives 0.00543.

We saw in the post on integers how the int constructor can be used to build integers. There’s a similar float constructor which converts other data types to float.

From a string:

f = float("4.56")

From an integer:

f = float(13)

…where f will become 13.0.

You don’t need to worry about converting between integers and floats when working with both types within the same statement. The result will be converted to float automatically so that you don’t run the risk of losing precision. Example:

fres = 13.6 / 4
print(res)

‘res’ will be 3.4 and not 3.

Read all Python-related posts on this blog here.

Python language basics 4: integers

Introduction

In the previous post we discussed the role of white space in Python We saw how it was used to give structure to the code. In a number of other popular languages like C# or Java curly braces ‘{}’ are used to delimit code blocks, but Python is cleaner.

In this post we’ll start looking at some of the built-in data types in Python:

Read more of this post

Python language basics 3: significant white space

Introduction

In the previous post on Python basics we looked at how to import external libraries into your Python file using the import keyword.

This post will quickly introduce an interesting feature of Python: the significant whitespace. This is meant to simply prepare you for the fact that you can forget code block delimiters like ‘{‘ and ‘}’ where code blocks are used, such as methods or loops.

Read more of this post

Python language basics 2: how to import libraries in Python

Introduction

In the previous post we made our first steps into the world of Python. We installed the SDK and an IDE called PyCharm. In this short post we’ll see how to import external libraries into a Python file.

The import keyword

Read more of this post

Python language basics 1: installing Python and PyCharm

Introduction

This is the very first part of the long series of posts on the Python language. It is intended to show the basics of Python: objects, libraries, classes, methods etc. Note that I’m not a trained educator but I’ll try to be as educational as possible anyway so that even novice programmers can follow this series.

I’ve decided to learn a new technology to diversify my skills and my choice fell upon Python. It is a general-purpose language which can be used for anything ranging from simple scripting to fully-fledged Web/desktop/game etc. applications. This series is the documentation of how I’m progressing that hopefully some readers can also benefit from. I’m developing on Windows so e.g. all file paths will be described like “c:\folder\file”.

Here’s a short list of – probably biased – motivations to learn Python:

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.