As a student, you must have come across various mathematical functions and formulae. You can either memorize all of them or write it down over and over again. But if you know programming, you might want to write them down to code. You can do that easily by using Python. In this article Learn Math With Python, we are going to write the simplest code that will help you with stuff like (Integers, Fractions, Central Tendencies, and all the basic math functions).
1. Learn Math With Python – Array of Numbers
An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Arrays are used in programs whenever you want to perform some operations such as finding maximum or minimum value, arranging, finding mean, median or mode on a group of numbers of the same data type.
1.1 Creating an Array
The following code creates an array of 3 numbers. Here, d represents the type of data present in an array.
import array as arr a = arr.array('d', [1.1, 3.5, 4.5]) print(a)
Following is the list of symbols used to denote different data types in Python:
Code | C Type | Python Type | Min Bytes |
b | signed char | int | 1 |
B | unsigned char | int | 1 |
u | Py_UNICODE | unicode | 2 |
h | signed short | int | 2 |
H | unsigned short | int | 2 |
i | signed int | int | 2 |
I | unsigned int | int | 2 |
l | signed long | int | 4 |
L | unsigned long | int | 4 |
f | float | float | 4 |
d | double | float | 8 |
1.2 Accessing Array Elements
An array is a continuous memory location storing a number of values of the same data types. The location of numbers is represented by position numbers starting from 0 and is known as the index or subscript. The index of 0 (zero) represents the first location in an array.
Syntax for accessing an element of an array is: <array_name>[index]
For example: a[0] for the first element, a[1] for the second element, and so on.
import array as arr a = arr.array('i', [2, 4, 6, 8]) print("First element:", a[0]) print("Second element:", a[1]) print("Last element:", a[-1])
The index starts from 0 (not 1).
1.3 Changing Elements
While working with arrays, you may need to change the content of a location in an array. It can be done by specifying the location and input value along with the array name.
import array as arr numbers = arr.array('i', [1, 2, 3, 5, 7, 10]) print("Original Array") print(numbers) # changing first element numbers[0] = 0 print("After changing first element") print(numbers) # Output: array('i', [0, 2, 3, 5, 7, 10]) # changing 3rd to 5th element numbers[2:5] = arr.array('i', [4, 6, 8]) print("After changing 3rd to 5th element") print(numbers) # Output: array('i', [0, 2, 4, 6, 8, 10])
1.4 Adding Elements
You can add new elements to an existing array using append and extend keywords.
import array as arr numbers = arr.array('i', [1, 2, 3]) print("Original Array") print(numbers) numbers.append(4) print("After inserting 4") print(numbers) # Output: array('i', [1, 2, 3, 4]) # extend() appends iterable to the end of the array numbers.extend([5, 6, 7]) print("After inserting another array") print(numbers) # Output: array('i', [1, 2, 3, 4, 5, 6, 7])
1.5 Adding Elements from User Input
Following is a code piece to accept a series of numbers from the user and store them in an array.
import array as arr numbers = arr.array('i', []) num = input("How many integers do you want? ") #user tells the range(optional) #iterating till user's range is reached for i in range(int(num)): n = input("Enter a integer value: ")#asking for input of 1 value numbers.append(int(n))#adding that value to the array print(numbers)