Installing Python 3 on Mac and checking basic operation Part 1

Introduction

I'm a complete beginner in Python, so there are many things I don't know, but I'm going to start with Hello World and do various things.

If you are familiar with Python and have any suggestions, please do not hesitate to contact us!

I think that the OS is Mac and the Python version is 3 assuming.

Installation

Python 2 is already installed on Mac. If you check the version, it will be displayed as follows.

python --version
Python 2.7.10

However, since the development of the 2nd system has already been completed, we will change it to the 3rd system.

I installed it from pkg. https://www.python.org/downloads/ (You can also install via brew)

After installing, let's check the version.

python3 --version
Python 3.5.0

In this state, the 2nd and 3rd systems coexist. However, if you make a mistake, don't delete the second series of Python here.

If you delete it as shown below, it will probably be a big problem. http://docs.python.jp/3/using/mac.html

It is used by Apple and third-party software and should not be edited or deleted.

Hello World It is a display of Hello World, the gateway to the iron plate.

Write the following in an editor and save it as hello.py.

hello.py


print ("Hello World")

Go to the directory where hello.py was saved

python3 hello.py

And run

Hello World

Should be displayed.

Now that Hello World is displayed, I would like to lay the foundation for actually writing the code.

Interactive mode

I created and ran hello.py to display Hello World, but other than this, I can do the same with just the command line. That is interactive mode.

At the terminal

python3

Enter. Then you will see the following display.

Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

If this is in the state of ``` >>> , the interactive mode is started.

Now, type `print ('HELLO')` and enter, you should see HELLO.

>>> print('HELLO')
HELLO

In addition to displaying character strings, you can also perform calculations.

>>> 10+10
20

When to finish

quit()

Variable declaration

Although it is a Python variable, it seems to be dynamically typed, so you can use it without predefining types such as strings and numbers.

#Set a variable as a number
num1 = 1
num2 = 2
# 1+2
result = num1+num2
#output
print(result)

#Set a variable as a string
string1 = 'a'
string2 = 'b'
# ab
concat = string1 + string2
#output
print(concat)

Result is

3
ab

String

A string is recognized as a string by enclosing it in single quotes ' or double quotes " .

Numerical value

The following numeric types are supported. Integer (int) Floating point number Complex

list

It is used when combining multiple values. Enclose it in [] and separate the values with , .

In the following example, A, B, C are put in the list called listsample, and all the lists are returned.

(Run in interactive mode)

>>> listsample = ['A','B','C']
>>> listsample
['A', 'B', 'C']

You can also specify the index.

>>> list = ['A','B','C']
>>> list[0]
'A'

constant

It seems that there are no constants in Python. In general, it seems that things defined by uppercase letters and underscores are treated as constants, such as TEST_VALUE.

Tuple

Although it is not a constant, there is a definition method that cannot change the element of an object called a tuple.

(Run in interactive mode)

#Definition of tuple
>>> taplesample = (100,200,300)
>>> taplesample
(100, 200, 300)

#When the index is specified, the 0th 100 is displayed.
>>> taplesample[0]
100

#This will result in an error
>>> taplesample[0] = 101

Similarly, if you define 100,200,300 in the list and change the 0th 100 to 101, this can be rewritten.

>>> samplelist = [100,200,300]
>>> samplelist[0] = 101
>>> samplelist
[101, 200, 300]

Comment out

Comment out a single line with #

In the case of multiple lines, if you enclose three single quotation marks ' or double quotation marks " side by side, multiple lines will be commented out.

print('indicate')

'''
print('Do not show')
print('Do not show')
'''

in conclusion

This time, I even displayed Hello World and manipulated simple values. I will write it as part 2 in the future.

Recommended Posts

Installing Python 3 on Mac and checking basic operation Part 1
Notes on installing Python on Mac
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Python basic operation 3rd: Object-oriented and class
Notes on building Python and pyenv on Mac
[Note] Installing Python 3.6 + α on Windows and RHEL
Try importing MLB data on Mac and Python
Basic operation of Python Pandas Series and Dataframe (1)
Install Python on Mac
Install Python 3 on Mac
Installing pandas on python2.6
Python basic memorandum part 2
Python basic memo --Part 2
Install Python 3.4 on Mac
Python basic memo --Part 1
Install selenium on Mac and try it with python
Python application: Pandas Part 1: Basic
Notes on installing dlib on mac
Install pygame on python3.4 on mac
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing pipenv on Mac
Catalina on Mac and pyenv
Notes on installing Python on CentOS
Python (Python 3.7.7) installation and basic grammar
Building a Python environment on a Mac and using Jupyter lab
Building a Python environment on Mac
If python on mac goes missing
Notes on Python and dictionary types
About installing Pwntools and Python2 series
Java and Python basic grammar comparison
perl objects and python class part 2.
MQTT on Raspberry Pi and Mac
Build a Python environment on your Mac with Anaconda and PyCharm
Support when installing pillow on python3.9
[python] week1-3: Number type and operation
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Install python3 on Mac (El Capitan)
Integrate Modelica and Python on Windows
Installing Kivy-Designer on Windows10 64bit Python3.5
Python Basic Course (14 Modules and Packages)
Notes on installing Python using PyEnv
Environment construction of python3.8 on mac
perl objects and python class part 1.
The road to installing Python and Flask on an offline PC
If Python 3.5.0 installation fails on Mac
Basic Python operation 2nd: Function (argument)
Install lp_solve on Mac OS X and call it with python.
Python application: data visualization part 1: basic
[Automation with python! ] Part 2: File operation
Steps to install python3 on mac
A python script that deletes ._DS_Store and ._ * files created on Mac
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
Try to build python and anaconda environment on Mac (by pyenv, conda)
Put MeCab binding for Python with pip on Windows, mac and Linux