Introduction to Python

Introduction to Python

简单运算

Example, do not modify!

print(5 / 8)

Print the sum of 7 and 10

print(7+10)

加注释

Division

print(5 / 8)

各类运算

#Addition
print(7 + 10)

Addition, subtraction

print(5 + 5)
print(5 - 5)

Multiplication, division, modulo, and exponentiation

print(3 * 5)
print(10 / 2)
print(18 % 7)
print(4 ** 2)

How much is your $100 worth after 7 years?

print(100*1.1**7)
设置变量和其类型

Create a variable savings

savings=100
int(savings)

Print out savings

print(savings)

用定义变量的方式进行复利计算

Create a variable savings

savings = 100
int(savings)

Create a variable growth_multiplier

growth_multiplier=1.1
float(growth_multiplier)

Calculate result

result=savings*growth_multiplier**7

Print out result

print(result)

定义变量类型(字符串、布尔)

Create a variable desc

desc=“compound interest”
str(desc)

Create a variable profitable

profitable=True
bool(profitable)

查看变量类型和字符串相加结果

savings = 100
growth_multiplier = 1.1
desc = “compound interest”

Assign product of growth_multiplier and savings to year1

year1=savings*growth_multiplier

Print the type of year1

print(type(year1))

Assign sum of desc and desc to doubledesc

doubledesc=desc+desc
print(doubledesc)

Print out doubledesc

print(doubledesc)

整型、浮点型与字符型的转换

Definition of savings and result

savings = 100
result = 100 * 1.10 ** 7

Fix the printout

print(“I started with $” + str(savings) + " and now have $" + str(result)+ “. Awesome!”)

Definition of pi_string

pi_string = “3.1415926”

Convert pi_string into float: pi_float

pi_float=float(pi_string)

list类型定义

area variables (in square meters)

hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

Create list areas

areas=[hall,kit,liv,bed,bath]

Print areas

print(areas)

给list中的每个元素加上元素名

area variables (in square meters)

hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

Adapt list areas

areas = [“hallway”,hall,“kitchen”,kit, “living room”, liv, “bedroom”,bed, “bathroom”, bath]

Print areas

print(areas)

house作为列表中的列表

area variables (in square meters)

hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

house information as list of lists

house = [[“hallway”, hall],
[“kitchen”, kit],
[“living room”, liv],
[“bedroom”,bed],
[“bathroom”,bath]]

Print out house

print(house)

Print out the type of house

print(type(house))

取元素操作

Create the areas list

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Print out second element from areas

print(areas[1])

Print out last element from areas

print(areas[-1])

Print out the area of the living room

print(areas[5])

取元素,运算

Create the areas list

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Sum of kitchen and bedroom area: eat_sleep_area

eat_sleep_area=areas[3]+areas[-3]

Print the variable eat_sleep_area

print(eat_sleep_area)
分割list

Create the areas list

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Use slicing to create downstairs

downstairs=areas[0:6]

Use slicing to create upstairs

upstairs=areas[6:10]

Print out downstairs and upstairs

print(downstairs)
print(upstairs)

另一种分割的方法

Create the areas list

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Alternative slicing to create downstairs

downstairs=areas[:6]

Alternative slicing to create upstairs

upstairs=areas[6:]

修改list中的元素

Create the areas list
areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Correct the bathroom area

areas[-1]=10.50

Change “living room” to “chill zone”

areas[4]=“chill zone”

增加list中的元素

Create the areas list and make some changes

areas = [“hallway”, 11.25, “kitchen”, 18.0, “chill zone”, 20.0,
“bedroom”, 10.75, “bathroom”, 10.50]

Add poolhouse data to areas, new list is areas_1

areas_1=areas+[“poolhouse”,24.5]

Add garage data to areas_1, new list is areas_2

areas_2=areas_1+[“garage”,15.45]

赋值给areas_copy中的元素不影响area

Create list areas

areas = [11.25, 18.0, 20.0, 10.75, 9.50]

Create areas_copy

areas_copy =list(areas)

Change areas_copy

areas_copy[0] = 5.0

Print areas

print(areas)

使用常用的函数

Create variables var1 and var2

var1 = [1, 2, 3, 4]
var2 = True

Print out type of var1

print(type(var1))

Print out length of var1

print(len(var1))

Convert var2 to an integer: out2

out2=int(var2)

将full_sorted的元素降序排列

Create lists first and second

first = [11.25, 18.0, 20.0]
second = [10.75, 9.50]

Paste together first and second: full

full=first+second

Sort full in descending order: full_sorted

full_sorted=sorted(full,reverse=True)

Print out full_sorted

print(full_sorted)

upper()函数,count()函数

string to experiment with: place

place = “poolhouse”

Use upper() on place: place_up

place_up=str.upper(place)

Print out place and place_up

print(place)
print(place_up)

Print out the number of o’s in place

place.count(“o”)
print(place.count(“o”))

index()函数查找元素在list中的位置

Create list areas

areas = [11.25, 18.0, 20.0, 10.75, 9.50]

Print out the index of the element 20.0

print(areas.index(20.0))

Print out how often 9.50 appears in areas

print(areas.count(9.50))

Create list areas

areas = [11.25, 18.0, 20.0, 10.75, 9.50]

append()函数增加list元素,reverse()函数将元素倒序排列

Use append twice to add poolhouse and garage size

areas.append(24.5)
areas.append(15.45)

Print out areas

print(areas)

Reverse the orders of the elements in areas

areas.reverse()

Print out areas

print(areas)

调用math中的pi

Definition of radius

r = 0.43

Import the math package

import math
pi=math.pi

Calculate C

C = 0
C=2pir

Calculate A

A = 0
A=pi*r**2

Build printout

print("Circumference: " + str©)
print("Area: " + str(A))

调用radians函数

Definition of radius

r = 192500

Import radians function of math package

from math import radians

Travel distance of Moon over 12 degrees. Store in dist.

dist=r*radians(12)

Print out dist

print(dist)

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))

将list转成numpy数组,并进行运算

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)

计算BMI

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_lb=np.array(weight_lb)
np_weight_kg=np_weight_lb*0.453592

Calculate the BMI: bmi

bmi=np_weight_kg/np_height_m**2

Print out bmi

print(bmi)

求BMI<21的数据

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=np.array(bmi)
light=bmi<21

Print out light

print(light)

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

print(bmi[light])

取numpy子集

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(np_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])

构建二维numpy数组

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)

构造二维数组,.shape输出数组大小

baseball is available as a regular list of lists

Import numpy package

import numpy as np

Create a 2D numpy array from baseball: np_baseball

np_baseball=np.array(baseball)

Print out the shape of np_baseball

print(np_baseball.shape)
取二维numpy子集

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[123,0])

二维数组运算

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)

平均值和中位数.mean()函数和.median()函数

np_baseball is available

Import numpy

import numpy as np

Create np_height_in from np_baseball

np_height_in=np.array(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))

.std()和.corrcoef()

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))

分别计算GK和非GK的中位数

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_positions=np.array(positions)
np_heights=np.array(heights)

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)))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值