Aside from autorun and double-click execution, execute interactively
Start with command prompt / PowerShell
C:\>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
Hello World
Just hit the command
C:\>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print("Hello Python!!")
Hello Python!!
>>>
Variable assignment
Variable declaration / assignment / can be used many times / can be overwritten
>>>###Calculate by substituting 10 for the variable hogeo
>>> hogeo=10
>>>
>>> hogeo+5
15
>>>###Variables can be retrieved many times
>>> hogeo *20
200
>>>###Variables can be overwritten
>>> hogeo=5
>>> hogeo+10
15
>>>
Isn't there a type or declaration? I had a question
Variables have no type. The assigned value itself has a type. is what they said
Concept of variable rules and naming conventions
The following variables are NG
Reserved words (reserved words if or NG in Python)
Variable name starting with a number (1a or NG)
Naming convention (Imitating a person's naming convention should make it more readable!)
Consists of English words and separates the words with "_"
Example) Consumption tax: tax_rate
Do not use uppercase alphabets (use constants below)
Do not start with _
Nodame cantabile abc or a
Constant (variable that cannot be overwritten in other languages)
Python allows you to override constants. This is because Python does not have a function called constants.
It seems that it is common to say "variables with only uppercase letters and numbers are treated as constants". <<< This kind of thing is quite important, isn't it?
>>>###A crappy example of price calculation
>>> price=100
>>> price*1.1
110
>>>
>>>###A good example using constants
>>> price=100
>>> TAX_RATE=1.08
>>> price * TAX_RATE
108.0
>>>
String
"'" "" "Enclose in single or double quotes
In this book, I will enclose it in "'" single quotes after this, so I wonder if this is better
Seems to use each other when dealing with "'" and "" "as strings
String manipulation
>>> firstname='hogeo'
>>> lastname='matsumo'
>>> name=firstname+' '+lastname
>>> print(name)
hogeo matsumo
>>>
>>> price = 100
>>> 'Price:' + price + 'yen'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>>
>>> price = 100
>>> 'Price:' + str(price) + 'yen'
'Price:100yen'
>>>
Summary of basics
Recommended Posts