I will write about algorithms and Python. This time, I will write not only how to calculate a simple calculation using a function, but also how to calculate it when a function is not used.
◯ About symbols for performing calculations.
operator | meaning | Example | result |
---|---|---|---|
+ | addition | 3 + 4 | 7 |
- | subtraction | 10 - 2 | 8 |
* | Multiplication | 5 * -6 | -30 |
/ | Floating point division | 5 / 2 | 2.5 |
// | Integer division | 5 // 2 | 2 |
% | Too much division | 7 % 2 | 1 |
** | ~Ride(index) | 2 ** 4 | 16 |
◯ About symbols for comparison. Returns a Boolean value of True or False.
meaning | operator |
---|---|
equal | == |
Not equal | != |
Smaller | < |
Less than | <= |
Greater | > |
Not equal | >= |
Is an element | in ~ |
◯ List comprehensions are used to efficiently create various types of lists. I would like to use various lists in this article as well, so I will write it in advance.
This is one way to make a list. It is defined in the form of ** [element for element to store in list] **. The ** element ** stored in the list is a ** expression ** that uses the element. A ** iterable object ** is an object that can retrieve elements one by one.
◯ The range () function returns an iterable object.
ex0.py
#Takes integers between 1 and 10 one by one and stores their values in the numbers list
numbers = [number for number in range(1,10)]
print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
ex1.py
#Extract integers from 1 to less than 10 one by one and store the value multiplied by 2 in the numbers list.
numbers = [number * 2 for number in range(1,10)]
print(numbers)
[2, 4, 6, 8, 10, 12, 14, 16, 18]
ex2.py
#Extract integers from 1 to less than 10 one by one and store only even values in the numbers list.
numbers = [number for number in range(1,10) if number % 2 == 0]
print(numbers)
[2, 4, 6, 8]
ex3.py
#List of strings
list_1 = ['1','2','3']
#List of strings list_Extract elements from 1 one by one, convert them to integers, and store them in the list numbers
numbers = [int(i) for i in list_1]
print(numbers)
[1, 2, 3]
ex4.py
#If the extracted element is odd, store it as it is. If it is even, square it and store it.
numbers = [i if i % 2 == 1 else i**2 for i in range(10)]
print(numbers)
[0, 1, 4, 3, 16, 5, 36, 7, 64, 9]
◯ You can easily find the total value of the elements in the list by using the sum () function.
#Prepare a list to use
numbers = [1,2,3,4,5]
#sum(list)Use like that
print(sum(numbers))
15
numbers = [1,2,3,4,5,6]
#Variable sum representing the sum value_of_Define numbers
#Nothing has been added yet, so set it to 0
sum_of_numbers = 0
#Use the for loop to get the elements of the list one by one
#range()Is'Between'In that sense, it indicates the range of iteration
#len(list)でlistの要素数(length)To get
for i in range(len(numbers)):
#Sum the retrieved elements one by one_of_Add to numbers
sum_of_numbers += numbers[i]
print('sum of numbers = ', sum_of_numbers)
sum of numbers = 21
◯ ** Point **: Iterator It retrieves and returns one element from a list, dictionary, etc. for each iteration. Lists, dictionaries, etc. are iterable objects that support iterators. Other iterable objects include strings, tuples, and sets.
◯Point: **+=, -=, *=, /=, //=, %=, = It is an abbreviation for the calculation process of substituting the calculated value. (Example)
a = 3
#a = a * 5
a *= 5
print(a)
15
◯ Used in the form of max (list) Convert the string to an integer, store it in a new list, and try to find the maximum value of the elements in it.
list_1 = ['1','2','3']
#list_1 element int()Use a function to convert it to an integer and then store it in the numbers list
numbers = [int(i) for i in list_1]
print('numbers = ',numbers)
#Max the maximum value in the list(list)Ask in the form of
print('max of numbers = ',max(numbers))
numbers = [1, 2, 3]
max of numbers = 3
◯ This is a method of comparing the elements with the provisional maximum value.
#Try defining list elements in list comprehension notation
#Store elements in the list that satisfy 1 or more and less than 11 and the remainder divided by 2 satisfies 0.
#The list is[2, 4, 6, 8, 10]Becomes
numbers = [number for number in range(1,11) if number % 2 == 0]
#First element(This time 2)To the temporary maximum value
max_of_numbers = numbers[0]
#Extract the elements of the list one by one.
#Exclude the first element because it is not a comparison target
for i in range(1,len(numbers)):
#If the compared factors are greater than the current maximum
if(max_of_numbers < numbers[i]):
#Update maximum
max_of_numbers = numbers[i]
print('numbers = ',numbers)
print('max of numbers = ', max_of_numbers)
numbers = [2, 4, 6, 8, 10]
max of numbers = 10
◯ Used in the form of min (list)
#Even-numbered values in the range 0 to less than 10 are squared before being stored in the list.
#Store odd values as they are
#The list is[0, 1, 4, 3, 16, 5, 36, 7, 64, 9]Becomes
numbers = [i if i % 2 == 1 else i**2 for i in range(10)]
print('numbers = ',numbers)
#The minimum value in the list is min(list)Ask in the form of
print('min of numbers = ',min(numbers))
numbers = [0, 1, 4, 3, 16, 5, 36, 7, 64, 9]
min of numbers = 0
◯Point: if-else- ** If-, otherwise-** means. A comparison statement that determines if the condition is met. In this case, If the element is odd (if i% 2 == 1)-if not, square the element (if even)
◯ This is a method of comparing the elements with the provisional minimum value. This time, I will make a list of random integers and select the minimum value from them.
#Import random module
import random
#Create a list of integers from 0 to 100 numbers
numbers = [number for number in range(0,101)]
#Randomly pick 10 elements from the numbers list and list them
#Find the minimum value of the elements in this list
random_numbers = random.choices(numbers,k = 10)
#First element(This time 2)To a temporary minimum value
min_of_random_numbers = random_numbers[0]
#Extract the elements of the list one by one.
#Exclude the first element because it is not a comparison target
for i in range(1,len(random_numbers)):
#If the compared factors are less than the current minimum
if(min_of_random_numbers > random_numbers[i]):
#Update the minimum value
min_of_random_numbers = random_numbers[i]
print('random_numbers = ',random_numbers)
print('min of random_numbers = ', min_of_random_numbers)
random_numbers = [33, 99, 33, 27, 25, 42, 19, 37, 14, 15]
min of random_numbers = 14
◯ ** Point **: random module
This time, we will use the choices (references, number of elements) function
in the random module.
You can use the choices () function to create a list of the number of elements specified by k =
.
Also, the elements of the list to be created will be randomly selected elements from the ones to be referenced (in this case, the numbers list).
Reference article: choice, sample, choices to randomly select elements from a list in Python
Thank you for reading. Next time, I would like to write the calculation of basic statistics Part2 (mean, median, mode). I would be grateful if you could point out any mistakes or improvements.