# -*- coding: utf-8 -*-
"""
Created on Sun Dec 22 10:15:39 2019
@author: zhh
"""
import math
'''1、基本語法'''
#同行顯示多行語句:用";"分號隔開
#print輸出:
#print默認換行,若要 不換行在變量末尾加上逗號,end=''
x='a';y='b'
print(x,end='') #不換行
print(y) #默認換行
print(x,y) #不換行 “a b”
'''2、數據類型'''
'''數值'''
#round(x,d) 對浮點數x四舍五入,d是小數位保留幾位
round(4.33345,4) #4.3335
round(4.33345,2)
#ceil向上取整 floor向下取整
math.ceil(4.1) #向上取整2
'''字符串'''
#輸入
a=input('')
b=int(input(''))
print(type(b)) #int
#格式化輸出
#% 如'''字典形式?。。?''
print('hello! I %(v1)s a %(v2)s'%{'v1':'am','v2':'student!'})
#hello! I am a student!
#format:括號{}代替%s,利用format()函數指定字符串
print('hello! I {} a {}!'.format('am','student'))
#指定位置
print('{0} {1} {0}'.format('am','student')) #am student am
#格式化輸出
print("{:.2f}".format(3.1415)) #3.14
#字符串基本運算
#+連接
print('ab'+'cd') #abcd
#*重復輸出
print("ab"*2) #abab
#切片
print("abcd"[1:-2]) #b
print("abcdefg"[0:7:2]) #aceg 索引0到6 步長為2
#替換
str1='abcdweghi'
str1.replace('we','ef') #'abcdefghi' 老的換成新的
#分割
str1='ab,cd,weg,hi'
str1.split(',') #['ab', 'cd', 'weg', 'hi']
'''列表'''
#通過索引訪問值
#del 刪除指定索引元素
list1=['a','b','c','d']
del list1[1] #['a', 'c', 'd']
#remove 刪除指定值
list1=['a','b','c','d']
list1.remove('a') #['b', 'c', 'd']
#pop 刪除指定索引 無參數時刪除最后一個元素
list1=['a','b','c','d']
list1.pop(1) #['a', 'c', 'd']
#清空列表所有元素
list1.clear() #[]
#append 增加元素到列表末尾
list1=['a','b','c','d']
list1.append('e') #['a', 'b', 'c', 'd', 'e']
#extend 將一個列表元素全部增加到另一個列表中
list1=['a','b','c','d']
list2=['e','f','g']
list1.extend(list2) #['a', 'b', 'c', 'd', 'e', 'f', 'g']
#insert 在指定位置增加元素
list1=['a','b','c','d']
list1.insert(1,'x') #['a', 'x', 'b', 'c', 'd']
#在列表中查找元素是否存在in /not in
x='a'
list1=['a','b','c','d']
if x in list1: #如果存在則結果為True
print("在")
else:
print("不在")
#排序
#倒置
list1=['a','b','c','d']
list1.reverse() #['d', 'c', 'b', 'a']
#排序(默認從小到大)
list1=['b','a','d','c']
list1.sort() #['a', 'b', 'c', 'd']
#倒序 reverse=True
list1=['b','a','d','c']
list1.sort(reverse=True) #['d', 'c', 'b', 'a']
#嵌套
list1=[['a','b'],['w','f','e']] #二維列表
print(list1[1][0]) #w
# + 連接 *重復輸出
list1=['a','b','c','d']
list2=['e','f']
print(list1+list2) #['a', 'b', 'c', 'd', 'e', 'f']
print(list1*2) #['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
'''元組'''
#不可修改
tup1=('a','b','c')
tup2=('d','e')
#訪問
print(tup1[1]) #b
# +連接
print(tup1+tup2)
print(tup1*2)
#del 刪除整個元組
del tup1
#print(tup1) #name 'tup1' is not defined
#
tup2=('d','e')
print(len(tup2))
print(max(tup2))
print(tup2[0:2])
#與列表相互轉換
tup1=('a','b','c')
list1=list(tup1)
list2=['d','d','f']
tup2=tuple(list2)
'''字典'''
dict1={'name':'zhh','age':'18'}
del dict1['name']
dict1.clear() #清空字典所有元素
dict1={'name':'zhh','age':'18'}
dict1.keys()
dict1.values()
dict1.items() #dict_items([('name', 'zhh'), ('age', '18')])
'''集合'''
#無序不重復元素的序列
#創建
b_set=set() #空集合 不能用{}創建空集合
b_set=set(['22','d','d','v']) #{'22', 'd', 'v'}
a_set={'ff','ff','d'} #{'d', 'ff'}
#del只能刪除集合
a_set={'a','b','c','d'}
del a_set
#pop 刪除一個隨機元素
a_set={'a','b','c','d','e'}
a_set.pop()
#remove刪除指定元素
a_set={'a','b','c','d','e'}
a_set.remove('a')
a_set.clear()
print(a_set)
#add增加元素
a_set={'a','b','c','d'}
a_set.add('e') #{'a', 'b', 'c', 'd', 'e'}
#一般格式為:s.update(s1,s2,...sn),其中功能是用集合s1,s2,...,sn中的成員
#修改集合s,s等于s與s1、s2...的并集。
a_set.update({3,4},{'rr','ff'}) #{3, 4, 'a', 'b', 'c', 'd', 'e', 'ff', 'rr'}
#可以使用"-"、"|"、"&"運算符進行集合的差集、并集、交集運算。
a=set('abcd')
b=set('cdef')
print(a)
print(a-b) #a中集合除去b里有的 {'a', 'b'}
print(a|b) #并集 {'a', 'f', 'd', 'b', 'e', 'c'}
print(a&b) #交集 {'c', 'd'}
'''邏輯預算符號'''
#and 與 or 或 not 非
'''函數'''
#在Python中,數值、字符串與元組是不可更改的類型,而列表、字典等則是可以修改的類型,函數參數采用不同方式傳遞。
#(1)可更改類型
#類似C++的引用傳遞(如列表、字典),將參數真正的傳入函數;函數內部修改參數值后,函數外部被傳入的參數也會受影響。
#例:
def changeme( mylist ):
mylist.append([1,2,3,4])
print ("函數內取值: ", mylist)
return
mylist = [10,20,30]
changeme( mylist )
print ("函數外取值: ", mylist)
#結果:函數內取值: [10, 20, 30, [1, 2, 3, 4]]
#函數外取值: [10, 20, 30, [1, 2, 3, 4]]
#(2)不可變類型(如數值、字符串、元組)
#類似C++的值傳遞,傳遞的只是參數的值,不影響參數本身;函數內部修改參數值后,函數外部被傳入的參數不受影響。
#實例:
def ChangeInt( a ):
a = 10
b = 2
ChangeInt(b)
print( b )
#結果:2
'''匿名函數!!!'''
#匿名函數不再使用def定義的函數,需要使用lambda關鍵字。
#聲明格式:lambda [arg1 [,arg2,.....argn]]:表達式
sum = lambda a1,a2 : a1+a2 #只能訪問自己參數列表的參數!??!
print(sum(2,3)) #5
'''面向對象'''
class Cat:
print('cat')
#實例化對象
x=Cat() #cat
class Test:
i='aa'
#方法
def test1(self):
return 'dede'
#實例化對象
x=Test()
#調用類的方法
print(x.test1()) #'dede'
#調用類的屬性
print(x.i)
#增加類的數學
x.co='aaa'
print(x.co) #aaa
#self
class Car:
#構造方法
def __init__(self,color):
self.strcolor=color
#類的方法
def toot(self):
print("這是%s色的車"%self.strcolor)
car = Car('red') #自動調用構造方法,賦值
car.toot() #這是red色的車
#文件
f = open("foo.txt", "w")
f.write( "Python 是一個非常好的語言。/n是的,的確非常好!!/n" )
f.close()
#此時打開文件 foo.txt,顯示如下:
#Python 是一個非常好的語言。
#是的,的確非常好!!
f = open("foo.txt", "r")
str = f.read()
print(str)
# 關閉打開的文件
f.close()
#Python 是一個非常好的語言。
#是的,的確非常好!!
#f.readline()會從文件中讀取單獨的一行。f.readline()如果返回一個空字符串, 說明已經已經讀取到最后一行。
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。