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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

大学python笔记_Introduction to Python课程笔记

發(fā)布時間:2025/3/21 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大学python笔记_Introduction to Python课程笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先恭喜自己終于通過了DATACAMP的第一個課程Introduction to Python,

課程講義也上傳到了百度云里,鏈接7天有效,需要的小伙伴們請?zhí)崆氨4妗?/p>

提取碼:0hr3

該課程主要分為四個章節(jié):

Python Basics

Python Lists

Functions and Packages

NumPy

卡的比較久的幾個代碼主要是在沒看清題目,又或者網(wǎng)絡(luò)不好導(dǎo)致視頻沒仔細看,直接刷題。

重點如下:python區(qū)分大小寫

變量不可以數(shù)字開頭

python數(shù)據(jù)選取從0開始

[including:excluding]左閉右開,即最左邊的區(qū)間選取,右邊的區(qū)間不選取

python 不支持直接對list進行運算,所以需要用到np.array數(shù)組對列表數(shù)據(jù)進行運算。

即標(biāo)準的列表list[1,2,3]+list[1,2,3]=list[1,2,3,1,2,3],而numpy中的列表可以通過運算符進行數(shù)學(xué)運算np_list[1,2,3]+np_list[1,2,3]=np_list[2,4,6]

因此多數(shù)列表操作使用要調(diào)用numpy數(shù)組,比如np.mean(),np.median()

2維列表2dnumpyarray,即list of list 中的數(shù)據(jù)選取規(guī)則如下,逗號分隔,:代表全選

data[:,0]標(biāo)識第一列全選,data[0,:]標(biāo)識第一行全選

部分通過代碼如下:

List操作方法

# string to experiment with: place

?

place = "poolhouse"

?

# Use upper() on place: place_up

?

place_up=place.upper()

?

# Print out place and place_up

?

print(place,place_up)

?

# Print out the number of o's in place

?

print(place.count('o'))

list.append()一次只能添加一個元素

import math

math.pi即為常數(shù)π

也可以只加載包中的一個函數(shù)使得運行更快。

from math import pi

一般會用ticks 縮寫包名,

如import numpy as np

由于python不支持列的操作,所以一般用numpy包來對列表(數(shù)組)進行運算。

python的列表進行+運算會相連

Numpy的列表進行+運算會求和

可以使用np.array(list)來得到一個numpy列表。

# Create list baseball

baseball = [180, 215, 210, 210, 188, 176, 209, 200]

?

# Import the numpy package as np

import numpy as np

?

# Create a numpy array from baseball: np_baseball

np_baseball=np.array(baseball)

?

# Print out type of np_baseball

print(type(np_baseball))

# height is available as a regular list

?

# Import numpy

import numpy as np

?

# Create a numpy array from height_in: np_height_in

np_height_in=np.array(height_in)

?

# Print out np_height_in

print(np_height_in)

?

# Convert np_height_in to m: np_height_m

np_height_m=np_height_in*0.0254

?

# Print np_height_m

print(np_height_m)

# height and weight are available as regular lists

?

# Import numpy

import numpy as np

?

# Create array from height_in with metric units: np_height_m

np_height_m=np.array(height_in)*0.0254

?

# Create array from weight_lb with metric units: np_weight_kg

np_weight_kg=np.array(weight_lb)*0.453592

?

# Calculate the BMI: bmi

bmi=np_weight_kg/(np_height_m**2)

?

# Print out bmi

print(bmi)

選取列表元素

# height and weight are available as a regular lists

?

# Import numpy

import numpy as np

?

# Calculate the BMI: bmi

np_height_m = np.array(height_in) * 0.0254

np_weight_kg = np.array(weight_lb) * 0.453592

bmi = np_weight_kg / np_height_m ** 2

?

# Create the light array

light=bmi<21

?

# Print out light

print(light)

?

# Print out BMIs of all baseball players whose BMI is below 21

print(bmi[light])

# height and weight are available as a regular lists

?

# Import numpy

import numpy as np

?

# Store weight and height lists as numpy arrays

np_weight_lb = np.array(weight_lb)

np_height_in = np.array(height_in)

?

# Print out the weight at index 50

print(weight_lb[50])

?

# Print out sub-array of np_height_in: index 100 up to and including index 110

print(np_height_in[100:111])

2d numpy arrays 可以看做List of list,最多可以有7維的列表

數(shù)據(jù)選取類似R語言中的DATAFRAME,都可以通過逗號分隔來選取,但是需要加 :

# Create baseball, a list of lists

baseball = [[180, 78.4],

[215, 102.7],

[210, 98.5],

[188, 75.2]]

?

# Import numpy

import numpy as np

?

# Create a 2D numpy array from baseball: np_baseball

np_baseball=np.array(baseball)

?

# Print out the type of np_baseball

print(type(np_baseball))

?

# Print out the shape of np_baseball

print(np_baseball.shape)

# baseball is available as a regular list of lists

?

# Import numpy package

import numpy as np

?

# Create np_baseball (2 cols)

np_baseball = np.array(baseball)

?

# Print out the 50th row of np_baseball

print(np_baseball[49,:])

?

# Select the entire second column of np_baseball: np_weight_lb

np_weight_lb=np_baseball[:,1]

?

# Print out height of 124th player

print(np_baseball[0:124,1])You managed to get hold of the changes in height, weight and age of all baseball players. It is available as a 2D numpy array, updated. Add np_baseball and updated and print out the result.

You want to convert the units of height and weight to metric (meters and kilograms respectively). As a first step, create a numpy array with three values: 0.0254, 0.453592 and 1. Name this array conversion.

Multiply np_baseball with conversion and print out the result.

# baseball is available as a regular list of lists

# updated is available as 2D numpy array

# Import numpy package

import numpy as np

# Create np_baseball (3 cols)

np_baseball = np.array(baseball)

# Print out addition of np_baseball and updated

print(np_baseball+updated)

# Create numpy array: conversion

conversion=np.array([0.0254,0.453592,1])

# Print out product of np_baseball and conversion

print(np_baseball*conversion)

# np_baseball is available

?

# Import numpy

import numpy as np

?

# Create np_height_in from np_baseball

np_height_in=np_baseball[:,0]

?

# Print out the mean of np_height_in

print(np.mean(np_height_in))

?

# Print out the median of np_height_in

print(np.median(np_height_in))

# np_baseball is available

?

# Import numpy

import numpy as np

?

# Print mean height (first column)

avg = np.mean(np_baseball[:,0])

print("Average: " + str(avg))

?

# Print median height. Replace 'None'

med = np.median(np_baseball[:,0])

print("Median: " + str(med))

?

# Print out the standard deviation on height. Replace 'None'

stddev = np.std(np_baseball[:,0])

print("Standard Deviation: " + str(stddev))

?

# Print out correlation between first and second column. Replace 'None'

corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])

print("Correlation: " + str(corr))

nparray選取子集

# heights and positions are available as lists

?

# Import numpy

import numpy as np

?

# Convert positions and heights to numpy arrays: np_positions, np_heights

np_heights=np.array(heights)

np_positions=np.array(positions)

?

?

# Heights of the goalkeepers: gk_heights

gk_heights=np_heights[np_positions=="GK"]

?

# Heights of the other players: other_heights

other_heights=np_heights[np_positions!="GK"]

?

?

# Print out the median height of goalkeepers. Replace 'None'

print("Median height of goalkeepers: " + str(np.median(gk_heights)))

?

# Print out the median height of other players. Replace 'None'

print("Median height of other players: " + str(np.median(other_heights)))

總結(jié)

以上是生活随笔為你收集整理的大学python笔记_Introduction to Python课程笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。