0% found this document useful (0 votes)
19 views10 pages

DS T04 - Numerical Data Types

The document provides information about numerical data types in Python. It discusses integers, floats, and complex numbers as the three numeric types in Python. It explains how to declare variables as different number types, cast between types, and perform arithmetic operations. The document also introduces common mathematical functions available in the math module. It concludes by outlining two compulsory tasks and an optional bonus task for students to complete involving numeric data types and calculations in Python.

Uploaded by

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

DS T04 - Numerical Data Types

The document provides information about numerical data types in Python. It discusses integers, floats, and complex numbers as the three numeric types in Python. It explains how to declare variables as different number types, cast between types, and perform arithmetic operations. The document also introduces common mathematical functions available in the math module. It concludes by outlining two compulsory tasks and an optional bonus task for students to complete involving numeric data types and calculations in Python.

Uploaded by

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

TASK

Numerical Data Types


Introduction
WELCOME TO THE NUMERICAL DATA TYPES TASK!
You are probably already very familiar with numbers as we encounter them daily
and, in most cases, multiple times a day. Through studying mathematics at school
you were introduced to the different types of numbers, such as integers and
decimal numbers, as well as the operations we can perform on them, such as
addition, subtraction, multiplication and division. Computer programming
languages, such as Python, provide support for storing and manipulating many
different types of numbers. In this task, you will learn how numbers are categorised
based on their nature as well as how to perform arithmetic operations in Python.

Remember that with our courses, you’re not alone! You can contact an expert code
reviewer to get support on any aspect of your course.

The best way to get help is to login to Discord at https://discord.com/invite/hyperdev


where our specialist team is ready to support you.

Our team is happy to offer you support that is tailored to your individual career or
education needs. Do not hesitate to ask a question or for additional support!
Software development is full of acronyms. Here’s a link to the Hyperion Hub where you will
find some frequently used software development terms you’d do well to be familiar with,
for when it’s next thrown your way.

NUMBERS IN OUR EVERYDAY LIVES

Whether you are fond of math or not, you cannot escape numbers. Numbers play
an extremely important role in our daily lives. Probably not a day goes by without
you interacting with numbers in some way. Think about it. Did you go shopping
today and look at the prices of items? Did you change the volume or channel on
your TV? Did you drive anywhere today and notice the speed limit signs or
instrumentation on your vehicle's dashboard? Have you made a telephone call
today? Chances are you have done at least one of these things and if you haven't,
you can probably think of numerous other instances where you have encountered
numbers today.

You have also probably learned from school math that there are different types of
numbers. For example, whole numbers and decimal numbers. Whole numbers do
not contain a fractional part and can be used to count the items in a list. However,
decimal numbers do contain a fractional part. Decimal numbers are normally used
when precision is required, such as when dealing with measurements or currency.

With numbers being so important in our daily lives, it comes as no surprise that
they are equally important in programming. Every single programming language
provides support for manipulating, storing and defining different types of
numbers.
NUMBERS IN PYTHON

There are three distinct numeric types in Python 3: integers, floating-point


numbers and complex numbers.

● Integers: these are synonymous with whole numbers. Numbers which are
stored as this type do not contain a fractional part or decimal. Integers can
either be positive or negative and are normally used for counting or simple
calculations. For example, you can store the number of items you wish to
purchase from a store as an integer, e.g. num = int("-12").

● Floats: decimal numbers or numbers which contain a fractional component


are stored as floats. They are useful when you need more precision, for
example, when storing measurements for a building or amounts of money.
Floats may also be in scientific notation, with E or e indicating the power of
10, e.g. x = float("15.20"), y = float("-32.54e100")

● Complex: complex numbers have a real and imaginary part, which are each
a floating-point number, e.g. c = complex("45.j").

DECLARING NUMBERS IN PYTHON

When you declare a variable in Python, it will already know if it is a float or an


integer based on the characteristics used. If you use decimals, it will automatically
be a float and if there are no decimals, then it will be an integer.

class_list = 25 # integer
interest_rate = 12.23 # float

CASTING BETWEEN NUMERIC DATA TYPES

To cast between numbers, make use of the int() or float() functions, depending on
which is needed.

num1 = 12
num2 = 99.99
print(float(num1))
# Converting floats to ints, as below, causes data loss. int() removes values
# after the decimal point, returning only a whole number.
print(int(num2))
ARITHMETIC OPERATIONS

Doing calculations with numbers in Python works similarly to the way they would
in normal math.

