python_01

  • python的特性 1.语法简单,容易理解,容易学习 2.跨平台,可在windows/linux/mac os等系统上运行 3.可做网站、爬虫、大数据处理、机器学习 4.拥有强大的第三方库,numpy,pandas...

print("hello, world")

print("hello, \"world")  --output:  hello, "world  (使用转义字符\ )

print('hello, \'world')   --output:  hello, 'world

  • 合理使用单引号,双引号,避免使用转义字符:  

print("hello, 'world") --output: hello, 'world

print('hello, "world')  --output: hello, "world

import math

math.pi --output: 3.141592653589793

math.sin(math.pi/2) --output: 1.0

math.floor(9.2343)--output: 9  //向下取整

math.ceil(9.2343)--output: 10  //向上取整

  • 应用题:小姐姐去买水果,苹果5元一斤,葡萄15元一斤,现在小姐姐买了2斤苹果,2.5斤葡萄,问:小姐姐买这两种水果分别花了多少去,总共花了多少钱?

# 苹果的花费
print(5*2)

# 葡萄的花费
print(15*1.5)

#总花费
print((5*2)+(15*1.5))

  • 三个问题:

    1.如果脱离了题干和注释,无法理解5*2的意思
    2.计算总价时,重新计算了苹果和葡萄的花费
    3.输出是一个数字,表达不清晰

apple_price = 5
apple_weight = 2

apple_cost = apple_price *apple_weight

grape_price = 15
grape_weight = 1.5

grape_cost = grape_price * grape_weight

total_cost = apple_cost + grape_cost 

print(apple_cost, grape_cost, total_cost)

  • 增强的格式化字符串函数 format

In [ ]:   "苹果花费为: {};葡萄花费为: {};总花费为: {}".format(apple_cost, grape_cost, total_cost) //输出一个拼接的字符串

Out[ ]: '苹果花费为: 10;葡萄花费为: 22.5;总花费为: 32.5'

  • 变量:代表某个值的名称

a = 10
b = 20

a, b = b, a    //进行a, b两个值的互换

print("a is {}, b is {}".format(a, b))  

Out[ ]: a is 20, b is 10

  • 命名规范

   1.标识符的第一个字符必须是字母表中的字母(大写或者小写)或者一个下划线
    2.标识符名称的其它部分可以由字母(大写或者小写)、下划线('_')或数字(0-9)组成
    3.标识符名称是对大小写敏感的

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值