【数据分析】python基础快速入门,超快(02)

本节简介运算符、循环语句、条件语句、函数、模块的概念

Section 1 运算符

  • 算数运算符
  • 比较运算符
  • 逻辑运算符
# 算术运算符
print(10 + 20)
print(10 - 20)
print(10 * 20)
print(10 / 20)    # 返回小数
print(20 / 10)
print(10 // 20)    # 相除后返回整数部分
print(10 % 20)    # 取余数
print(10**3)    # m的n次幂,m**n
'''
结果
30
-10
200
0.5
2.0
0
10
1000
'''
# 比较运算符
print(10 == 20)
print(10 != 20 )
print(10 > 20)
print(10 < 20)
print(10 >= 20)
print(10 <= 20)
'''
结果
False
True
False
True
False
True
'''
# 逻辑运算符
# 与and 或or 非not
print((10 > 20) and (10 < 20))    # false and true -> false ,只运算半边
print((10 > 20) or (10 < 20))    # false or true -> true 
print(not (10 < 20))    # not true -> false
'''
结果
False
True
False
'''

Section 2 循环语句

'''
for 循环格式
for xxx in xxx:
'''
# 计算 1-100 的和
sum = 0
for i in range(1, 101):    # i 从1到100
    sum = sum + i    # for循环i自增不用管
print(sum) 
'''
结果
5050
'''
'''
while 循环格式
while 循环条件 :
    statement
条件满足时执行statement,条件不满足就跳出次循环
'''
# 计算1-100的和
sum = 0
i = 0
while i<=100:
    sum = sum + i
    i = i + 1    # i的自增必须自己写!! 区别for循环
print(sum)
'''
结果
5050
'''

Section 3 条件语句

  • if部分必须有,elif语句可以没有,else有则必须放在最后面
  • 可以有多个elif, 但只能有一个if 和 else
score = 75
if score > 80:
    print("优秀")
elif score > 70:
    print("通过")
elif score > 60:
    print("及格")
else:
    print("不及格!")
    '''
结果
通过
'''

Section 4 函数

  • 函数即程序中可以被重复使用的一段程序
  • 构成要素:函数名(必须)、参数、语句块(必须)、返回值
  • 格式:def 函数名(参数):
  •       语句块
    
# 无返回值的函数
def learn_py(location):
    print("我在{}学习python".format(location))

learn_py("地铁")
learn_py("教室")
'''
关于python输出格式化字符串:
python 2:
print("我叫%s,今年读%d年级" % ('小明',5))    # 我叫小明,今年读5年级
和C语言的格式化字符串非常类似

pyhton 3:
print("我叫{},今年读{}年级".format('小明',5))    # 我叫小明,今年读5年级
或者
print("我叫",'小明', "今年读", 5, "年级")    # 我叫 小明 今年读 5 年级
第二种算是字符串拼接,不推荐,可能产生空格
python 2的方式在python 3 中也能用,但不推荐,首选format()
'''
'''
结果
我在地铁学习python
我在教室学习python
我叫小明,今年读5年级
我叫 小明 今年读 5 年级
'''
# 有返回值的函数
def learn_pyth(location):
    doing = "我在{}学习python".format(location)
    return doing
arr = learn_pyth("车站")
print(arr)
'''
我在车站学习python
'''
# 多个参数的函数
def learn_python(num, location):
    doing = "我和{}个同学在{}学习python".format(num, location)
    return doing

arr = learn_python(5, "教室")
print(arr)
'''
我和5个同学在教室学习python
'''

Section 5 模块

  • 如果要使用函数,必须在同一文件内有相应的函数定义
  • 但是如果把所有代码都写在同一个文件内,这个文件会很大且不易维护
  • 所以将功能相近的函数,抽象成一个模块,然后Import导入
  • 例如pandas,numpy等都是模块
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值