Numbers in Python are stored as integers or floats. Your choice of data type depends on the nature of your program.
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.
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.
Floats
Any number that has a decimal point is a float.
We can also add, subtract, multiply and divide floats in Python.
Any operation that involves integers and floats would always result to floats.
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
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.
It also works for floats
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.
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.
Constants
These are variables whose values should not change throughout a program. They can be indicated by capital letters
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.
Multiline comment
This is surrounded by three single or double quotes. Anything inside the quotes is ignored by Python.
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.
For the code below, we'll need a 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
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: