Python学习笔记_Python 基础

一、数据类型和变量

数据类型:整数、浮点数、字符串、布尔值、空值
变量
常量

Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来。
对变量赋值x = y是把变量x指向真正的对象,该对象是变量y所指向的。随后对变量y的赋值不影响变量x的指向。

Python是一门动态语言。Java是静态语言,赋值语句如下:

int a = 123; // a是整数类型变量 
a = "ABC"; // 错误:不能把字符串赋给整型变量

二、字符串和编码

1、字符编码

最早只有127个字符被编码到计算机里,这个编码表被称为ASCII编码Unicode用两个字节表示一个字符。UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节。

在最新的Python 3版本中,字符串是以Unicode编码的。
当Python源代码中包含中文时,务必指定保存为UTF-8编码。当Python解释器读取源代码时,为了让它按UTF-8编码读取,我们通常在文件开头写上这两行:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

第一行注释是为了告诉Linux/OS X系统,这是一个Python可执行程序,Windows系统会忽略这个注释;
第二行注释是为了告诉Python解释器,按照UTF-8编码读取源代码,否则,你在源代码中写的中文输出可能会有乱码。

2、格式化

在Python中,采用的格式化方式和C语言是一致的,用 % 实现。

练习:小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出’xx.x%’,只保留小数点后1位。

# -*- coding: utf-8 -*-
s1 = 72
s2 = 85
r = (s2-s1)/s1*100
print('%.1f %%' % r)

Run:18.1 %

占位符 替换内容
%d、 整数
%f、浮点数
%s、字符串
%x、十六进制整数

三、使用list和tuple

list和tuple是Python内置的有序集合,一个可变,一个不可变。根据需要来选择使用它们。
list:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

classmates = ['Michael', 'Bob', 'Tracy']
print('classmates =', classmates)
print('len(classmates) =', len(classmates))
print('classmates[0] =', classmates[0])
print('classmates[1] =', classmates[1])
print('classmates[2] =', classmates[2])
print('classmates[-1] =', classmates[-1])

classmates.pop()
print('classmates =', classmates)

tuple:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

classmates = ('Michael', 'Bob', 'Tracy')
print('classmates =', classmates)
print('len(classmates) =', len(classmates))
print('classmates[0] =', classmates[0])
print('classmates[1] =', classmates[1])
print('classmates[2] =', classmates[2])
print('classmates[-1] =', classmates[-1])

# cannot modify tuple:
classmates[0] = 'Adam'

【练习】请用索引取出下面list的指定元素:

# -*- coding: utf-8 -*-

L = [
    ['Apple', 'Google', 'Microsoft'],
    ['Java', 'Python', 'Ruby', 'PHP'],
    ['Adam', 'Bart', 'Lisa']
]

# 打印Apple:
print(L[0][0])
# 打印Python:
print(L[1][1])
# 打印Lisa:
print(L[2][2])

Run:Apple Python Lisa

四、条件判断

【练习】小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数,用if-elif判断并打印结果:
低于18.5:过轻
18.5-25:正常
25-28:过重
28-32:肥胖
高于32:严重肥胖

# -*- coding: utf-8 -*-

height = 1.75
weight = 80.5

bmi = weight / height * height
if bmi < 18.5:
    print('过轻')
elif bmi < 25:
    print('正常')
elif bmi < 28:
    print('过重')
elif bmi < 32:
    print('肥胖')
else:
    print('严重肥胖')

Run:严重肥胖

五、循环

1、for…in循环
names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)
2、while循环
sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
print(sum)

【练习】请利用循环依次对list中的每个名字打印出Hello, xxx!

# -*- coding: utf-8 -*-
L = ['Bart', 'Lisa', 'Adam']
for L in L:
    print('Hello,'+ L+'!')

Run: Hello,Bart! Hello,Lisa! Hello,Adam!

3、break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环,并直接开始下一轮循环。这两个语句通常都必须配合if语句使用。

六、使用dict和set

>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95
>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}

版权声明:本文为m0_53363188原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。