[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]

Introduction

Python provides a variety of built-in functions and methods. Among them, I have summarized the ones that I use frequently.

Overview

The task of Tommy's blog was very helpful in understanding Python, so I will proceed with this article in the form of a task. If you have any questions, please refer to the detailed explanation at the link.

Target person

This article is for the following people.

environment

Implementation details

Exercise 1 Calculate the average amount of input amount

Technical elements

Outline of specifications

Output image

スクリーンショット 2020-04-09 5.05.39.png

Implementation example

#Prepare a list to store input values
money_list = []

#Store average amount
average = 0
while True:
    #Store input value
    money = int(input("Please tell me your money:"))   
    if money == 0:
        break
    #Add the amount entered in the list
    money_list.append(money)

#Consider division by zero
if len(money_list) !=0:
    #Calculate the total value
    s = sum(money_list)
    #The size stored in the list(Here the number of elements)
    n = len(money_list)
    #Calculate the average value
    average = s / n
    
print("The average amount of money that everyone has",average,"is.")

Exercise 2 Count all the characters in English words

Outline of specifications

Specification details

Output image

スクリーンショット 2020-04-09 5.08.23.png スクリーンショット 2020-04-09 5.09.17.png

Implementation example

alphabet_code= dict.fromkeys(list("abcdefghijklmnopqrstuvwxyz"), 0)
words = []

#Enter English words and add the number of characters
while True:
    word = input("Please enter and specify English words:")
    if word == "":
        break
    words.append(word)
    for letter in word:
        if letter in alphabet_code.keys():
            #Add if you have a key(If not judged, key error will occur)
            alphabet_code[letter] += 1

print('')
print('The words entered are:')
words.sort()
for word in words:
    print('・' + word)

print('')
print('The result of the number of alphabet appearances is as follows')
for letter , count in alphabet_code.items():
    print("{0}\Number of occurrences of t: {1}Times".format(letter, count))

Exercise 3 Output the result of the specified Fibonacci sequence

Technical elements

Outline of specifications

Usage details

Output image

スクリーンショット 2020-04-09 5.15.03.png

Implementation example

def fib_func(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib_func(n - 1) + fib_func(n - 2)

target_num = int(input('What number in the Fibonacci sequence do you represent?: ') )
print(fib_func(target_num))

Reference information

This article is based on Tommy's blog Roadmap article. If you want to know more details or other ways to use it, we recommend learning from the link. Not only the tasks but also the know-how for learning Python from scratch is open to the public, so please take advantage of it!

Recommended Posts

[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Linux is something like that in the first place
This and that of the inclusion notation.
Talking about the features that pandas and I were in charge of in the project
12. Save the first column in col1.txt and the second column in col2.txt
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
Summary of built-in methods in Python list
Summary of probability distributions that often appear in statistics and data analysis
Summary of how to import files in Python 3
List of frequently used built-in functions and methods
Summary of how to use MNIST in Python
[Python] Summary of functions that return the index that takes the closest value in the array
Summary of tools needed to analyze data in Python
Summary of date processing in Python (datetime and dateutil)
[Introduction to element decomposition] Let's arrange time series analysis methods in R and python ♬
[python] Summary of how to retrieve lists and dictionary elements
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
Summary of Python implementation know-how and tips that AI engineers want to be careful about
Determine the date and time format in Python and convert to Unixtime
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Summary of Chapter 2 of Introduction to Design Patterns Learned in Java Language
[Python3] I made a decorator that declares undefined functions and methods.
[Introduction to Python3 Day 1] Programming and Python
Summary of character string format in Python3 Whether to live with the old model or the new model
[Notes / Updated from time to time] This and that of Azure Functions
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language
Regular expressions that are easy and solid to learn in Python
[Introduction to Python] I compared the naming conventions of C # and Python.
Summary of Chapter 3 of Introduction to Design Patterns Learned in Java Language
How to use functions in separate files Perl and Python versions
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Precautions when passing def to sorted and groupby functions in Python? ??
This and that of python properties
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[ROS2] How to describe remap and parameter in python format launch
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Dynamically define functions (methods) in Python
Introduction to TensorFlow-Summary of four arithmetic operations and basic mathematical functions
[Introduction to Udemy Python 3 + Application] Summary
A class that summarizes frequently used methods in twitter api (python)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
Summary of Python indexes and slices
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
Modules of frequently used functions in Python (such as reading external files)
Implementation of particle filters in Python and application to state space models
How to format a list of dictionaries (or instances) well in Python
[Introduction to Python] Thorough explanation of the character string type used in Python!
[Introduction to Udemy Python3 + Application] 18. List methods
[Python] Summary of how to use pandas
Summary of methods often used in pandas
General Theory of Relativity in Python: Introduction
Summary of frequently used commands in matplotlib
Bind methods to Python classes and instances
[Introduction to Udemy Python3 + Application] 55. In-function functions
Summary of various for statements in Python