Python语法入门-课件下载

链接: https://pan.baidu.com/s/1h1RuWimLicVanK8xCka_xA 提取码: x264


if条件判断

condition为布尔值、布尔运算、成员运算符

通常我们理解的condition为布尔值

#Tab
condition = True

if condition:
    print('condition为True')
else:
    print('condition为False')
condition为True

age = 17

if age>=18:
    print('你是成年人了')
else:
    print('你还是个孩子')
你还是个孩子

age = 20

if age>=55:
    print('老年人')
elif 35<=age<55:
    print('中年')
elif 18<=age<35:
    print('青年')
elif 0<=age<18:
    print('儿童')

青年



特殊的conditon

  • 各种空值(空字符串、空列表等)作用等同于False
  • 各种非空值,作用等同于True
a = None

if a:
    print('a是非空数据')
else:
    print('a是空数据')
a是空数据



for循环

  • 重复做某件事
  • 迭代出数据中的内容(元素)

上面这个图可以解读为

我们想对iterable这个集合中的每一个item:

做点事(对item做操作)

重复做某事

问题1

计算1+2+3+…+97+98+99+100=?

1 + 2 = 3 3 + 3 = 6 6 + 4 = 10 10 + 5 = 15

result = 0

#int
for i in range(1, 101):
    result = result + i
    
print(result)
5050

迭代出数据中的内容

从某种“集合”(这个“集合”可以使list、set、tuple等),只要“集合”内部有多个成员就可以使用for循环迭代出内部的成员

names = ['David', 'Henry', 'Mary']

for name in names:
    print(name)
David
Henry
Mary

name = 'David'

for s in name:
    print(s)
D
a
v
i
d

infos =  {'David':{'age':25, 
                   'gender':'Male'},
          'Mary':{'age':23,
                  'gender': 'Female'},
          'Henry':{'age':23,
                  'gender': 'Male'}
         }

for item in infos.items():
    print(item)
('David', {'age': 25, 'gender': 'Male'})
('Mary', {'age': 23, 'gender': 'Female'})
('Henry', {'age': 23, 'gender': 'Male'})

for name, info in infos.items():
    print(name, info)
David
Mary
Henry



try-except

遇到无关紧要的bug,不会停下来,让程序有一定的容错能力。通俗点就是此处不留爷,自有留爷处,凡事别钻牛角尖。

for x in [1,2,0,2,1]:
    print(10/x)
10.0
5.0
---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-19-83bea9c92c0e> in <module>
      1 for x in [1,2,0,2,1]:
----> 2     print(10/x)
ZeroDivisionError: division by zero

for x in [1,2,0,2,1]:
    try:
        print(10/x)
    except:
        print('0除错误')
        #pass
10.0
5.0
0除错误
5.0
10.0



练习1

假设现在某人的能力为1, 每天比前一天进步0.01, 一年后他的能力是多少?

ability = 1
scale = 1.01

records = []
for i in range(365):
    ability = ability * 1.01
    records.append(ability)
    
print(records)
[1.01, 1.0201, 1.030301, 1.04060401, 1.0510100501, 1.061520150601, 1.0721353521070098, 1.08285670562808, 1.0936852726843609, 1.1046221254112045, 1.1156683466653166, ...................36.30913774096189, 36.672229118371504, 37.03895140955522, 37.40934092365077, 37.783434332887275]

import matplotlib.pyplot as plt
import math
%matplotlib inline

ability = 1
scale = 1.02
records = []

days = range(1, 365)
for day in days:
    ability = ability*scale
    records.append(ability)
 

plt.plot(days, records)
plt.title('Be better everyday!')
Text(0.5, 1.0, 'Be better everyday!')



安装包的方法

  1. 命令行执行 pip install packagename
  2. jupyter notebook的Cell中执行!pip install packagename
  3. 如果是mac,pip写成pip3
!pip install matplotlib
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: matplotlib in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (3.2.1)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from matplotlib) (1.2.0)
Requirement already satisfied: python-dateutil>=2.1 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: cycler>=0.10 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: numpy>=1.11 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from matplotlib) (1.18.2)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from matplotlib) (2.4.7)
Requirement already satisfied: six>=1.5 in c:\users\thunderhit\appdata\local\programs\python\python37-32\lib\site-packages (from python-dateutil>=2.1->matplotlib) (1.14.0)

练习2

打印九九乘法表格

用到的知识点:

  • for循环
  • 字符串format方法
  • print函数(涉及到end参数)
for row in range(1, 10):
    #print(row)
    for col in range(1, row+1):
        formula = '{col}*{row}={res}'.format(col=col, row=row, res=col*row)
        print(formula, end='\t')
    print('')
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

for row in range(1, 10):
    for col in range(1, row+1):
        formula = '{col}*{row}={res}'
        content = formula.format(col=col, row=row, res=col*row)
        print(content, end='\t')
    print()
    
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	



了解课程

点击上方图片购买课程

点击进入详情页