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

首頁 > 編程 > Python > 正文

[Python]學習筆記(1)

2019-11-06 09:50:55
字體:
來源:轉載
供稿:網友

格式

Python程序在最開頭需要注明python的路徑

#!/usr/bin/python3

導入包

Python需要引入使用到的模塊,格式如下:

import sys import mathimport randomimport module1[, module2[,... moduleN]from modname import name1[, name2[, ... nameN]]

python的保留字段

python中的保留字段如下:

and exec not
assert finally or
break for pass
class from PRint
continue global
def if return
del import try
elif in while
else is with
except lambda yield

簡單的打印

print("我是打印的語句")

python語句

is_true=True //這里注意python的boolean值是True和Flase if(is_true): print("is_true value is true!")//這里注意python代碼分塊是通過代碼行的縮進區分的,并且縮進必須一樣;并且注意沒有';' else: print("is_true value is false!") is_true=False if(is_true): print("is_true value is true!") else: print("is_true value is false!")

python中的文本操作

file_name="test.txt"http://文件名 try: file=open(file_name,"w")//打開文件 except IOError: print("error open file:",file_name) sys.exit() file_finished="s" print("Enter: ",file_finished,"when file finished!") file_text="" while(file_text!=file_finished): file_text=input("Enter text:")//讀取鍵盤輸入 if(file_text==file_finished): file.close break file.write(file_text)//寫入內容 file.write("/n") file.close() file_name=input("Enter file name:") if(len(file_name)==0): print("Input nothing,exit") sys.exit() try: file=open(file_name,"r") except IOError: print("error open file,name is:",file_name) sys.exit() file_text=file.read()//讀取文件內容 file.close() print(file_text)

python中的字符串

//字符串的區分通過 '' "" """來區分,注意的是:一定要成對;長字符串的分行可以通過'/'來區分 text_multi_line="112"/ +"33"/ +"44" print(text_multi_line) print('/a'+r'/n') //r''表示不轉義

多行代碼寫在一行

代碼也可以一行寫多個語句,但是要通過;來區分

text_input=input("/n/nname:");print("input name is:",text_input)

變量

對于變量的聲明可以批量聲明.

a=b=c=1 e,f,g=1,2,"123dd" #print(a,b,c,e,f,g)

關于字符串的截斷輸出

text_output="abcdefghijklmn" print(text_output*2) print(text_output[0]) print(text_output[2:6]) print(text_output[3:]) print(text_output+"___new characters")

字典,list,tuple類型

list_test=[1,2,3,4,6,'g','h','j'] //list tuple_test=(1,2,3,'f','g','h','t','h') //tuple,注意tuple內容不能修改元素的值 dict_test={} dict_test["a"]=123 dict_test["b"]=456 print(list_test,tuple_test) print(list_test[2]) print(dict_test["a"],dict_test["b"],dict_test) print(repr(list_test)) print(eval("13"))

強制轉換類型

Function Description
int(x [,base]) 強制x轉換為整型
long(x [,base] ) 強制x轉換為長整型
float(x) 強制x轉換為浮點型
complex(real [,imag]) 強制x轉換為復數
str(x) 強制x轉換為字符型
repr(x) 強制x轉換為表達式
eval(str) 強制str轉換為指定對象
tuple(s) 強制x轉換為tuple
list(s) 強制x轉換為list
set(s) 強制x轉換為set類型
dict(d) 強制d轉換為字典型
frozenset(s) 強制s轉換frozen set.
chr(x) 強制將整型轉換為字符型
unichr(x) 強制將整型轉換為unicode類型
ord(x) 強制將單字符轉換為整型
hex(x) 將整型轉換為16進制字符串
oct(x) 將整型轉換為8進制字符串

操作符

+ - * / % // == != < > <= => = += -= *= /= %= **= //= & | ^ ~(補碼) >> << in not in is is not //注意沒有自增和自減 print(2**2)#指數 print(12//5)#floor division

pass語句

for letter in 'Python': pass //do noting print('Current Letter :', letter) length=len('Python') i=0 while(i<length): print("print i:",i) i+=1

Python 支持的數據類型

包括整型,長整型,浮點型,復雜類型(復數)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孟津县| 钟祥市| 凌源市| 阳城县| 高陵县| 永吉县| 肥乡县| 平乡县| 东安县| 沅江市| 临夏市| 中西区| 白山市| 奇台县| 屏东县| 筠连县| 开原市| 贞丰县| 嘉定区| 根河市| 九寨沟县| 宝坻区| 和顺县| 高淳县| 洪泽县| 梅州市| 双流县| 和政县| 益阳市| 抚远县| 阳泉市| 英吉沙县| 宁夏| 安仁县| 齐齐哈尔市| 衡东县| 桑日县| 田林县| 宜良县| 南华县| 桑日县|