Python学習入門ノート-基礎知識


今日からPythonを勉強します.
今日は主にPythonの基礎知識を学び、主に以下の内容をカバーしています.
1、数値と式
Pythonインタラクティブ解析器では,直接演算が可能である.
一般的な数学関数はmathブロックを導入することができ,いくつかの複雑な数学関数はcmathブロックを導入する必要がある.Pythonはロング整形データ:10000000 Lをサポートしています.整数演算の2つの方法:(1):math.floor(X) (2) int(X)
Pythonでは、8進数と16進数は以下のように書かれています.
 0xAF , 010...
最初の数字はすべてゼロです.
2、変数
Pythonの変数には、アルファベット、数字、下線を含めることができますが、数字で始まることはできません.変数を使用する前に変数を付与し、付与されていない変数は意味がありません.
3、ユーザー入力の取得
2つの方法:inputとraw_input、両者の違いは、inputが偽象で取得する入力が合法的なPython式、raw_inputは、ユーザの元の入力を取得し、文字列に格納します.特に必要でない限りraw_を一般的に使用しますinputでユーザー入力を取得します.
4、文字列
通常、いくつかの単二重引用符がある文を入力したり、Pythonで特殊な意味を持つ文字として認識されたりするには、これらの特殊な文字をエスケープ文字で区別する必要があります.このような問題をよりよく解決する方法は、元の文字列を使用して入力することです.たとえば、次のようになります.
print r'string'は、エスケープ文字を大量に使用する手間を回避しますが、元の文字列入力を使用して文の末尾に「」を使用することはできません.使用する必要がある場合は、エスケープ文字「」を追加する必要があります.
長い文字列.長い文字列を入力し、複数の行にまたがる必要がある場合は、通常の引用符の代わりに3つの単一引用符を使用します.
unicode文字とは、入力した文字列の前にuを付けることです.print u'hello worldのように!
数値を文字列に変換する2つのメカニズム:関数strとrepr.両者の違いはstrが数値をユーザーが理解できる形式の文字列に変換し、reprはpythonの合法的な形式の式である文字列を作成することである.
5、モジュール
python以外のデフォルト関数を使用する場合は、参照モジュールの形式で関数を呼び出すことができます.
次の2つの方法を使用できます.
(1)fromモジュールimport関数
次に、関数を直接使用できます.
(2)import 1モジュール
       module.関数#カンスウ#
