3-1.  Numeric Data Types

Data types are fundamental in programming, as they define the kind of data variables can store and the operations they support. Python includes default built-in data types grouped into several categories. These types are used to store different kinds of data. This module will explore commonly used simple data types, including text types, numeric types, and Boolean types.

Data Types Key Words
Text Type str
Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview
None Type NoneType

 

When computers were initially created, their primary role was to perform complex calculations, and this remains a significant use today. Problems that involve mathematical formulas are straightforward to convert into Python programs. In this section, we will delve deeper into programs specifically designed for numerical computations.

Numbers in Python are divided into three main types: integers, floating-point numbers, and complex numbers. Integers are whole numbers without a fractional component, while floating-point numbers contain a decimal point, making them suitable for representing more precise values. Complex numbers consist of a real part and an imaginary part, denoted by the suffix 'j'. Python provides a wide range of operations for working with these number types, including arithmetic operations, type conversion, and built-in functions for mathematical calculations.

Numeric variables are created when you assign a value to them. To check the type of any object in Python, you can use the type() function.

  Integer

Integers (int) in Python refer to whole numbers that can be positive or negative, without decimal points, and can be of unlimited size.

 

Float

A float, or "floating point number," in Python is a number that can be positive or negative and contains one or more decimals.

 

The following program demonstrates basic input handling, arithmetic operations, and output formatting for calculating and displaying the sum and average of three test scores.

# test_scores.py
# A program to calculate the sum and average of three test scores

def main():
   print("Test Scores Calculator")
    print()

   # Prompting the user to enter three test scores
   score1 = float(input("Enter score 1: "))
   score2 = float(input("Enter score 2: "))
    score3 = float(input("Enter score 3: "))

   # Calculating the sum of the scores
    total = score1 + score2 + score3

   # Calculating the average of the scores
    average = total / 3

   print()
   print("Sum of the scores:", total)
   print("Average of the scores:", average)

# Calling the main function to execute the program
main()

Example output:

Test Scores Calculator

Enter score 1: 85
Enter score 2: 92
Enter score 3: 78

Sum of the scores: 255.0
Average of the scores: 85.0

In the program, the main() function prompts the user to enter three test scores (score1, score2, and score3) using float(input()) to handle decimal input. Then, it calculates the sum of the three scores using total = score1 + score2 + score3. In addition, it calculates the average of the three scores by dividing the total sum by 3 (average = total / 3). Finally, it prints out the calculated sum and average of the scores.

Type Conversion

In Python, you can convert between different data types using int() and float() methods. The int() function is used to convert a number or a string containing a numeric value to an integer.

The float() function converts a number or a string containing a numeric value to a float.

 

These methods are handy when you need to change the type of a variable or handle different types of numeric inputs in your Python programs.

Random Numbers

Python does not have a built-in function to make a random number, but Python has a built-in module called random that can be used to make random numbers. You need to import the random module to generate random numbers. The following code generates and displays a random number between 1 and 6.