- Computers & Software»
- Computer Science & Programming
Numbers In Python - Part 2
This is Part 2 of the Python Coding Series.
- Part 1: Introduction to Python
Numbers work incredibly different in Python than other coding languages. One of the first foundations we need to establish with Python coding is learning how to store information. Computers are really smart at handling information. A program uses variables to make this work.
An integer, float, and a long variable walk into a bar...
There are three main variable types associated with numerical values.
- Integer (int): An integer is a number that does not contain a fraction or decimal. (ex: 1, -6, 20 , 50)
- Float: Floats are known as decimal numbers (3.14, -5.69,25.25)
- Long: A long variable is able to store a large numerical value.
Each variable is crucial to understanding what they are and what they can be. However, in Python, there is no need to define what the variable type is because Python will identify them for you. In Java, we would normally define an integer with either int, float, or long before we state the name of the variable. In Python, we just assign the variable to the number as seen in Example 1.
a = 5 print(a) //Output is the number 5
If you want to return the type of a variable all you would do is use the type(var) function. This function will return what the type of that variable is as seen in Example 2.
type(a) //returns <type 'int'>
If you ever run into problems associated with numbers, then sometimes it had to do with how you are handling numbers. Using the type function is key to identifying whether the compiler did not like how you handled the numbers.
How to Name a Variable
Variable names take up memory so giving a long title for a variable is not ideal. There are rules associated with naming variables that every coder should know.
- Names cannot begin with a number. The compiler will not like this.
- Names cannot be too long due to memory spent use.
- Names cannot contain any special characters like $,%,^,&,*,!,(,@,# though the underscore (_) is great to use with variables that are given more than one word.
- If a variable is a constant and does not change in the program, assign that variable to all caps like TAX or INTEREST.
- Make sure that your variable name makes sense. Do not assign the word 'apple' for a variable that should be labeled 'orange'.
- Rather than writing numberofcars consider writing it as number_of_cars
- Make all variables lowercase
- Avoid using the letter 'l' or 'o' if representing a variable as a single letter. This can be confused as the number 1 or 0.
Now that we have the rules in place, let's begin doing some math in Python!
Math in Python
Math in Python works just like normal math with the use of operators. There are about 9 operators that can be used.
1 + 1 returns 2
2 - 1 returns 1
5 * 2 returns 10
6 / 2 returns 3
13 // 2 returns 6
13 % 2 returns 1
-a returns -5 if a =5
abs(-5) returns 5
2**6 returns 64
Addition, subtraction, multiplication, and division work the same as regular math. Floor division is when you divide two numbers and round to the floor. Modulo will return the remainder of dividing the two numbers. Modulo is an important operator because it can be used to determine if a number is odd or even. Negation negates a variable. Absolute value returns the absolute value of the variable. The exponent operator in Python is strange as it features two asterisks instead of the usual '^' symbol. This will assign the 2nd number as the exponent value of the first.
There are some few important points to understand when working with numbers. First of all combining different types of numbers when doing math will often change the variable's type. For example, the type of 1 +1 is an integer, but the type returned by the summation of 1.0+1 is a float, not an integer because of the addition of a float. Remember this when working with different data types. When dealing with floats, it is best to go ahead and represent any integers as floats.
Also, remember that you cannot violate math in Python. If you divide by zero, you will get a ZeroDivisionError. If you ever get this error, that means you tried to divide zero somewhere in the program.
Python follows Order of Operations
The Order of Operations is a set of guidlines to solving complex equations that feature more than one type of operator. The PEMDAS acronym is used when solving order of operations problems. When solving a complex problem in python it will follow this order from top to bottom.
Here are some examples:
5 + 6 * 2
5 + 6 ** 4
(5 + 6) * (2 + 5 * 2)
5 + (4 * (4 + 2))
In this part of the Python Coding series, we learned about the different numerical variables in Python. We also learned how to name a variable and the rules associated with doing so. In the next part of this series, we will learn about Logic in Programming which involved conditional statements.
view quiz statistics