Minimum grammar notes for writing Python

Notes for beginners!

Reserved word

Avoid reserved words as they are used in built-in functions such as list, abs, str! See below for details. http://qiita.com/cm3/items/6a856c44dd92632aa54f

constant

bool operation

bool


a and b         #True if both a and b are true
a or b          #True if a or b is true
not a           #True if a is false

None processing discrimination

none


if hoge is None:
  #Processing when None
  
if hoge is not None:
  #When not None

Array (list)

array


values = [1, 2, 3, 4]
print values[0] #1
print "length=" + str( len(values) ) #length=4

push


list = ["A", "B", "C"]

list.append("D")
print list     # ["A", "B", "C", "D"]

Other related functions http://www.pythonweb.jp/tutorial/list/

Object (dict)

dict


d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}

dict


for k, v in d.items():
    print k, v             # Tanaka 80, Yamada 30, Suzuki 40

for k in d.keys():
    print k, d[k]          # Suzuki 40, Yamada 30, Tanaka 80

for v in d.values():
    print v                # 80, 30, 40

for k, v in d.iteritems():
    print k, v             # Tanaka 80, Yamada 30, Suzuki 40

for statement

for


#Loop from 0 to 9
for i in range(0, 10):
	print "hoge " + str(i)

for


#Scan the contents of the array
values = [ "hoge", "fuge", 123 ]
for value in values:
	print value

if statement

if


if hoge < 1: 
	print "fuga1"
elif hoge < 2:
	print "fuga2"
else:
	print "fuga3"

cast

cast


print "hoge" + str( 100 ) #hoge100
print 10+int("5") #15

Round the numbers

floor


import math
math.floor(x)	 
math.ceil(x)

function

function


def hoge(a,b):
    print "hoge";
    return "hoge" + a + b
    
#call
hoge("AAA","BBB")#hogeAAABBB

__name__

A special variable __name__ that you often see. In the case of the main program, it will be __name__ ==" __main__ ". When you import, the file name is entered. It seems that the following notation is often used to distinguish them.

__name__


if __name__ = "__main__":
	print "It's the main program"
#else:
#	print "It's imported"

use json

How to use http://www.python-izm.com/contents/application/json.shtml Load and save http://d.hatena.ne.jp/fenrifja/20130306/1362571700

Write class

http://www.tohoho-web.com/python/class.html#class

About import

http://python.matrix.jp/pages/tips/import.html

reference

http://www.tohoho-web.com/python/index.html

Recommended Posts

Minimum grammar notes for writing Python
Python grammar notes
Python Tkinter notes (for myself)
Basic Python grammar for beginners
[Python] Sample code for Python grammar
Notes on writing config files for Python Note: configparser
Personal notes for python image processing
Notes for me python csv graph
Notes for Python file input / output
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes on PyQ machine learning python grammar
Notes on nfc.ContactlessFrontend () for nfcpy in python
Template for writing batch scripts in python
Notes for using python (pydev) in eclipse
2016-10-30 else for Python3> for:
python [for myself]
Python scraping notes
Python study notes _000
Python beginner notes
Python study notes_006
Basic Python writing
python C ++ notes
Python study notes _005
Python Library notes
Python3 basic grammar
python personal notes
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Instant method grammar for Python and Ruby (studying)
Dart grammar for those who only know Python
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Notes for implementing simple collaborative filtering in Python
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo # 5 for Python beginners to read "Detailed Python Grammar"
About Python for loops
Python basic grammar / algorithm
Write about building a Python environment for writing Qiita Qiita
missingintegers python personal notes
Python basics ② for statement
Python ~ Grammar speed learning ~
Python package development notes
Before writing Python code
About Python, for ~ (range)
python decorator usage notes
Python ipaddress package notes
Python basic grammar (miscellaneous)
python textbook for beginners
Refactoring tools for Python
[Personal notes] Python, Django
Python Pickle format notes
[Python] pytest-mock Usage notes
[Introduction for beginners] Reading and writing Python CSV files
python for android Toolchain
First Python miscellaneous notes
Python basic grammar note (4)