Decision Making Tools Lecture No_ 03
Decision Making Tools Lecture No_ 03
Lecture No: 03
1
Numbers in Python: Integers
● Python supports Integers, Float, and complex numbers.
● The integers are numbers such as -1, 0, 1, 2, and 3, .. and they have type int.
● You can use Math operators like +, -, *, and / to form expressions that include
integers. For example:
○ >>> 20 + 10 30
○ >>> 20 - 10 10
○ >>> 20 * 10 200
○ >>> 20 / 10 2.0
● To calculate exponents, you use two multiplication symbols (**). For example:
○ >>> 3**3 27
● To modify the order of operations, you use the parentheses (). For example:
○ >>> 20 / (10 + 10) 1.0
2
Numbers in Python: Floats
● Any number with a decimal point is a floating-point number. The term float
means that the decimal point can appear at any position in a number. In
general, you can use floats like integers. For example:
○ >>> 0.5 + 0.5 1.0
○ >>> 0.5 - 0.5 0.0
○ >>> 0.5 / 0.5 1.0
○ >>> 0.5 * 0.5 0.25
● The division of two integers always returns a float:
○ >>> 20 / 10 2.0
○ >>> 1 + 2.0 3.0
● Due to the internal representation of floats, Python will try to represent the
result as precisely as possible. For example:
○ >>> 0.1 + 0.2 0.30000000000000004
3
Underscores in numbers
● To make the long numbers more readable, you can group digits using underscores, like
this:
○ count = 10_000_000_000
● When storing these values, Python just ignores the underscores. It does so when
displaying the numbers with underscores on the screen:
Output: 10000000000
○ count = 10_000_000_000
○ print(count)
4
Summary
5
Introduction to Boolean data type
● To find out if a value is True or False, you use the bool() function. For
example:
○ >>> bool('Hi') True
○ >>> bool('') False
○ >>> bool(100) True
○ >>> bool(0) False
Summary
● Python boolean data type has two values: True and False.
● Use the bool() function to test if a value is True or False.
7
Python constant
● Sometimes, you may want to store values in variables. But you don’t want
to change these values throughout the execution of the program.
● The constants are like variables but their values don’t change during the
program execution.
● The bad news is that Python doesn’t support constants.
● To work around this, you use all capital letters to name a variable to
indicate that the variable should be treated as a constant. For example:
○ FILE_SIZE_LIMIT = 2000
● When encountering variables like these, you should not change their values
● These variables are constant by convention, not by rules.
8
Summary
9
Type conversion in Python
● To get input from users, you use the input() function. For example:
○ value = input('Enter a value:')
○ print(value)
● When you execute this code, it’ll prompt you for input on the Terminal:
○ Enter a value:
● If you enter a value, for example, a number, the program will display
that value back:
○ Enter a value: 100
○ 100
10
Example
● The following example prompts you for entering two input values: net price
and tax rate.
○ price = input('Enter the price ($):')
○ tax = input('Enter the tax rate (%):')
○ net_price = price * tax / 100
○ print(f'The net price is ${net_price}')
Output:
Enter the price ($):100
● When you execute the program and
Enter the tax rate (%):10
enter some numbers:
Error:
Traceback (most recent call last):
● You’ll get the following error:
File "filename.py", line 4, in <module>
● Since the input values are strings,
net_price = price * tax / 100
you cannot apply the arithmetic
TypeError: can't multiply sequence by non-
operator (+) to them.
int of type 'str'
11
Cont’d
● To solve this issue, you need to convert the strings to numbers before
performing calculations.
● To convert a string to a number, you use the int() function. More precisely, the
int() function converts a string to an integer.
● The following example uses the int() function to convert the input strings to
numbers:
○ price = input('Enter the price ($):')
○ tax = input('Enter the tax rate (%):')
○ net_price = int(price) * int(tax) / 100
○ print(f'The net price is ${net_price}')
● If you run the program, and enter some values, you’ll see that it works
correctly:
○ Enter the price ($):100
○ Enter the tax rate (%):10
○ The net price is $ 10.0
12
Other type of conversion functions
● Use type conversion functions such as int(), float(), bool(), and str(value)
14