The whole purpose of programming is to manipulate data. Without data, software is of little use. So lets see what kind of data can be handled in Python. Primitive Data Types are data types which cannot be broken down into sub-data types and they form the building blocks of more complex data. At its very heart, all computers only store numbers , so the two kinds of numbers which Python can work with are integers and floating point. To put it simply, integers are numbers without a decimal part eg.45 and floating point numbers are numbers with a decimal part eg.45.091
The maximum value that can be stored in int is 9223372036854775807 and the minimum is -9223372036854775807 .
Floating point numbers are more complicated to understand than int, specially when you see the E notation, but for information purposes the max float value can be 1.7976931348623157e+308 and min value is 2.2250738585072014e-308 .
Apart from numbers, there is a bool type which represents a logical True or False.
In order to work data we need operators. Operators are very similar to what we use in arithmetic:
- Addition +
- Subtraction –
- Multiplication *
- Integer Division returning quotient / or //
- Integer Division returning remainder % (called the modulus operator)
- Exponentiation ** (raised to the power of)
Here is a sample run:
The point to note about division is that the result of a division internally is always in floating point but the / operator only returns the integer part . For a modulus operation if the result of a division has no remainder it will be zero. If you try to do any division by zero it will return an error since division by zero is not supported. You can try the same operators with floating point numbers. They work the same except the result will be in decimal numbers.
What we saw above were operators for numbers. We have operators for logical operations too which return a bool value:
- less than <
- less or equal to <=
- greater than >
- greater or equal to >=
- is equal ==
- is not equal !=
- logical and and
- logical or or
- logical not not
Some example logical operations are shown below:
The arithmetic operators are self-explanatory. The logical operators work on the following rules:
- True and True = True
- True or True = True
- True and False = False
- True or False = True
- not True = False
- not False = True
Any numeric value which is greater than zero is True. Zero is considered false. So 2 and 2 translates to True and True. 45 or 0 translates to True or False . The not operator works on only a single value so you can see the error in the output above for print 5 not 7 . The not operator reverses the logical value. So using not on any number greater than zero will return False since it is flipping True to False.
Multiple operators can be used to come up with a single result. The sequence in which operators will be executed is known as operator precedence. The acronym BEDMAS is an easy way to remember operator precedence.
- B – Brackets
- E – Exponentiation
- D – Division
- M – Multiplication
- A – Addition
- S – Subtraction
So print 7-5+5+2-10*2/2 will give a result of -1 . It is always wise to use brackets so that there is no confusion in understanding and also making python understand how exactly you want the operations to work. So print (10+2) / (2+1) will return 3 because by using brackets you are telling Python to execute the operations within the brackets first and then execute the division operator.
When you want to work with multiple data and you want to refer to them in some way , we give them names called identifiers. A variable is an identifier which contains a value. Values are assigned to identifiers using the = operator. Eg. age = 10 or rows = 100 or cost = 25.99
In the next post we will look at Collections.