python3 和 python2 的區(qū)別
print "hello" #python2 不用加括號(hào),是一個(gè)語句
print("hello") #python3 需要加括號(hào),是一個(gè)函數(shù)
2也可以加括號(hào)
python2 默認(rèn)編碼 ascii
python3 默認(rèn)編碼 utf-8
python2 的輸入有raw_input 和 input
raw_input 獲取到的輸入永遠(yuǎn)都是str類型
input獲取到的輸入會(huì)自動(dòng)判斷其類型
python3 只有input
python3 從鍵盤輸入的均為string字符串類型,如果需要整形或者浮點(diǎn)型需要強(qiáng)轉(zhuǎn)
python2 使用type打印類型 是 <type 'int'> <type 'list'>
python3 使用type打印類型 是 <class 'int'> <class 'list'>
File and Code Templates
代碼模板
#-*- coding: utf-8 -*-
# @Time : ${DATE} ${TIME}
# @Author : Z
# @Email : S
# @File : ${NAME}.py


這樣在新創(chuàng)建文件的時(shí)候開頭會(huì)自動(dòng)生成信息。包括創(chuàng)建的時(shí)間和文件名。
pycharm 和 anaconda 的整合

指定python解析器的位置
腳本語言的第一行,只對(duì)Linux/Unix用戶適用,用來指定腳本用什么interpreter來執(zhí)行
有這句的,加上執(zhí)行權(quán)限后,可以直接用./執(zhí)行,不然會(huì)出錯(cuò),因?yàn)檎也坏絧ython解釋器
!/usr/bin/python
print "Hello World";
修改pycharm中的解釋器


查看當(dāng)前編碼
import sys
print(sys.getdefaultencoding())
編碼
python2 默認(rèn)編碼是ascii,python3默認(rèn)編碼是utf-8.
python3中,在字符引號(hào)前加'b' ,明確表示這是一個(gè)bytes類型的對(duì)象,實(shí)際上它是一組二進(jìn)制字節(jié)序列組成的數(shù)據(jù)。
encode負(fù)責(zé)字符(Unicode)到字節(jié)(byte)的編碼轉(zhuǎn)換,默認(rèn)使用utf-8編碼轉(zhuǎn)換。t
s = "中國長城"
# 使用python3中提供的encode從Unicode轉(zhuǎn)化成bytes
print(s.encode())
print(b'\xe4\xb8\xad\xe5\x9b\xbd\xe9\x95\xbf\xe5\x9f\x8e'.decode())
# 使用python3中提供的decode從bytes轉(zhuǎn)化為了Unicode字符
print(s.encode("gbk"))
print(b'\xd6\xd0\xb9\xfa\xb3\xa4\xb3\xc7'.decode("gbk"))
utf-8的作用就是把Unicode轉(zhuǎn)換成bytes
Unicode 是字符集
utf-8 是編碼規(guī)則
pip list 命令查看安裝的包
python已經(jīng)為我們提供了高精度運(yùn)算
IndentationError : unexpected indent 縮進(jìn)錯(cuò)誤
注釋
#單行注釋
''' first line
second line
'''
兩個(gè)三引號(hào)多行注釋
區(qū)分大小寫
輸出字符串加數(shù)字需要用str()把數(shù)字轉(zhuǎn)化成字符串
print('hello' + 7) # error
print('hello' + str(7)) # ok
在python當(dāng)中沒有i++這類的操作
可以i+=1
查看所有的內(nèi)置函數(shù)
在Python中運(yùn)行下邊命令:
dir(__builtins__)