Python Integers and Floats

Numbers in Python are stored as integers or floats. Your choice of data type depends on the nature of your program.

python integers and floats

If you would store game scores, you'd want to use rounded numbers. Integers in this case.

Stock prices would require decimal places. Floats have to be used in this case.

Integers

These are whole numbers. Whole numbers don't have decimal points. Some examples of integers are 50, 2, 3, 100 e.t.c.

We can add, subtract, multiply and divide integers in Python.

integer operation

Dividing an integer either by another integer or a float would always result to a floating number.

Exponentiation can be achieved with two multiplication symbols.

exponentiation

Floats

Any number that has a decimal point is a float.

We can also add, subtract, multiply and divide floats in Python.

float operation

Any operation that involves integers and floats would always result to floats.

integer and float

Order of Operation

Python also supports the mathematical order of operation, PEMDAS.

P - Parentheses

E - Exponentiation

M - Multiplication

D - Division

A - Addition

S - Subtraction

Operations in parentheses are evaluated before exponentiation operations. The priority decreases down the list.

(2+4)*5-3 would give 6*5-3

Since multiplication has a higher order than subtraction, we would have 30 - 3 which in turn gives 27

PEMDAS

Storing long numbers in python

Large numbers can be difficult to read. We would normally group digits with comma but the comma symbol has a different meaning in Python.

Underscores can be used to group large numbers to make them more readable.

100000000000 can be written as 100_000_000_000.

It doesn't matter how you group the numbers. 2_0_0 also means 200.

storing large numbers

It also works for floats

long numbers

Python strips the underscores when printing.

This feature is only available in Python 3.6 and later versions.

Multiple variable assignments

We can assign values to multiple variables at once with just one line. This can help reduce the lines of code and make our program more readable and maintainable.

multiple assignment

The variable names have to be separated by commas and must match the number of values. Three variables and four values would throw an error.

multiple assignment error

Constants

These are variables whose values should not change throughout a program. They can be indicated by capital letters

constant

This doesn't mean a constant can't be reassigned. It only serves as an indicator that the value of the variable should not be changed.

Comments

Comments are used to describe sections of code. This can make a program easily understandable.

A programmer can easily figure out how pieces of code fit together after a long time of writing the program.

Descriptive comments can also make the users of a program understand it easily.

There are two types of comments in python:

Single line comment

This is indicated by the hash symbol(#). Anything that follows this symbol is ignored by Python.

single line comment

Multiline comment

This is surrounded by three single or double quotes. Anything inside the quotes is ignored by Python.

multiline comment

You should only write comments when a section of code is not descriptive enough.

The code below doesn't need a comment because the variable names are descriptive.

multiple assignment

For the code below, we'll need a comment.

comment

The Zen of Python

There's a philosophy for writing clean and efficient code in Python. This is contained in "The Zen of Python" by Tim Peters.

Simply run the code below

zen of python

The import statement is a way of using the contents of a program in another program. We'll cover this concept in future sessions.

Skim through the principles and try to apply them in your programs.

Don't always try to write a perfect code. Write programs that work. After writing a code that works, you should consider improving the code to make it efficient.

Summary

Numbers in Python are stored as integers or floats

Any operation that involves integers and floats would always result to floats

Python follows the PEMDAS rule to carry out mathematical operations

Comments are used to describe sections of code

The Zen Of Python contains principles for writing clean and efficient code

No comments:

Powered by Blogger.