1. 程式人生 > >python 第一天

python 第一天

python

安裝python https://www.python.org/downloads/windows/ 我這裏安裝在windows上,若其他操作系統可另行尋找

安裝完成後,打開執行程序,輸入第一個命令

print ("Hello World!") //註意因為我這裏用的版本新的,所以print 後面要加括號,否則報錯

“SyntaxError: Missing parentheses in call to ‘print‘” mean in Python?


下面可以使用IDLE編寫python程序,保存後點擊可以直接執行。

一般在/lib/idlelib/idle

技術分享

這裏可以看到import os

..

.. 多次輸出只需要加上*號即可。

os.system("pause") //作用文件執行後暫停。


import random

secret = random.randint(1,100)

guess = 0

tries = 0

print ("AHOY! I‘m the Dread Priate Roberts, and I have a secret!")

print ("It is a number from 1 to 99. I‘ll give you 6 tries.")

print (secret)

while guess !=secret and tries < 6: //註意這個冒號

guess = int(input("what is your guess?")) //註意你input 進去的是默認為字符,無法與數字比較 ,這裏與早期的python 版本有所區別。 這裏除了int()外還有其他的例如float(),str().等。

if guess < secret:

print ("Too low, ye scurvy dog!")

elif guess > secret:

print ("Too high,landlubber!")

tries = tries + 1 //註意對齊,因為python中沒有括號,所以判斷語句段看這些對齊。

if guess == secret:

print ("Avast! you got it! Found my secret,you did!")

else:

print ("NO more guess !")

print ("The secret number was",secret)


變量名只能以字母和下劃線開頭,且中間不能有空格 與C語言變量名基本一致。


若使用跨多行的字符串需要使用 """ 。


較新版本的python 在使用除法時,已經不用再區分小數了 print (3/2) 會輸出1.5


print (2**5) 雙星代表後面的數為冪

輸出 32


自增自減符號 += -=


>>> a=24

>>> b=float(a)

>>> b

24.0 強轉為浮點型。


type (a) 可以顯示a的類型。

int (56.78) 會向下取整成56


somebody = input("Enter your name:") //註意這樣可以使輸入你的名字。在提示的同一行

print ("Hi",somebody,"how are you today?")



import urllib.request

file = urllib.request.urlopen(‘http://192.168.18.110/‘) //註意urllib.urlopen 改成了urllib.request.urlopen

message =file.read()

print(message)


本文出自 “姑蘇城” 博客,請務必保留此出處http://ji123.blog.51cto.com/11333309/1964036

python 第一天