Hi, my name is Riku. To briefly introduce myself, I usually work as a consultant, and programming is a basic hobby of writing code. Even so, when using a large amount of data that cannot be done with Excel etc. or when performing advanced analysis, I sometimes write code even at work, and nowadays when data utilization is becoming one business trend, there are more opportunities to write code. It's on the way. For that reason, it is not always essential for the profession of a consultant, but even from a junior, "I don't want to be an engineer, but I want to be able to proceed with analysis myself, so please teach me programming for data analysis!" It is often said that. So, when I searched for various articles for beginners, I noticed that there are few articles that systematically summarize how to handle Python for super beginners who do not know the programming character in Japanese, and I am confident in my knowledge. I decided to give a Python explanation for beginners with zero knowledge here as well as practice of communication. This article is intended to be a bridge between beginners and beginners in Python, and will be four or five articles. The contents will cover basic programming methods such as variables and types, how to use packages, and how to use NumPy. We hope that these articles will help beginners as much as possible.
Python is one of the programming languages created by Dutch programmer Guido van Rossum. Originally developed as part of a hobby, the language is now used by a large number of engineers due to its open source, free usage and easy package construction. At first glance, it is easy to think that a programming language is used for developing some kind of application, but this language called Python is also a language that can be used as a weapon for advancing advanced analysis, and this article focuses on data analysis. I'm going to give you a guess.
Now let's write the code! Well, you might wonder if nothing has been explained yet, but programming is a more familiar world than learning. Let's start with a method for getting Python to do calculations with a simple calculator. For the time being, please refer to the table below for details.
Thing you want to do | code | code記入例 | Output for entry example |
---|---|---|---|
addition | + | 5+8 | 13 |
subtraction | - | 13-5 | 8 |
multiplication | * | 9*6 | 54 |
division | / | 8/5 | 1.6 |
Exponentiation | ** | 4**2 | 16 |
remainder(mod) | % | 18%7 | 4 |
display | print() | print(13) | 13 |
Now let's actually write the code. Try writing code that does the following calculations: Question 1. Have 7 + 10 calculated and display the calculation result Q2.5 Have 5/8 calculated and display the calculation result Q3.8 Calculate the cube of 8 and display the calculation result.
The answer is as follows.
#Answer to Question 1
print(7+10)
#print()Is "()It is a command to "display what is inside", so if you write the above code and run the program, it will return the answer of 17.
#Answer to Question 2
print(5/8)
#Similarly, if you run the above code, it will be 0.It will return the answer 625.
#Answer to Question 3
print(8**3)
#Similarly, it is a code that returns 544, which is the cube of 8.
For the time being, could you say, "You can do something like a calculator with Python!"
Now, if you want to calculate a simple formula that requires only one line, you can do it now. However, the more complex the analysis, the more likely you are to want to "reuse" the formula once it has been calculated. For example, if you want to use the calculation result "(3 + 4-5 ** 5 + 8/2 + 3-1 * 55) /88*12+45-652**2" in later analysis, this every time. It's hard to write an expression, isn't it? Also, assumptions may change in the analysis. For example, I was conducting an analysis at 110 yen to the dollar, but I would like to know what the result will be when it reaches 115 yen to the dollar. However, if the number "110" appears 100 times in the whole code at that time, it is very troublesome to correct all of them. That's where variables </ b> come in.
For example, let's say you import a $ 2 product A from the United States. The cost at this time is the product price plus 13% tariff. And the current market price is 110 yen to the dollar. When calculating the cost in Japanese Yen in this case, the conventional method
print(2*110*1.13)
It can be calculated by writing. So what happens when you use variables?
price = 2
exchange = 110
tax = 0.13
totalprice = price*exchange*(1+tax)
print(totalprice)
Can be described as. The number 2 (dollar) is assigned to the variable price, the number 110 (yen / dollar) is assigned to the exchange, the number 0.13 is assigned to the variable tax, and the formula of multiplying these is assigned to the variable total price. is. By doing this, if the price of goods changes after that, you can see the new result by running the code again by playing with the price, if the exchange rate fluctuates, the exchange, and if the tax rate changes, by playing with the tax number. I can do it. Also, if you want to use the price of this item for another analysis, you can just enter total price and it will refer to the price without you having to type "2 * 110 * 1.13".
Now, what we need to handle variables is the concept of data types </ b>. The data type is literally the type of data. For example, the data type of the data "2" assigned to price earlier is called "numeric type". Until now, we have dealt only with numerical values, but in future analysis, we will frequently deal with non-numerical information. A typical data type other than the numeric type is the "character type". For example, the data "price" is a character type, and the number "3" is also a character type data if you keep it as a character string instead of a number. For example, if you assign 13 (numeric type) to the variable a and 2 (numeric type) to the variable b, you can calculate 13 + 2 with a + b. However, if you substitute the character string 3 for the variable c, you cannot add the character string and the number, so even if you calculate a + c, you cannot calculate 13 + 3. (By the way, if 5 of the character string is assigned to the variable d, c + d is the addition of the character strings, so instead of 8 of 3 + 5, "3" + "5" is called "35". It will be a character string.) Now, let's check the type of the variable tax used earlier. You can find out the type by typing type ().
tax = 0.13
type(tax)
Running the above code will return a "float" result. This means that tax is of type float. The float type is a type that represents a number (including a decimal point).
Typical types are as follows. There are many other types, but it's okay to keep the following types in mind at the beginning.
Mold | Example | |
---|---|---|
Numerical value(integer) | int | 13 |
Numerical value(Floating point number) | float | 13.54 |
String | string | price |
Boolean(yes or no) | boolean | True |
The type of each variable depends on what you store in that variable. See the example below.
price = 2
#Since it stores an integer, it is an int type
tax = 0.13
#Float type because it stores a small number
mark = "price"
#Since it stores a character string, it is a string type
check = True
#Since it stores True, it is a boolean type.
Now, did you understand the concept of variables and types? Next time, we plan to update it in about a week.
Recommended Posts