名前の競合が発生する場合があるため、2番目の形式で関数を呼び出すことをお勧めします.
今日練習したコード:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 2 ** 3
8
>>> -3 **2
-9
>>> (-3) ** 2
9
>>> 1111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111L
>>> 11111111111111111111111111111111111111l
11111111111111111111111111111111111111L
>>> 2324324523423432432432432L * 2132132143432423423432L
4955767028159349056163576467822347272465546624L
>>> 0X86
134
>>> 01010101010101010101
18300341342965825L
>>> x = 5
>>> x % 4
1
>>> x *9
45
>>> x+1
6
>>> y= x+1
>>> y
6
>>> print x+y
11
>>> z =x +y
>>> z
11
>>> input("the meaning of life: ")
the meaning of life: 34
34
>>> x = input("x: ")
x: 9
>>> y =input("y: ")
y: -9
>>> print x+y
0
>>> if 1 == 2:print 'One equals two '
>>> if 1 == 2: print 'One equals two '
>>>
>>> if 1 ==1 : print 'One equals one '
One equals one
>>> if time % 60 == 0: print 'On the hour '
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    if time % 60 == 0: print 'On the hour '
NameError: name 'time' is not defined
>>> pow(2.9)
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    pow(2.9)
TypeError: pow expected at least 2 arguments, got 1
>>> pow(2 9)
SyntaxError: invalid syntax
>>> pow(2,9)
512
>>> pow(2, 2+1)
8
>>> x= input("       :“)
                      
SyntaxError: EOL while scanning string literal
>>> x= input("       :")
                      
SyntaxError: invalid syntax
>>> x = input("       : ")
                       
SyntaxError: invalid syntax
>>> x= input(" 123:")
                      
SyntaxError: invalid syntax
>>> x = input("       : ")
       : pow (2,10)
>>> x
1024
>>> abs(-1)
1
>>> 1/2
0
>>> round(1.0/2.0)
1.0
>>> floor (1,2)
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    floor (1,2)
NameError: name 'floor' is not defined
>>> import math
>>> math.floor(30.1)
30.0
>>> floor(30.9)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    floor(30.9)
NameError: name 'floor' is not defined
>>> math.floor(23.9)
23.0
>>> int(math.floor(20.5))
20
>>> from math import floor
>>> floor(34.1)
34.0
>>> big=math.floor
>>> big(23.1)
23.0
>>> int(34.2
    )
34
>>> from math import sprt
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    from math import sprt
ImportError: cannot import name sprt
>>> from math import sprt
Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    from math import sprt
ImportError: cannot import name sprt
>>> import math
>>> math.sprt(2)
Traceback (most recent call last):
  File "<pyshell#59>", line 1, in <module>
    math.sprt(2)
AttributeError: 'module' object has no attribute 'sprt'
>>> from math import sqrt
>>> sqrt(-2)
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    sqrt(-2)
ValueError: math domain error
>>> from math import sqrt
>>> sqrt(4)
2.0
>>> imprt math
SyntaxError: invalid syntax
>>> import math
>>> cmath.sqrt(-2)
Traceback (most recent call last):
  File "<pyshell#66>", line 1, in <module>
    cmath.sqrt(-2)
NameError: name 'cmath' is not defined
>>> import cmath
>>> cmath.sqrt(-2)
1.4142135623730951j
>>> from math import sqrt
>>> from cmath import sqrt
>>> sqrt(3)
(1.7320508075688772+0j)
>>> from math import sqrt
>>> sqrt(3)
1.7320508075688772
>>> ================================ RESTART ================================
>>>
Hello, world
>>> ================================ RESTART ================================
>>>
What is your name? bing
Hello, bing !
>>> x= "hello"
>>> y= "world"
>>> xy
Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    xy
NameError: name 'xy' is not defined
>>> x y
SyntaxError: invalid syntax
>>> x
'hello'
>>> she's mothen
SyntaxError: EOL while scanning string literal
>>>
>>> she\'s mother
SyntaxError: unexpected character after line continuation character
>>> "hello, world "
'hello, world '
>>> she
Traceback (most recent call last):
  File "<pyshell#83>", line 1, in <module>
    she
NameError: name 'she' is not defined
>>> 'she 's mother '
SyntaxError: invalid syntax
>>> ' she\ 's mother '
SyntaxError: invalid syntax
>>> ' she \'s mother '
" she 's mother "
>>> print ' hello world '
 hello world
>>> 10000000l
10000000L
>>> print ' 10000L '
 10000L
>>> print " 10000L "
 10000L
>>> 10000L
10000L
>>> print 10000l
10000
>>> print repr("hello, world !")
'hello, world !'
>>> print repr(10000L)
10000L
>>> print str("hello, world !"
      )
hello, world !
>>> temp =42
>>> print "the temperature is" + temp
Traceback (most recent call last):
  File "<pyshell#99>", line 1, in <module>
    print "the temperature is" + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "the temperature is" + `temp`
the temperature is42
>>> print "the temperature is " + `temp`
the temperature is 42
>>> name = input("what is your name ? ")
what is your name ? bing
Traceback (most recent call last):
  File "<pyshell#102>", line 1, in <module>
    name = input("what is your name ? ")
  File "<string>", line 1, in <module>
NameError: name 'bing' is not defined
>>> name = input("what is your name ? ")
what is your name ? 1223
>>> name = input("what is your name ? ");
what is your name ? 122
>>> print "hello " + name "!"
SyntaxError: invalid syntax
>>> print "hello " + name " ! "
SyntaxError: invalid syntax
>>> print "hello " + name + "!"
Traceback (most recent call last):
  File "<pyshell#107>", line 1, in <module>
    print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> name = input("what is your name ? ");
what is your name ? 123
>>> print "hello " + name + "!"
Traceback (most recent call last):
  File "<pyshell#109>", line 1, in <module>
    print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> ================================ RESTART ================================
>>>
what is your name ? 123print "hello " + name + "!"
Traceback (most recent call last):
  File "D:/python/test", line 1, in <module>
    name = input("what is your name ? ")
  File "<string>", line 1
    123print "hello " + name + "!"
           ^
SyntaxError: invalid syntax
>>> ================================ RESTART ================================
>>>
what is your name ? 123
Traceback (most recent call last):
  File "D:/python/test", line 2, in <module>
    print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> ================================ RESTART ================================
>>>
what is your name ? "bing"
hello bing!
>>> print ''' This is a vert
idd
fdfd
" hello world "
there '''
 This is a vert
idd
fdfd
" hello world "
there
>>> print " hello, \
world ! "
 hello, world !
>>> print 'C:\\ on"
SyntaxError: EOL while scanning string literal
>>> print "C:\\ on"
C:\ on
>>> print ' hello, 
world ! ' hello, world ! >>> print 'c:
owhere' c: owhere >>> print r'c:
ere
df' c:
ere
df >>> print r"this is not corrert\" SyntaxError: EOL while scanning string literal >>> print r"this is not corrert" "\\" this is not corrert\ >>> print r"this is not corrert" "\" SyntaxError: EOL while scanning string literal >>> u'hello world !' u'hello world !' >>> print u' ' Äãoà >>> print r' ' >>>

本文は“Bing_Angela”のブログから出て、転載して作者と連絡してください!