日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python从入门到实践笔记_Python编程 从入门到实践 #笔记#

發布時間:2023/12/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python从入门到实践笔记_Python编程 从入门到实践 #笔记# 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

變量

命名規則

只能包含字母、數字、下劃線

不能包含空格,不能以數字開頭

不能為關鍵字或函數名

字符串

用單引號、雙引號、三引號包裹 name = "ECLIPSE"

name.title()

name.upper()

name.lower()

name.rstrip()

name.lstrip()

name.strip()

數字

x = 1

message = "bla" + str(x) + "bla"

列表

bicycles = ['1th', '2th', '3th']

bicycles[-1]

bicycles.append('4th')

bicycles.insert(0, '5th')

del bicycles[0]

poped_bicycle = bicycles.pop()

poped_bicycle = bicycles.pop(0)

a = '2th'

bicycles.remove(a)

bicycles.sort()

bicycles.sort(reverse=True)

sorted(bicycles)

sorted(bicycles, reverse=True)

bicycles.reverse()

len(bicylces)

操作列表

for bicycle in bicycels:

print(bicycle)

不會打印出數字5

for v in range(1,5):

print(v)

even_numbers = list(range(2, 21, 2))

squares = []

for v in range(1,10):

squares.append(v**2)

print(squares)

max()

min()

sum()

squares = [v**2 for v in range(1,10)]

print(squares)

players = ['1th', '2th', '3th', '4th', '5th']

print(players[0:3])

print(players[:3])

print(players[1:])

print(players[-3:])

上面的那一種并沒有創建兩個數組

new_players = players

new_playerss = players[:]

元組

不可被修改的列表

if

if... elif... else

and

or

if '1th' in bicycles:

print('yes')

if '1th' not in bicylces:

print('no')

if bicycles:

if not bicycles:

字典

aliens = {}

aliens['1th'] = 1

del aliens['1th']

for k, v in aliens():

for k in aliens.keys():

for k in sorted(aliens.keys()):

for v in aliens.values():

用戶輸入

message = input('tell me something:')

print(message)

age = int(input('tell me your age:'))

print(age+1)

循環while

break

continue

pets = ['1th', '2th', '3th', '4th', '5th']

while pets

url = 'www.baidu.com'

while url:

print('urlis: %s' %url)

url = url[:-1]

函數

形參數

實參數

位置實參

關鍵字實參

默認值

等效的函數調用

讓實參變成可選的

def get_formated_name(firstName, lastName, middleName=''):

if middleName:

fullName = firstName + ' ' + middleName + ' ' + lastName

else:

fullName = firstName + ' ' + lastName

return fullName.title()

function_name(list_name)

function_name(list_name[:])

允許收集任意數量的實參,并把其作為一個元組傳入函數

def make_pizza(*toppings):

python先匹配位置實參和關鍵字實參,再將余下的實參收集到一個形參中

def make_pizza(size, weight=15, *toppings):

使用任意數量的關鍵字實參

def build_profile(first, last, **user_info):

import module_name

from module_name import function_name

from module_name import function_name as f_nick_name

import module_name as m_nick_name

from module_name import *

類名稱首字母大寫

class Dog();

def init():

屬性

方法

給屬性指定一個默認的值

def init(self):

fixed_num = 0

修改屬性的值:

通過句點法直接訪問并修改屬性的值

通過方法修改屬性的值

父類and子類

子類繼承另父類時,自動獲得父類所有屬性and方法

同時也可以定義自己的屬性and方法

class Car():

def __init__(self, make, model, year):

...

class ElectricCar(Car):

def __init__(self, make, model, year):

super().__init__(make, model, year)

子類中和父類相同的方法(重寫父類的方法)

可以將一個實例用作屬性

from car import Car

from car import Car, ElectricCar

import car

from car import * 不推薦使用

from collections import OrderedDict

languages = OderedDict() // 有順序的字典

文件

相對文件路徑

絕對文件路徑

file_path = 'text_file/filename.txt' // Linux and Mac

file_path = 'text_file\filename.txt' // Windows

with open(file_path) as file_object:

contents = file_object.read()

for line in file_object:

print(line.strip())

lines = file_object.readlines()

讀取文本文件時,python將其中的所有文本都讀為字符串

'r' 讀取模式

'w' 寫入模式

'a' 附加模式

'r+' 讀取和寫入文件模式

python只能將字符串寫入文本文件

file_object.write('...') // 不會在文末添加換行符

異常

try:

answer = ...

except ZeroDivisionError:

print('erroe')

else:

print(answer)

title = 'alice is wonderland'

title.split()

def count_words(fileName):

--snip--

fileNames = ['1th', '2th', '3th']

for fileName in fileNames:

count_words(fileName)

pass

count()

import json

json.dump(numbers, file_object)

numbers = json.load(file_object)

重構

unittest測試

總結

以上是生活随笔為你收集整理的python从入门到实践笔记_Python编程 从入门到实践 #笔记#的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。