[Python]文法の基本--- 2、組み込み関数

1.2組み込み関数

印刷機能

出力機能、コンソールウィンドウに表示するコンテンツをフォーマットします。

>>> a=1
>>> b=2
>>> c=3
>>> print(abc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> print(a,b,c)
1 2 3

印刷機能のプロトタイプ:print(self, *args ,sep=' ',end='\n',file=None)

  • 自己:気にしない

  • * args:引数、複数のパラメーター

    >>> print(1)
    1
    >>> print(1,2)
    1 2
    >>> print(1,2,3)
    1 2 3

     

  • sep:複数のデータ間の間隔はデフォルトでスペースです ' '

    >>> print(1,2,3,sep = '!')
    1!2!3

     

  • end:最後のデフォルト\n

    >>> print(1,2,3,sep='#',end='哈哈')
    1#2#3哈哈>>> print(1,2,3,sep='#',end='哈哈\n')
    1#2#3哈哈
    >>>

     

フォーマットされた出力

>>> name = "旺财"
>>> age = 18
>>> print(name,age)
旺财 18
>>> print("你叫旺财,你是18岁")
你叫旺财,你是18岁
>>> print("abc"+"ABC")
abcABC
>>> print("你叫"+name+",你是"+age+"岁")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print("你叫%s,你是%d岁"%(name,age))
你叫旺财,你是18岁
  • %s:文字列データを表します

  • %d:整数データを表します

  • %f:浮動小数点データを表します

入力機能

ユーザー入力データを取得し、输入的数据是一个字符串

>>> a = input("请输入你的年龄:")
请输入你的年龄:12
>>> a + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

複数のデータ入力を取得する方法は?

#错误输出
>>> a,b,c = input("请输入三个数字:")
请输入三个数字:12,13,14
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
>>> input("请输入三个数字:")
请输入三个数字:1,2,3
'1,2,3'
​
#正确输出
>>> a,b,c = eval(input("请输入三个数字:"))
请输入三个数字:1,2,3
>>> a+b+c
6

評価関数

文字列内の数値、整数、小数、およびその他の整数を解析します。

>>> eval("3")
3
>>> eval("3.14")
3.14
>>> eval("WYZ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'WYZ' is not defined
>>> eval("0xABC")
2748
>>> eval("0x1001")
4097
>>> eval("0b1001")
9

int関数

整数文字列または浮動小数点数を整数に変換します。

>>> int("123")
123
>>> int("3.14")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
>>> int(3.14)
3

ベースを指定することもできます。

>>> int("1001")
1001
>>> int("1001",2)
9
>>> int("1001",100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: int() base must be >= 2 and <= 36, or 0
>>> int("1001",3)
28
>>> int("1919",6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 6: '1919'

フロート機能

10進文字列または整数を浮動小数点数に変換します。

>>> float("3.14")
3.14
>>> float(3)
3.0
>>> float("3.1a4")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '3.1a4'

str関数

オブジェクトを文字列タイプに変換します。

>>> a = 10
>>> str(a)
'10'
>>> name = "旺财"
>>> age  = 10
>>> print(name+age)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print(name+str(age))
旺财10

腹筋機能

絶対値を見つけます。

>>> abs(-10)
10
>>> abs(-10.2)
10.2
>>>

和関数

シーケンスの合計を見つけます。

>>> sum([1,2,3])
6
>>> sum("abc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> sum(["abc","ABC","lala"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

おすすめ

転載: blog.csdn.net/trichloromethane/article/details/108267260