Python入門(一)隨意感受一下

Python入門(一)隨意感受一下


Python版本:3.6.3

這一節,先隨意感受一下python,不要在乎格式、邏輯什麼的。

首先,打開Python的shell,開始我們的Hello World

>>>print("hello world")
hello world

>>>

好了,已經入門了。python自帶計算機的功能的,看一看:

>>>1+2
3

>>>123*3455
424965

>>>1/2
0.5     #注意:輸出並不是0

>>>1//2
0       #注意:輸出是0,/與//的區別

>>>

這都是比較常見的,那再看看不可思議的:

>>>print("Hello Python"*3)
Hello PythonHello PythonHello Python

“Hello Python”被輸出了三次,這個有點意思啊。
繼續

>>>num=input("請輸入一個數字")
請輸入一個數字>? 25
>>>num
'25'    #注意:這個輸出有引號
>>>print(num)
25      #注意:這個輸出沒有引號,但是num是字符串

>>>type(num)
<class 'str'>       #看到了吧,type()可以查看數據類型

>>>int(num)         #強轉爲int類型
25

>>>
num = input("請輸入一個數字")
num = int(num)

if num % 10 == 0:
    print("輸入的數是10的倍數")
elif num % 33 == 0:
    print("輸入的數是33的倍數")
else:
    print("其他")

測試輸出

請輸入一個數字25
其他

請輸入一個數字100
輸入的數是10的倍數

請輸入一個數字66
輸入的數是33的倍數

Python有很多BIF(Built-in Function),上面的print()、input()、type()、int()等都是BIF,其實我們可以知道所有BIF,如下:所有小寫的都是BIF

>>> dir(__builtins__)
[...(省略了一部分) 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

我們可以使用help查看各個BIF的功能,括號中的參數就是BIF名:

>>>help(int)
Help on class int in module builtins:
class int(object)
 |  int(x=0) -> integer
 |  int(x, base=10) -> integer
 |  
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is a number, return x.__int__().  For floating point
 |  numbers, this truncates towards zero.
 |  
 |  If x is not a number or if base is given, then x must be a string,
 |  bytes, or bytearray instance representing an integer literal in the
 |  given base.  The literal can be preceded by '+' or '-' and be surrounded
 |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
 |  Base 0 means to interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4
 |  
 ...(只截取了一部分)

這篇大概先感受到這裏,下一篇開始正式從最基礎的語法講起。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章