sum = 2 + 4
print(sum)
# prints out 6

cents = 0.25 + 4.33


print(cents)
# prints out 4.58

The only difference between calculations in real mathematics and programming is


the symbols you use:

Arithmetic Operations Symbol used in


Python

Addition: adds values on either side of the operator +

Subtraction: subtracts the value on the right of the -


operator from the value on the left

Multiplication: multiplies values on either side of the *


operator

Division: divides the value on the left of the operator by /


the value on the right

Modulus: divides the value on the left of the operator by %


the value on the right and returns the remainder

Exponent: performs exponential calculation, i.e. calculates **


the answer of the value on the left to the power of the
value on the right

MATHEMATICAL FUNCTIONS

Python also has a library of pre-written code that you can access and use in your
code. The math module contains many mathematical functions. Some of the most
commonly used functions are listed in the table below:
Function Description Example

math.floor() Rounds a number down print(math.floor(30.3333)) will


print 30.0.

math.ceil() Rounds a number up print(math.ceil(30.3333)) will


print 31.0

math.trunc() Cuts off the decimal part of the print(math.trunc(30.33333))


float will print 30

math.sqrt() Finds the square root of a print(math.sqrt(4)) will print


number 2.0

math.pi() Returns the value for pi where pi print(math.pi) will print


is the number used to calculate 3.141592653589793
the area of a circle

Explore some more functions in the math module here. To be able to use the
functions in the math module, add this line of code to the top of your program:

import math
The further you progress on your Python journey, the more complex the programs you
build will become. Here are the top 5 Python libraries that you could use to create these
complex programs. It helps to be aware of them and know what they do so that you can
call on them when needed and as you gain more experience.

● Pillow. A Python imaging Library used for image processing.

● NumPy. A fundamental package for scientific computing using Python. It adds


support for linear algebra, statistics, large multidimensional arrays and matrices,
along with a large library of high-level mathematical functions to operate on these
arrays.

● PyGame. A cross-platform set of Python modules for creating video games and
multimedia programs. It is highly portable and runs on nearly every platform and
operating system.

● Scrappy. An open-source and collaborative framework for extracting the data you
need from websites. In a fast, simple, yet extensible way.

● Apache Libcloud. A library interacting with many of the popular cloud service
providers using a unified API. It was created to make it easy for developers to build
products and work between any of the services that it supports.
Instructions

Before you get started, we strongly suggest you use an editor such as VS Code or
Anaconda to open all text files (.txt) and python files (.py).

First, read example.py (remember to open it using VS Code or Anaconda.

● example.py should help you understand some simple Python. Every task
will have example code to help you get started. Make sure you read all of
example.py and try your best to understand.

● You may run example.py to see the output. Feel free to write and run your
own example code before doing the Task to become more comfortable with
Python.

Compulsory Task 1

Follow these steps:

● Create a new Python file in this folder called numbers.py.


● Ask the user to enter three different integers
● Then print out:
○ The sum of all the numbers
○ The first number minus the second number
○ The third number multiplied by the first number
○ The sum of all three numbers divided by the third number
Compulsory Task 2
Follow these steps:

● Create a new Python file in this folder called shopping.py.


● Once this is done, ask the user to enter the names of three products
● Now ask for the price of each product. Each product must have two decimal
values.
● Calculate the total of all three products.
● Calculate the average price of the three products. (Hint: you may want to
look up round() )
● Then print out the following sentence after performing your calculations:
○ “The Total of [product1], [product2], [product3] is Rxx,xx and the
average price of the items is Rxx,xx.”

If you are having any difficulties, please feel free to contact our specialist team on
Discord for support.

Optional Bonus Task

Follow these steps:

● Create a new Python file in this folder called optional_task.py.


● Ask the user to enter the lengths of all three sides of a triangle.
● Calculate the area of the triangle.
● Print out the area.
● Hint: If side1, side2 and side3 are the sides of the triangle:
○ s = (side1 + side2 + side3)/2 and
○ area = √(s(s-a)*(s-b)*(s-c))

Thing(s) to look out for:

1. Make sure that you have installed and set up all programs correctly. You have set
up Dropbox correctly if you are reading this, but Python or your editor may not
be installed correctly.
2. If you are not using Windows, please ask a reviewer for alternative instructions.
Hyperion strives to provide internationally-excellent course content that helps you achieve
your learning outcomes.

Think that the content of this task, or this course as a whole, can be improved, or think
we’ve done a good job?

Click here to share your thoughts anonymously.

You might also like