0% found this document useful (0 votes)
4 views14 pages

Decision Making Tools Lecture No_ 03

Uploaded by

atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Decision Making Tools Lecture No_ 03

Uploaded by

atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Decision Making Tools

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

● When a number is large, it’ll become difficult to read. For example:


○ count = 10000000000

● 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

● Python supports common numeric types including integers, floats, and


complex numbers.

● Use the underscores to group numbers for the large numbers.

5
Introduction to Boolean data type

● In programming, you often want to check if a condition is true or false and


perform some actions based on the result.
● To represent true and false, Python provides you with the boolean data type.
The boolean value has a technical name as bool.
● The boolean data type has two values: True and False.
● The following example defines two boolean variables:
○ is_active = True
○ is_admin = False
● When you compare two numbers, Python returns the result as a boolean
value. For example:
○ >>> 20 > 10 True
○ >>> 20 < 10 False
● We can also compare two strings results in a boolean value:
○ >>> 'a' < 'b' True
○ >>> 'a' > 'b' False 6
The bool() function

● 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

● Python doesn’t have built-in constant types.

● By convention, Python uses a variable whose name contains all capital


letters to define a constant.

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

● Besides the int(str) functions, Python support other type conversion


functions. The following shows the most important ones for now:
○ float(str) – convert a string to a floating-point number.
○ bool(val) – convert a value to a boolean value, either True or False.
○ str(val) – return the string representation of a value.

Getting the type of value


● To get the type of a value, you use the
Output:
type(value) function. For example:
○ >>> type(100) <class ● The number 100 has the type of int.
'int'> ● The number 2.0 has the type of
○ >>> type(2.0) <class float.
float'> ● The string 'Hello' has the type of str.
○ >>> type('Hello') <class 'str'> ● The True value has the type of bool.
○ >>> type(True) <class
13
'bool'>
Summary

● Use the input() function to get an input string from users.

● Use type conversion functions such as int(), float(), bool(), and str(value)

to convert a value from one type to another.

● Use the type() function to get the type of a value.

14

You might also like