python入门(三) 基础用法变量+数据类型+流程控制
文章目录
-
- 背景
-
- 1)单行和多行注释
- 2)变量及基础数据类型
- 3)条件语句
- 4)循环语句
- 5)条件、循环嵌套语句
- 6)list(列表)
- 7)tuple(元组)
- 8)列表、元组区别
- 9)字典(dict)
- 10) 函数
- 11)引入模块
- 12)运行时传递参数
背景
本人工作中,用到了ai相关技术,但是java出身,所以从0开始学习,先从python入门了,本文章为第三篇。
“前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。”
1)单行和多行注释
"""
! author: hhh
! date: 2022-12-28
这是第2课的演示程序。多行注释,常用来声明作者、时间,描述整个文件、类的功能等
程序不会运行注释内的文本,主要为了方便人理解程序
"""import time # (单行注释)导入时间模块print(time.time()) # 显示UNIX时间戳,即:1970年01月01日00时00分00秒起至现在的总秒数
# 1681473283.0174155def calSquare(x):"""计算x平方的函数:param x 传进来一个整数x:return 返回计算结果"""square = x 2 return squareprint(calSquare(3))
#9
2)变量及基础数据类型
"""
!author: hhh
!date: 2021-12-29
演示变量及基础数据类型:计算每个月开支"""# 计算11月开支
# 房租水电:¥3500
# 餐饮:¥1200
# 服装 ¥500# 总开支
print(3500 + 1200 + 500)
# 5200# 计算12月开支
# 房租水电:¥3500
# 餐饮:¥1300
# 服装 ¥300
# 买手机 ¥2000# 总开支
print(3500 + 1300 + 300 + 2000)
#7100# 但这样有2个问题:
# 1、很麻烦,如果不写注释,过段时间,这些数字代表什么意思肯定不记得了;
# 2、类似房租这样每个月固定的开支,每个月都要写一遍,无法简化计算。# 怎么办?我们可以使用变量来解决,使用四个变量分别存储各项开支,每月要计算总开支的话只需修改变动的数值即可重新计算。# 使用变量计算开支
cost_rent = 3500 # 房租水电开支,每月固定¥3500
cost_meals = 1300 # 餐饮开支
cost_clothing = 300 # 服装开支
cost_other = 2000 # 其他开支# 总开支
print( cost_rent + cost_meals + cost_clothing + cost_other )
#7100
3)条件语句
"""
! author: hhh
! date: 2021-12-29
演示Python条件语句
"""age_xiaoming = 27
age_xiaohong = 24if age_xiaoming > age_xiaohong: print('小明比小红大')elif age_xiaoming ==age_xiaohong:print('小明和小红一样大')else:print('小明比小红小')#小明比小红大
4)循环语句
"""
! author: hhh
! date: 2021-12-29
演示Python循环语句
"""# while {条件表达式}:
# {代码块}index = 0while index <= 10:print(index)index = index + 1 # 重新赋值,自增1"""
0
1
2
3
4
5
6
7
8
9
10
"""# for {迭代变量} in {可迭代对象}:
# {代码块}for i in 'Python':print(i)"""
P
y
t
h
o
n
"""for x in [1,3,5,6]:print(x)"""
1
3
5
6
"""for y in range(11):print(y)"""
0
1
2
3
4
5
6
7
8
9
10
"""
5)条件、循环嵌套语句
"""
! author: hhh
! date: 2021-12-29
演示Python条件、循环嵌套语句
"""# 输出 0~10内的偶数
for i in range(0,10):if i % 2 == 0: # 判断i能否被2整除print(i) """
0
2
4
6
8
"""
6)list(列表)
"""
! author: hhh
! date: 2021-12-29
演示Python list(列表)
"""# 3个变量分别存储三个字符串
fruit_a = '苹果'
fruit_b = '香蕉'
fruit_c = '橘子'# 一个list变量存储3个字符串
fruit_list = ['苹果','香蕉','橘子']# 打印列表
print(fruit_list)
# ['苹果', '香蕉', '橘子']# 打印列表第1个元素
print( fruit_list[0] ) #苹果
# 打印列表第2个元素
print( fruit_list[1] ) #香蕉 # 打印列表元素数量
print( len(fruit_list) )# 增加一个元素
fruit_list.append('橙子')
print(fruit_list) # ['苹果', '香蕉', '橘子', '橙子']# 修改一个元素
fruit_list[0] = '芒果'fruit_list = ['苹果','香蕉','橘子']# 删除一个元素:根据索引
del fruit_list[1]
print(fruit_list) # ['苹果', '橘子']# 删除一个元素:根据值
fruit_list.remove('苹果')
print(fruit_list) # ['橘子']
7)tuple(元组)
"""
! author: hhh
! date: 2021-12-29
演示Python tuple(元组)
"""fruit_tuple = ('苹果','香蕉','橘子')
print(fruit_tuple) #('苹果', '香蕉', '橘子')# 打印元组第1个元素
print( fruit_tuple[0] ) # 苹果
# 打印元组第2个元素
print( fruit_tuple[1] ) # 香蕉# 打印元组元素数量
print( len(fruit_tuple) ) # 3
8)列表、元组区别
"""
! author: hhh
! date: 2021-12-29
演示Python 列表、元组区别
"""# 定义一个列表
fruit_list = ['苹果','香蕉','橘子']
# 将列表第2项变更为 芒果
fruit_list[1] = '芒果'# 打印新列表
print(fruit_list) # ['苹果', '芒果', '橘子']# 定义一个元组
fruit_tuple = ('苹果','香蕉','橘子')# 尝试将元组第二项变更为 芒果,会报错
fruit_tuple[1] = '芒果'"""
Traceback (most recent call last):File ".\\demo8.py", line 20, in <module>fruit_tuple[1] = '芒果'
TypeError: 'tuple' object does not support item assignment
"""
9)字典(dict)
"""
! author: hhh
! date: 2021-12-29
演示Python 字典(dict)
"""# 定义一个人员的字典
person_dict = {'name':'张三','height': 183.2,'age': 27, 'graduated': True
}# 打印这个字典
print(person_dict)
# {'name': '张三', 'height': 183.2, 'age': 27, 'graduated': True}# 打印这个人的年龄
print( person_dict['age'] )
#27# 打印字典长度
print( len(person_dict) )
# 4# 增加一个元素
person_dict['weight'] = 120
print(person_dict)
# {'name': '张三', 'height': 183.2, 'age': 27, 'graduated': True, 'weight': 120}# 修改一个元素
person_dict['height'] = 181.9
# 打印修改后的身高
print(person_dict['height'])
# 181.9# 删除一个元素
del person_dict['name']
print(person_dict)
# {'height': 181.9, 'age': 27, 'graduated': True, 'weight': 120}
10) 函数
"""
! author: hhh
! date: 2021-12-29
演示Python 函数
"""# 创建一个函数
def my_function():# 函数执行部分print('这是一个函数')# 调用1次这个函数
my_function()
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数
#这是一个函数# 调用10次这个函数
for i in range(0,10):my_function()# 有参数的函数
def say_hello(name,age):# 函数执行部分print( name + '说:我今年' +age+ '岁')# 调用有参数的函数
say_hello('小明','25') # 小明说:我今年25岁
say_hello('小红','22') # 小红说:我今年22岁
11)引入模块
customModule.py
"""
! author: hhh
! date: 2021-12-29
演示Python 自定义模块
"""# 说hello的函数
def hello():print('这个是模块内的函数')
demo11.py
"""
! author: hhh
! date: 2021-12-29
演示Python 模块
"""# 导入Python内置模块
import re # 正则
import random # 随机数
import datetime # 日期# 使用模块一个函数
print( random.randint(0,10) ) # 2# 导入自定义模块
import customModule as myMod
# 调用模块内的函数
myMod.hello()# 这个是模块内的函数
12)运行时传递参数
"""
演示Demo
"""from argparse import ArgumentParser# 参数
parser = ArgumentParser()parser.add_argument("--width", type=int, default=960,help="宽度")
parser.add_argument("--height", type=int, default=720,help="高度")
args = parser.parse_args()area = int (args.width * args.height)print('面积为' + str(area) )
python命令行中执行:
python .\\hello.py --width=100 --height=30
运行结果:
面积为3000
大功告成!!!
“前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。”