I tried to touch Python (basic syntax)

Introduction

I tried to touch Python (installation) continued

Things to check & do in advance

--Japanese characters are garbled when connecting to a virtual machine with SSH. ――When I looked it up, I was surprised that the following article was in trouble! -What to do if garbled characters on CentOS under SSH environment

Virtual machine


#Status check → VC Keymap is us
$ localectl status
   System Locale: LANG=ja_JP.utf8
       VC Keymap: us
      X11 Layout: n/a
#Updated VC Keymap to jp106(Run with sudo)
$ sudo localectl set-keymap jp106
#Check the status again → VC Keymap updated to jp106
$ localectl status
   System Locale: LANG=ja_JP.utf8
       VC Keymap: jp106
      X11 Layout: jp
       X11 Model: jp106
     X11 Options: terminate:ctrl_alt_bksp
#Check if Japanese locale can be used → There is no ja in the first place! ??
$ localectl list-locales | grep -i ja
#Added Japanese locale
$ sudo localedef -f UTF-8 -i ja_JP ja_JP
#Make sure the Japanese locale is enabled
$ localectl list-locales | grep -i ja
ja_JP
ja_JP.utf8

Learn basic syntax

Execute a normal program

Let's create and run the following program to learn the basic syntax.

Command execution


# .Create py file
$ vi test02.py

test02.py


print('Module loading')

def test():
    print('Function: called test')

if __name__ == '__main__':

    print('python-izm')
#   print('Python ism')
    test()

Command execution


#Created using Python commands.Run py file
$ python test02.py
Module loading
python-izm
Function: called test

UTF-8 is applied by default for character encoding of 3 series, so you do not have to be aware of it.

Execute anomalous program (indent)

Python uses indentation for its block structure. Since {} is not used, an error will occur if the indentation is not consistent.

Command execution


# .Create py file
$ vi test03.py

test03.py


print('Module loading')

def test():
        print('Function: called test')

#Execute only when the target program is started as a script
#Not executed when importing etc.
if __name__ == '__main__':

      print('python-izm')
#   print('Python ism')
    test()

Command execution


#Created using Python commands.Run py file
$ python test03.py
  File "test03.py", line 10
    test()
         ^
IndentationError: unindent does not match any outer indentation level

IndentationError: unindent does not match any outer indentation level IndentationError: unindent does not match any outer indentation level

The error message that google teacher says is the above.

in conclusion

You only have to be careful about the block structure of the indent, I've touched the GO language before, so I didn't feel any discomfort.

As for the future flow, I will write an article if I can make something after implementing the basic and advanced editions! (Because it is easier to remember character strings and if statements than to write articles)

Recommended Posts

I tried to touch Python (basic syntax)
I tried to touch jupyter
I tried to touch the CSV file with Python
I tried to touch Tesla's API
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to implement ADALINE in Python
I tried to touch the COTOHA API
I tried to implement PPO in Python
[Python] I tried to calculate TF-IDF steadily
I tried Python> autopep8
I tried Python> decorator
I tried to touch the API of ebay
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to touch Python's GUI library "PySimple GUI"
I tried to implement TOPIC MODEL in Python
I tried to automate sushi making with python
I tried to implement selection sort in python
I tried fp-growth with python
I tried to graph the packages installed in Python
When I tried to introduce python3 to atom, I got stuck
I tried to learn PredNet
I tried to summarize how to use matplotlib of python
I tried to summarize the basic form of GPLVM
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
I tried to organize SVM.
I tried to implement a pseudo pachislot in Python
Continuation ・ I tried to make Slackbot after studying Python3
I tried to implement PCANet
I tried to get started with blender python script_Part 02
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to automatically generate a password with Python3
I tried to reintroduce Linux
I tried Python C extension
[Python] I tried using OpenPose
I tried to introduce Pylint
I tried to summarize how to use pandas in python
I tried to solve the problem with Python Vol.1
I tried to summarize SparseMatrix
I tried gRPC with Python
I tried scraping with python
I learned Python basic grammar
[Python] I tried to get Json of squid ring 2
I tried to implement StarGAN (1)
I tried to access Google Spread Sheets using Python
I tried to summarize the string operations of Python
I tried to solve AOJ's number theory with Python
I tried to implement a basic Recurrent Neural Network model
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to create API list.csv in Python from swagger.yaml
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python