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.
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.
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()
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
A string is recognized as a string by enclosing it in single quotes '
or double quotes "
.
The following numeric types are supported. Integer (int) Floating point number Complex
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'
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
.
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 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')
'''
This time, I even displayed Hello World and manipulated simple values. I will write it as part 2 in the future.
Recommended Posts