一、Python简介
诞生
Python的作者是著名的“龟叔”Guido van Rossum,1989年,龟叔为了打发无聊的圣诞节,开始编写Python语言。1991年,第一个Python编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。
Python主要应用领域
-
人工智能: 典型库NumPy, SciPy, Matplotlib, TensorFlow(张量流)
-
云计算: 云计算最火的语言, 典型应用OpenStack
云计算的一个比喻(便于理解)https://kb.cnblogs.com/page/101907/ -
WEB开发: 众多优秀的WEB框架,众多大型网站均为Python开发,Youtube, Dropbox, 豆瓣。。。, 典型WEB框架有Django,Flask
-
系统运维: 运维人员必备语言
-
金融:量化交易,金融分析,在金融工程领域,Python不但在用,且用的最多,而且重要性逐年提高。
-
图形GUI: PyQT, WxPython,TkInter
Python中文文档
https://docs.python.org/zh-cn/3/
集成开发环境 IDE
我们使用Pycharm
下载地址:http://www.jetbrains.com/pycharm
项目文件夹中的.idea文件是该项目的个性化配置
二、Python变量标识符关键字
# -*- coding: utf-8 -*-
# @Time : 2019/11/4 14:48
# @Author : Han lei
# @Email : hanlei5012@163.com
# @File : demo2.py
# @Software: PyCharm
# 变量、标识符、关键字
# 变量(Variable)
# 在python中每个变量在使用前必须进行赋值,变量赋值以后该变量才会被创建
# 使用 = 进行赋值 等号左边是变量的名字,等号右边是变量的值
# 变量名 = 值
# 等号两边各空一个空格, 美观
# 名字变量 = "张三" # 中文变量命名只能在Python3中使用,但是不推荐
# print(名字变量)
# 实例1:学生注册
# 定义变量---学生学号
# studentNo = '8633'
# 定义变量---学生密码
# studentPassword = '123456'
# 使用print函数输出
# print(studentNo)
# print(studentPassword)
# 延展
# 1.print()函数可以一次输出多个变量,中间用逗号隔开
# print('学生学号:', studentNo, '密码:', studentPassword)
# 2.每次print输出都是默认换行的,如果不需要换行,可以将end参数设置为''
# print(studentNo, end='') # 默认end='\n' 不需要换行改成end=''
# print(studentPassword)
# 3.在Python2.x版本中 print并不是一个函数,写法是print "hello world"
# 所以判断代码使用的Python版本的最直接方式是看print的格式,
# 所以粘贴Python2的代码在Python3中无法运行试试正常的
# print " "
# 代码缩进
# Python是使用缩进来组织代码块的,这是强制要求, 下面这样声明是错误的
# studentNo = '8633'
# print(studentNo)
# studentNo = '8633'
# print(studentNo)
# 练习:超市买菜
# 定义黄瓜的价格
h_price = 5
# 定义购买重量 (kg)
h_weight = 6
# 计算金额
h_money = h_price*h_weight
# 显示输出
print(h_money)
# 变量的类型
# 变量的四要素
# 1. 变量名称
# 2. 变量保存的数据
# 3. 变量存储数据的类型
# 4. 变量的内存地址(标识)# at 0x00430378
# 需求
# 定义变量保存小强的个人信息
# 姓名:小强
# 年龄:22 岁
# 性别:是男生
# 身高:1.80 米
# 体重:77.0 公斤
name = '小强'
print(type(name)) # <class 'str'>
age = 22
print(type(age)) # <class 'int'>
sex = False
print(type(sex)) # <class 'bool'>
height = 1.80
print(type(height)) # <class 'float'>
weight = '77.0公斤'
print(type(weight)) # <class 'str'>
print('haha')
# 调试入门 debug F8单步调试
# python 在定义变量时不需要指定变量的类型
# 变量在运行过程中,Python解释器会自动推导出变量的精确类型,这就是动态语言(弱类型)的一大特性之一
# 变量可以按照类型分为数字类型和非数字
# 数字
# int float bool
# 非数字: 列表、元组、字符串、字典、集合
# 可以使用type()内置函数查看变量的数据类型
# 内置函数在Python中直接调用即可,不用进行其他操作
# 常见的内置函数有:input() type() int()...
# 不同变量之间的运算
# 1. 在Python中数字型变量可以直接进行算数运算
# 如果变量是布尔型的话 True----> 1; False ----> 0
# i = 10
# f = 10.1
# b = True
# 一行声明以上的变量
# i, f, b = 10, 10.1, True
# print(i+f+b) # 21.1 以上运算发生了类型自动转换,全部转化成了精确度最大的浮点数
#
# # python中快速交换两个变量的方式
# a = 100
# b = 1000
# a, b = b, a
# print('a= ', a, 'b= ', b) # a= 1000 b= 100
# 在Python中,字符串之间可以使用+ 号进行拼接,生成新的字符串
first_name = '最帅'
last_name = '李'
num = 1
# print(last_name+first_name+1)
# 数字和字符串之间不能进行连接
# 解决办法
# 1.
print(last_name+first_name+"1")
# 2.str()内置函数可以将变量强制转换成字符串类型
print(last_name+first_name+str(num)) # 李最帅1
# 字符串变量可以和整数使用*,可以达到重复拼接字符串的效果
print('hehe'*50)
print('--'*50) # 做分界线
# 变量的输入
# input()内置函数可以实现在控制台中进行输入,
# 所谓函数,我们暂时可以理解为别人写好的代码,我们直接调用就完了,不用关心内部的实现细节
# 超市买苹果案例
apple_price = input('请输入苹果的价格(元):')
apple_weight = input('请输入苹果的重量(kg):')
# 进行类型转换
apple_price = float(apple_price)
apple_weight = float(apple_weight)
# 计算总金额
amount = apple_price*apple_weight
print('总金额为:', amount)
变量格式化输出
格式:print('格式化字符串'%(变量1, 变量2, ....))
# -*- coding: utf-8 -*-
# @Time : 2019/11/4 16:53
# @Author : Han lei
# @Email : hanlei5012@163.com
# @File : demo3.py
# @Software: PyCharm
# 变量格式化输出
# 1. 使用 % 进行格式化输出
# 格式
# print('格式化字符串'%(变量1, 变量2, ....))
# %s 字符串, %d 有符号十进制数 %06d 表示输出的整数显示位数 不足的地方使用 0 补全, %f 浮点数 %.2f表示小数点后只显示两位, %% 输出的就是%本身
name = '鲁班七号'
grade = 15
print('当前使用英雄为%s,当前等级为%d级' % (name, grade))
# 举个栗子
# 需求
# 定义字符串变量 name,输出 我的名字叫 小明,请多多关照!
# 定义整数变量 student_no,输出 我的学号是 000001
# 定义小数 price、weight、money,输出 苹果单价 9.00 元/斤,购买了 5.00 斤,需要支付 45.00 元
# 定义一个小数 scale,输出 数据比例是 10.00%
name = '小明'
print('我的名字叫%s,请多多关照!' % (name))
print('--'*50)
student_no = 1
print('我的学号是%06d' % (student_no))
print('--'*50)
price, weight = 9, 5
money = price * weight
print('苹果单价%.2f元/斤,购买了%.2f斤,需要支付%.2f元' % (price, weight, money))
print('--'*50)
scale = 10
print('数据比例是%.2f%%' % (scale))
网友评论