国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python3 入門教程 簡單但比較不錯

2020-02-23 04:45:05
字體:
供稿:網(wǎng)友
本文適合有Java編程經(jīng)驗的程序員快速熟悉Python
本文程序在windows xp+python3.1a1 測試通過.
本文提到的idle指python shell,即安裝python后你在菜單看到的IDLE(python gui)
在idle里ctrl+n可以打開一個新窗口,輸入源碼后ctrl+s可以保存,f5運行程序.
凡打開新窗口即指ctrl+n的操作.
1 你好
代碼如下:
#打開新窗口,輸入:
#! /usr/bin/python
# -*- coding: utf8 -*-

s1=input("Input your name:")
print("你好,%s" % s1)
'''

知識點:
* input("某字符串")函數(shù):顯示"某字符串",并等待用戶輸入.
* print()函數(shù):如何打印.
* 如何應(yīng)用中文
* 如何用多行注釋
'''

2 字符串和數(shù)字
但有趣的是,在javascript里我們會理想當(dāng)然的將字符串和數(shù)字連接,因為是動態(tài)語言嘛.但在Python里有點詭異,如下:
代碼如下:
#! /usr/bin/python
a=2
b="test"
c=a+b

運行這行程序會出錯,提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進行轉(zhuǎn)換
代碼如下:
#! /usr/bin/python
#運行這行程序會出錯,提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進行轉(zhuǎn)換
a=2
b="test"
c=str(a)+b
d="1111"
e=a+int(d)
#How to print multiply values
print ("c is %s,e is %i" % (c,e))
'''

知識點:
* 用int和str函數(shù)將字符串和數(shù)字進行轉(zhuǎn)換
* 打印以#開頭,而不是習(xí)慣的//
* 打印多個參數(shù)的方式
'''

3 列表
代碼如下:
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表類似Javascript的數(shù)組,方便易用
#定義元組
word=['a','b','c','d','e','f','g']
#如何通過索引訪問元組里的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
print (b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: ")
print (c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: ")
print (d) # All elements of word.
#元組可以合并
e=word[:2]+word[2:]
print ("e is: ")
print (e) # All elements of word.
f=word[-1]
print ("f is: ")
print (f) # The last elements of word.
g=word[-4:-2]
print ("g is: ")
print (g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: ")
print (h) # The last two elements.
i=word[:-2]
print ("i is: ")
print (i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
print ("Adds new element")
word.append('h')
print (word)
#刪除元素
del word[0]
print (word)
del word[1:3]
print (word)
'''

知識點:
* 列表長度是動態(tài)的,可任意添加刪除元素.
* 用索引可以很方便訪問元素,甚至返回一個子列表
* 更多方法請參考Python的文檔
'''

4 字典
代碼如下:
#! /usr/bin/python
x={'a':'aaa','b':'bbb','c':12}
print (x['a'])
print (x['b'])
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵东县| 佛坪县| 丰顺县| 巴林右旗| 马鞍山市| 兴城市| 绍兴县| 谢通门县| 拉萨市| 大城县| 讷河市| 玛沁县| 民丰县| 余姚市| 台安县| 阳城县| 南城县| 夏邑县| 泰顺县| 开远市| 海兴县| 绥江县| 永年县| 柘城县| 嘉峪关市| 沅江市| 合水县| 汉沽区| 穆棱市| 长葛市| 凤庆县| 喜德县| 延长县| 万山特区| 荥经县| 新竹县| 夏邑县| 来宾市| 白河县| 翁牛特旗| 余干县|