Lists, functions, for, while, with (open), class and learning supplements up to the last time (Python beginners after learning Ruby)

list

test.py


list = ["0", "1", "2"]
#Close to Ruby's array handling

list[0]
#0th"0"Is taken out.

function

test.py


def test(arg):
    return arg + 1

print(test(0))

#The function is test and the argument is arg.
#In the case of a function, the return value by return is required.

In the above case, enter * arg * of the function * test * with * 0 * of * test (0) * as an argument,

for statement

test.py


for index in range(100)
    print index

#Output numbers from 0 to 99 times

for i in range(1,101)
    print index

#Specify the range from 1 to 100 times
#It is customary to use index or i between for and in.

#For statement by list
num = 0
list = ["0", "1", "2"]
for item in list:
    print list[num]
    num += 1

#Everything is done for the contents of the list. In the case of a list, item is usually between for and in.

while statement

test.py


num = 1 #Initialize counter variable
while num <= 10:
    print(num)
    num += 1 #Update counter variable

The * num * part before * while * is called the counter variable, and it is handled by the conditional expression after * while *. If you do not update num in the while statement and specify something that does not apply to the conditional expression, it will loop.

with(open)

test.py


# with open('File to reference', 'Execution process called mode') as file:
#Description of the process to be executed

For example, if there is a test.txt file in the directory directly above test.py ./test.txt You can specify the reference destination by writing. In addition, in the execution process called mode, ** r **, which is an acronym for read, and ** w **, which is the acronym for write, is the execution process that skips the mode. (Detailed content will be learned from the next time onwards)

class

test.py


#class Class name (uppercase):
#    def __init__(self,Property 1,Property 2, ...,):
#        self.Property 1=Property 1
#def method name(self):
#return Process to be executed

#Example
class Card: #Class called Card
    def __init__(self, date, user_name): 
        self.date = date
        self.user_name = user_name
    def message(self): #The function in the class is called a method, this time the method called message
        return self.user_name + self.date #Method return value

date_a = '2020-01-01' 
user_name_a = 'Test'

card_a = Card(date_a, user_name_a) #Card in the Card class_Create an instance called a

print(card_a.message()) #card_The message method is executed for a and print is output.

Supplement up to the last time

(1) When I wrote the code in VScode and prepared an environment where it can be executed in the terminal, an error occurred when I executed it, so a memorandum. If you do not describe # coding: utf-8, you will get an error due to using Japanese. ② Reserved words are also in Python ③ You can tell whether it is str type or int type by print (type (what you want to check)). ④ There is a Boolean type that is neither str nor int. Example: T = True, N = False ⑤ You can search for modules from the outside by searching with pypi for modules that can be imported. Famous places like NumPy, Pandas, Flask, Django, etc.

Recommended Posts

Lists, functions, for, while, with (open), class and learning supplements up to the last time (Python beginners after learning Ruby)
About adding / deleting lists (Python beginners after learning Ruby)
Tips for Python beginners to use the Scikit-image example for themselves 8 Processing time measurement and profiler
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
[Python] [Machine learning] Beginners without any knowledge try machine learning for the time being
How to get the date and time difference in seconds with python
I measured the speed of list comprehension, for and while with python2.7.
Turn multiple lists with a for statement at the same time in Python
Python learning notes for machine learning with Chainer Chapters 11 and 12 Introduction to Pandas Matplotlib
The fastest way for beginners to master Python
Causal reasoning and causal search with Python (for beginners)
About list processing (Python beginners after learning Ruby)
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
The first step of machine learning ~ For those who want to implement with python ~
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Until you can install blender and run it with python for the time being
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
[Python] Measures and displays the time required for processing
Python # How to check type and type for super beginners
[Python] How to play with class variables with decorator and metaclass
How to learn TensorFlow for liberal arts and Python beginners
Try to bring up a subwindow with PyQt5 and Python
Tips for Python beginners to use the Scikit-image example for themselves
[Introduction to Reinforcement Learning] Reinforcement learning to try moving for the time being
This and that for using Step Functions with CDK + Python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.