- ubuntu下的如何修改python的連接,默認打開python3 而不是python2
刪除軟鏈接 : sudo unlink /usr/bin/python
創建一個新連接 :sudo ln -s /usr/bin/python3.4 /usr/bin/python
-
python執行方式:
1)進入python的環境內進行功能測試
在命令行中輸入 : python 直接進入python環境
退出: ctrl + D ,或則 exit()
2)直接運行python程序 -
編寫第一個python程序 , hello world
vi helloworld.py
#!/usr/bin/env python
coding=utf-8
print(“hello wrold !!”)
-
python的數的概念
整數, 小數,
help : 內建幫助函數help的使用,
help(id) :可以顯示id 的使用幫助id : 用來顯示數(對象)的地址
type : 顯示數的類型
type(3)
<class ‘int’>type(3.0)
<class ‘float’>type(“a”)
<class ‘str’>type(‘a’)
<class ‘str’>
-
python的變量
x = 100
y = 12.1
z = “hello” -
python的輸入 input函數的使用
input(字符串):
從標準輸入讀入一個字符串,換行符被剝離 , 讀回來的內容都是字符串
這個函數的返回值: 是讀回來的字符串str(): 把一個浮點數或整數轉換成字符串
int(): 把一個字符串或浮點數轉換成整數
float(): 把一個字符串或整數轉換成浮點數 -
算術運算符:
- : 加法
- : 減法
- : 乘法
/ : 除法, 整數相除結果是浮點數
// : 求整數
% : 求餘數
** : 次冪
-
數學庫的使用math
可以使用命令行模式來查看數學庫中的函數,
import math
dir(math) : 可以查看math數學庫的所有函數名
help(math.sqrt) -
關係運算符
: 大於
< : 小於
== : 等於
!= : 不等於
<= : 小於等於
= : 大於等於 -
python中的真是true
假是false -
邏輯運算符
and : 與運算, 等價於c語言的&&
or : 或運算, 等價於c語言的||
not : 非運算, 等價於c語言的! -
import 導入模塊, 等價於c 語言的#include <*.h>
import math <==>#include <math.h>
-
控制語句之if else
-
只用if 不適用else
if 表達式 :
語句1
解析: 表達式的值爲真(true),執行語句1 , 否則不執行語句1 -
if 和else 一起使用
if 表達式1 :
語句1
else :
語句2
解析: 表達式1的值爲真(true),執行語句1 , 否則不執行語句2 -
if 表達式1 :
語句1
elif 表達式2 :
語句2
elif 表達式3 :
語句3
…
elif 表達式n :
語句n
else :
語句n+1
-
練習5: 從終端輸入兩個數,比較兩個數的大小關係 > , == ,<
10. 三元操作符
a = Y if X else Z
解釋:如果X 的條件爲真, a = Y, 否則 a = Z
11 .控制語句 for循環
list = “abcdedf”
for i in list :
語句1
i從list開始讀取字符,一直讀到結尾,for循環停止
-
for循環精確控制循環次數
range這個函數來實現
range(start,stop[,step])
start : 可以不寫, 默認爲1
stop: 結束的數值,必須寫
step: 步長,一次變化的大小,必須大於0的數range(stop) : 包含到給定數的所有數
range(10) : 包含 從0 到9的所有數range(start,stop[,step])
range(1,10,2)
13 .控制語句while循環
while 表達式1 :
語句1
解釋:表達式1的值爲真, 執行語句1 , 爲假結束while循環
特殊語句: while true , 類似c語言的while(1)
語句1
語句2
語句n
-
while else
while 表達式1 :
語句1
else :
語句2
解釋: 表達式1爲真, 執行語句1 , 表達式1爲假, 執行else ,語句2 -
for else
for 變量 in 列表 :
語句1
else :
語句2 -
原始字符串
print(“I like \npython”)
普通字符串, 遇見\n 換行
print(r"I like \npython")
原始字符串是什麼就輸出什麼, 所見即所得 -
字符串的索引和切片
l = “goodbye”
l=“goodbye”
l[0]
‘g’l[1]
‘o’l[2]
‘o’l[3]
‘d’l[4]
‘b’l[5]
‘y’l[6]
‘e’
“goodbye”[0]
‘g’“goodbye”[3]
‘d’
以上都是通過索引來找到字符, 返回來也可以通過字符找到索引
可以index可以完成
l
‘goodbye’l.index(“b”)
4l.index(“e”)
6
字符串的切片: 要使用區間進行保留
l
‘goodbye’l[3:4]
‘d’l[2:4]
‘od’l[3:5]
‘db’haha=l[3:5]
haha
‘db’
- 字符串的基本操作
len() : 極端字符串的長度
len(“hello”)
5
+ :連接字符串
* : 字符串被複制幾份
a=“hello”
a*3
‘hellohellohello’
in :判斷這個元素時候在序列內
>>> l
‘Difficulty shows what men are.’
d in l
“d” in l
False“D” in l
True“what” in l
True“good” in l
False
ord : 得到字符的編碼(ascii)
chr : 把對應的編碼轉成字符串
ord(“g”)
103chr(103)
‘g’
-
字符串的組裝
printf(“a=%d,b=%d,c=%d\n”,a,b,c)
sprintf(buf,“a=%d,b=%d,c=%d\n”,a,b,c)python推薦使用string.format 進行字符串的拼接組合
例如:
“i like {0} and {1}”.format(“c”,“python”)
‘i like c and python’str = “i like {0} and {1}”.format(“c”,“python”)
str
‘i like c and python’
到此爲止 只是來記錄了部分還有很多需要去花時間去看。