Python Basics 1; Programming in Python & Turtle Graphics 変数に値をassignする。

Programming in Python & Turtle Graphics

後からわかったこと。

variableにobjectをassignすることは、

Frame(枠)に名前をつけて、そこにobject(コンテンツ)を入れること。

integralはobjectではありません?

 

Values and Data Types

Expressionはvalue とtypeを持っている。

assignment statementは よく使う =

Operators and Operands

operators(演算子)はvalueを繋げるもの。(), *,+,-,/

x//yはxをyで割った商

x%yはxをyで割ったあまり

 

Type Conversion Functions

float, str, int

Hard codingとは、冗長な code?

Variables

大文字小文字が区別される。

Updating Variables

x = x + yと x += y は同じ

Input

inputで得たvalue typeは数字でもstring になる。

Turtle Graphics

モジュールをインポートする。

 

モジュール?クラス?ファンクション?メソッド?

Object Oriented Concepts

turtle.Screen()は turtleというモジュールからScreenというクラス(メソッド)を呼び出すためのインスタンス

turtle.Turtle()は turtleというモジュールからTurtleというクラス(メソッド)を呼び出た目のインスタンス

turtle.Turtle()はインスタンスというまとまり(単位?)

instanceの中にあるattribution

periodで繋がっている関係だとしよう。何かがどれかにattributeしていることがperiodで表されている。

20.14. Glossary

attribute
One of the named data items that makes up an instance.

instance

An object whose type is of some class. The words instance and object are used interchangeably.

class

A user-defined compound type. A class can also be thought of as a template for the objects that are instances of it.

わからぬ。

Screen(), Turtle()はメソッドというらしい。クラスではない?

メソッドはfunctionとも言われている。

randrange()というメソッドはrandomというモジュールに含まれているfunction?

こちらではScreen(), Turtle()がクラスと言われている。

メソッドはfunctionではない。(method syntaxとdunction syntaxはargumentのあるなし。)

メソッドでできることは色々。

オブジェクトの新しいクラスを定義

Runestone Interactive
Learning Python should be fun and easy. We provide an interactive Python textbook that helps you learn to program in Pyt...

 

Importing Modules

python standard moduleに入っていないモジュールを使うときにはimportする。

python interpreter にimport statementして利用可能にする。

 

この書き方をすると、モジュールをその都度指定しなくて済む。

 

variable再考

alex = turtle.Turtle() は定義しただけでfunctionは動いていない。

 

alex = turtle.Turtle() は

alex というvariableに

  turtle.Turtle() というan instance of a classをassignするstatement

alexというvariableとして

  turtle.Turtle() というan instance of a classをobject化するstatement?

 

variable は an instance of a classをobject化する

 

 

alex.forward()は

turtle.Turtle().forward()とかいたらsemantic errors。

an instance of a classをvariableによってobject化しないと別のインスタンスで使えない?

オブジェクト.メソッド

[object]. [method]

attributes/instance variablesとは?

()がついていないもの。

メソッドでないもの。

variableはfunctionではない。

For an instance of a class that is assigned to the variable student, what is the proper way to refer to the title attribute/instance variable?

✖️ This accesses the attribute but then tries to invoke it as a method, which will fail if title is not a method.

✖️ student is the object, so it goes before the period; the attribute goes after.

✖️ student is the object, so it goes before the period; the attribute goes after.

✖️ This would be the syntax for a function named student being called on a variable named title.

✔️ Yes, this is the correct syntax to use.

 

attribute variableはmethodではない。

attribute variable はインスタンスの後ろにあるもの?

 

turtle.Screenはクラスの新しいインスタンス(まとまりを表す単位)。

呼び出さなくても良い仕様になっている?

argumentとは?

argument

A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function. The argument can be the result of an expression which may involve operators, operands and calls to other fruitful functions.

june.forward()

TypeError: forward() missing 1 required argument: distance on line 7

4.2. Modules

Note: Python modules and limitations with activecode

If you wish to explore any additional modules, you will need to run from the native python interpreter on your computer.

 

Tips

本当にランダム、ではないらしい。

 

 

The Way of the Programmer

 

Syntax, Runtime, and Semantic Errors

Syntax errorsは構造が壊れているとき(がないとか。 The compiler / interpreter がプログラムを読み込めない。

Runtime errors も読み込めないときに出るが、「迷子」の状態を指す?The interpreter.が見つける。

Here are some examples of common runtime errors you are sure to encounter:

  • Misspelled or incorrectly capitalized variable and function names
  • Attempts to perform operations (such as math operations) on data of the wrong type (ex. attempting to subtract two variables that hold string values)
  • Dividing by zero
  • Attempts to use a type conversion function such as int on a value that can’t be converted to an int

Semantic errorsは、プログラムは動いてくれるが、プログラマーが意図した結果が出ないエラー。The programmer.が見つける

 

../_images/error_dist.png

 

incremental programming

意味がわからなかった。

 

アルゴリズムとは?

Hard coding

コメント