Learning notes from the beginning of Python 2

comment

Comments are from # to the end of the line. However, # written in the character string does not function as a comment.

String

Description method for generating a character string (literal)

Enclose it in single quotes ' or double quotes " to get a string.

"sample" #This is a string

Here document

Use 3 double quotes.

Below is the content of the edit request from * shiracamus * Reflected.

sample = """line1
line2
line3"""

In the above description, the variable sample is associated with the string"line1 \ nline2 \ nline3".

String concatenation

Use the + symbol.

sample = "a" + "b"
print(sample) #=> ab

String replacement

Call the replace method on the string.

sample = "a" + "b"
print(sample) # => ab
sample.replace("b", "c") #=> ac

Splitting strings

Call the split method on the string.

sample = "a" + "1" + "b"
print(sample)
a1b
print(sample.split("1")) #=>["a", "b"]

list

A container that can store multiple data. Think of something like a long box with a partition.

What's in the box starts at 0, which is attached to the partition. Identified by ascending number.

The list where the strings "0", "1" and "2" are in the dividers 0, 1 and 2 is as follows: It can be created by writing.

["0", "1", "2"]

Enclose the list (box) in [] and separate it with commas. The components in the list can be described as they are.

If you want to put 0 in the character string, write "0", and if you want to put 0 in the numerical value, write 0.

The beginning of the component can be accessed by the number 0 (index) The next component can be accessed by the number 1 and the next component by the number 2.

Access example by index when the list is assigned to the variable a


a = ["0", "1", "2"]

print(a[0]) #=> "0"

a[0] = "a"

print(a[0]) #=> a

For things (objects) that you want to access in order like a list When accessing in order, there is a convenient description method.

a = ["0", "1", "2"]

for i in a:
    print(i)
  
#Execution result
# 0
# 1
# 2

First, associate the first element (index 0) of the list (object) of a with the variable i. Execute print (i). Then associate the next element (index 1) in the list of a with the variable i Execute print (i). Finally, associate the next element (index 2) in the list of a with the variable i. Execute pirint (i).


Variables and constants

variable

A name tag attached to a container to put a value in.

Name the container ʻaand enter the valuecontents`.

a = "contents"
print(a) #=>contents

constant

There are no constants in python. By convention, all variables that do not change their value are written in uppercase and are written as constants.

SAMPLE_VALUE = 986
print(SAMPLE_VALUE) # => 986

Method

What you can call a process with a name.

Definition method

Use def.

def sample():
	print("hello")

sample() #=> hello

The above defines a method named sample. The content of the method is the print ("hello") part.

In Python, the beginning and end of a block is marked by the : at the end of the line and the presence or absence of indentation from the next line. Express.

The indentation depth of the next line (in Python, the standard is four spaces, representing one indent) When a line appears that is indented shallower than the standard, Judge that the line before that is a block. In other languages, there are characters, strings, and expressions that represent blocks, In the case of Python, it is very characteristic to distinguish blocks by the depth of indentation.

It feels strange, but once you get used to it, it doesn't bother you at all. If you don't mind it, this one will be cleaner and easier to see.

In Python, when defining and calling a method, You must put the parentheses firmly.

argument

What is written in parentheses after the method name It is called an argument.

Formal argument, when defining the method The argument when calling the method is called the actual argument.

Formal arguments are at the stage of defining the method (temporary), so I don't know the specific value (it hasn't been decided). Specify the variable name in the formal argument.

Since the actual argument is specified at the stage of calling the method, You will specify the actual value that is specifically determined.

def sample(string): #string is a formal argument
    print(string) 
    
sample("hello") # "hello"Is the actual argument

#Execution result
# hello

Return value

The output from the method is received as a return value.

The return value is returned using return in the method.

def sample(string) :
	return string

a = sample("hello")
print(a) #=> hello

Block structure

In other languages, blocks are represented by enclosing them in {, } symbols.

def sample() {
  puts "this is sample1"
  puts "this is sample2"
}

In python, the block structure is represented by indentation.

def sample() :
    print("this is sample1") # => this is sample1
    print("this is sample2") # => this is sample2

Indent

Indentation is the distance from the left side of the source.

The same block must be indented the same number of rows on each row in the block.

def sample():
    print("sample1")
        print("sample2") #Wrong because the number of indents is different from the number of indents in the line above

Recommended Posts

Learning notes from the beginning of Python 1
Learning notes from the beginning of Python 2
Learn Nim with Python (from the beginning of the year).
Study from the beginning of Python Hour1: Hello World
Study from the beginning of Python Hour8: Using packages
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
Python learning notes
python learning notes
Omit BOM from the beginning of the string
The beginning of cif2cell
the zen of Python
Get the contents of git diff from python
The story of low learning costs for Python
Finding the beginning of Abenomics from NT magnification 2
Finding the beginning of Abenomics from NT magnification 1
The wall of changing the Django service from Python 2.7 to Python 3
Towards the retirement of Python2
O'Reilly python3 Primer Learning Notes
Basics of Machine Learning (Notes)
[Python] Get the text of the law from the e-GOV Law API
About the ease of Python
Mathematical understanding of principal component analysis from the beginning
Get the return code of the Python script from bat
Python points from the perspective of a C programmer
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Python data analysis learning notes
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
Summary of the basic flow of machine learning with Python
[Maya Python] Crush the contents of the script 2 ~ list Notes
Beginning with Python machine learning
The Power of Pandas: Python
DJango Note: From the beginning (simplification and splitting of URLConf)
The result of Java engineers learning machine learning in Python www
Different from the import type of python. from A import B meaning
What beginners learned from the basics of variables in python
The story of Python and the story of NaN
Notes on using MeCab from Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Use the Flickr API from Python
Notes on accessing dashDB from python
About the basics list of Python basics
Learn the basics of Python ① Beginners
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
[Python] Try to graph from the image of Ring Fit [OCR]
I want to output the beginning of the next month with Python
[Python] Get the update date of a news article from HTML
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Important unit seen from the Python lecture materials of Kyoto University
Carefully derive the interquartile range of the standard normal distribution from the beginning
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
Change the length of Python csv strings
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
Notes on PyQ machine learning python grammar
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module