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:

  • Whole numbers, i.e. integers, which is the topic if this post
  • Floating-point numbers, like 3.4 and 6.77 which we’ll take up in the next post
  • Booleans, i.e. true or false

An important characteristic of Python is duck-typing. In practice it means that we don’t need to declare the type of a variable. In C# or Java you would declare an integer like this:

int x = 4;

In Python it’s enough to write “x = 4” and the compiler will infer from the value what type it is.

Integers

Like most other programming languages Python supports integers. I’m not even aware of a single serious language out there which doesn’t support this data type. Integers in Python are signed, i.e. you cannot declare an unsigned integer like e.g. in C# with “uint”. As mentioned above it’s not even possible to declare a variable like…

int x = 4

…in Python, it will give you a compilation error. Also, there’s no distinction between the different types of integers, like shorts, ints, longs, bigints etc. The following are all valid integer declarations:

x = 4
y = -4
z = math.factorial(100)

z will be an enormous number and Python will have no problem assigning it to a variable of some integer type:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

That’s probably more than the total distance to the nearest galaxy in millimeters. In strongly typed languages like Java not even the ‘long’ data type could store a number that large. You’d need to look for some specialised class like BigInteger.

You can also declare a number in the binary system:

b = 0b101
print(b)

This will print 5 as that’s the decimal equivalent of the binary 101.

Other frequently used number bases are also represented by shortcuts:

  • Octal with ‘0o’, e.g. 0o101, i.e. 65 in decimal
  • Hexadecimal with ‘0x’, e.g. 0x101, i.e. 257 in decimal

It’s possible to construct integers from other data types using the int constructor. Floating point numbers are rounded towards 0:

i = int(5.75)
print(i)

…i will be 5. The variable i will be -5 in the below example as that’s the nearest integer towards 0 on the number line:

i = int(-5.99)
print(i)

Strings can also be converted into integers:

s = int("345")

We can also convert values stored as a string in some number base. Say you have a number like “1231” in base 5 and you’d like to know what it is in decimal. Here you are:

s = int("1231", 5);

The result is 191 in decimal.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

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

%d bloggers like this: