在pycharm下設定自己的模闆:
在File---settings---File and Code Templates---Python script 腳本裡添加:
#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:${USER}
@file: ${NAME}.py
@time: ${YEAR}/${MONTH}/${DAY}
"""
一、第一個python程式:
字元串類型的必須要加引号
傳回結果為(5,3)
解析:a = 3,記憶體位址指向3,b = a,則b = 3,此時a 和 b 都指向記憶體位址3,當 a = 5的時候,a 的記憶體位址指向了5,則a = 3 這個記憶體位址被回收了,但是b的記憶體位址未被回收,b仍然等于3,是以最後傳回的結果是(5,3)
變量起名的原則:
1、顯示,通俗易懂
2、駝峰寫法(首字母大寫) 例如:NumsOfJackGf
3、下橫線寫法(不能為中橫線) 例如:nums_of_jack_gf
4、不能數字開頭,但是可以在中間和結尾
5、命名中不能有特殊字元
6、變量的命名不能有空格
7、關鍵字不能聲明為變量
記憶體位址的驗證:
C:\Users\Administrator>python
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> a = 5
>>> b = a
>>> id(a),id(b)
(1363763552, 1363763552)
a 和 b的記憶體位址完全一樣
>>> a = 10
>>> (1363763712, 1363763552)
當a的值改變之後,a的記憶體位址也發生了變化(是python中的記憶體位址,不是實體機器的記憶體位址)
三、使用者互動
[root@python3 ~]# python
Python 3.6.3 (default, Nov 12 2017, 04:07:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
>>> name = input("please input your name:")
please input your name:chenjisong
>>> print(name)
chenjisong
僞代碼:
如果 你是富二代
我們倆就拍拖
或者 你很努力上進
我們可以接觸試試
否則
免談
遊戲:猜幸運數字:
五、循環控制:
break結束循環:(猜對即跳出循環,沒猜對就一直猜)
六、循環次數限制:
兩重判斷:
第一重:三次猜不對直接退出(guess_count>3),列印“try too many times”
第二重:猜對了直接列印bingo,退出
for循環猜數字遊戲:
七、常用資料類型
資料類型:
數字:
int(整型)
float(浮點型)
long(長整型)
布爾:(True(1) 和 False(0)) 真和假
字元串 str
清單 list
元祖 tuple
字典 dict
type可以檢視資料類型:
>>> type(2**10)
<class 'int'>
>>> type(2.99)
<class 'float'>
八、字元串格式化
第一種寫法會開辟很多記憶體空間,對記憶體資源是一種浪費,是以不建議
第二種寫法隻開辟了一塊記憶體空間,可以有效節省記憶體資源,效率更優
%s要與後面的值一一對應,否則會報錯
第三種寫法:
'''
'''的妙用
九、清單常用操作:
strip:去掉,拿掉空格:以下例子去掉了前面的空格,但是中間的沒法去掉
輸入
name: chen jisong
age: 22
job: IT
輸出:
Information of chen jisong:
Name:chen jisong
Age :22
Job :IT
也可以去掉字元:如下
name: chen jisong
job: IT
Information of jisong:
Name:jisong
Age :30
清單索引(下标)取值: []
>>> name_list = ["65brother","87brother","99brother"]
>>> name_list
['65brother', '87brother', '99brother']
>>> name_list[0]
'65brother'
>>> name_list[1]
'87brother'
>>> name_list[2]
'99brother'
>>> dir(name_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
index 索引:
count 計數:
append 追加:
insert 插入:
pop 删除最後一個索引值
remove 删除固定的值
reverse 反轉
sort 排序
extend 清單的擴充
>>> name_list.append("Eric")
>>> name_list
['65brother', '87brother', '99brother', 'Eric']
>>> name_list.append("87brother")
['65brother', '87brother', '99brother', 'Eric', '87brother']
>>> name_list.index("87brother")
1
>>> name_list.count("87brother")
2
>>> name_list.insert(2,"66brother") 在索引2之後加66brother
['65brother', '87brother', '66brother', '99brother', 'Eric', '87brother']
>>> name_list.remove("66brother")
>>> name_list.pop()
>>> name_list.reverse()
['Eric', '99brother', '87brother', '65brother']
>>> name_list.sort()
['65brother', '87brother', '99brother', 'Eric', '87brother', '87brother']
要一次删除3個87brother,應當怎麼做???
>>> for i in range(name_list.count('87brother')):
... name_list.remove("87brother")
...
['65brother', '99brother', 'Eric']
十、清單的後續操作
>>> a = [1, 2, 4, 3, 'a', 'b']
>>> a
[1, 2, 4, 3, 'a', 'b']
>>> a.insert(1,8) ---在索引一處插入數字
[1, 8, 2, 4, 3, 'a', 'b']
正向切片:
>>> a[3:5]
[4, 3]
>>> a[0:7]
反向切片:
>>> a[-4:]
[4, 3, 'a', 'b']
>>> a[-4:-1]
[4, 3, 'a']
>>> a.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int' 字元串和整型不能進行排序
[1, 2, 3, 4, 8, 'a', 'b']
>>> a.pop()
'b'
'a'
[1, 2, 3, 4, 8]
拿掉字元串後即可進行排序
清單可以相加:
>>> a = [1,2,3,4,5,6,7,8,9]
>>> b = ["a","b","c","d","e","f","g","h","i"]
>>> a + b
[1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> a.extend(b)
十一、二進制位運算
元祖:
>>> t = (1,2,3,4)
>>> dir(t)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
元祖改清單,用list方法:
>>> type(t)
<class 'tuple'>
>>> list(t)
[1, 2, 3, 4]
>>> a = list(t)
>>> type(a)
<class 'list'>
二進制運算:
>>> A = 10
>>> B = 50
>>> A & B 兩者都真才為真
>>> A | B 兩者有一真就為真
58
>>> A ^ B 一真一假則為真
56
>>> A >> 1 整體往右移一位
5
>>> A << 1 整體往左移一位
20
>>> B >> 1
25
>>> B << 1
100
邏輯運算符:與(and) 或(or) 非(not)
>>> sex = "man"
>>> age = 26
>>> if sex == "man" and age > 25:
... print("time to get married")
time to get married
>>> if sex == "man" or age < 23:
... print("do not worried")
do not worried
>>> name_list=["oldboy","alex","eric"]
>>> if "jack" not in name_list:
... print("sorry")
sorry
身份運算符(is is not)
>>> type(name_list) is tuple
False
>>> type(name_list) is list
True
>>> type(name_list) is not list
>>> type(name_list) is not tuple
十二、簡單的嵌套循環
continue 跳出本次循環,繼續下一次循環:
執行結果如下:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/S12/2017-11-18/cotinue.py
6
7
8
9
執行結果:
十三、檔案的基本操作
讀取檔案的内容:
一次性加載所有内容到記憶體
obj.read()
一次性加載所有内容到記憶體,并根據行分割成字元串
obj.readlines()
每次僅讀取一行資料:
for line in obj:
print line
寫入檔案内容:
obj.write(“内容”)
關閉檔案句柄:
obj.close()
打開檔案:
file_obj = file("檔案路徑","模式")
file_obj = open("檔案路徑","模式")
打開檔案的模式有:
r 以隻讀方式打開檔案。
w 打開一個檔案隻用于寫入。
a 打開一個檔案用于追加。
w+ 打開一個檔案用于讀寫。
寫檔案的操作:
test.log下面的文字:
讀檔案的操作,循環逐行讀取:
this is the first line
this is the second line
this is the third line
this is the fourth line
判斷:
追加檔案操作:
test.log輸出結果:
本文轉自陳繼松 51CTO部落格,原文連結:http://blog.51cto.com/chenjisong/1983201,如需轉載請自行聯系原作者