python学习笔记(一)

本文介绍了Python编程的基础知识,包括变量、运算符、条件语句、流程控制、函数等核心概念,并通过实例演示了其使用方法。此外,还涉及了函数参数、关键字参数、默认参数和可变参数的传递方式,以及注释和空语句的作用。文章以实际编程练习为结尾,展示了如何输出矩形和三角形图案,进一步巩固了循环控制的理解。

1 变量

_123 = 'hello,world'
print(_123)
a = 2
print(a)
# 错误写法
1a = 4
print(1a) 

变量是数据的容器, 变量由字母,数字,下划线组成, 数字不能是第一位

2 运算符 + - * / %(取余运算符) **(幂运算符) //(整除运算符)

print(2 ** 3)
print(15 //4)
print(101+500)
print(500-200)
print(1000*200)

3 比较运算符 > < >= <= == !=(不等于运算符)

print(5 > 3)
print(3 > 8)
print(5 == 5)
print(1.1 + 2.2 == 3.3)
print(2+3!=6)

4  逻辑运算符 and or not

sex = '男'
age = 17
if sex == '男' and age >= 18:
    pass
flag = True
if not flag:
    pass

5 成员运算符 in not in

i = [2, 4, 6, 8]
print(2 in i)
print(10 not in i)

6  赋值运算符 = += *= /= %= **=

​
a = 5
a += 5
print(a)

​

7 条件语句

score = 75
if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 60:
    print('及格')
else:
    print('不及格')

8 流程控制语句 for/while

for i in range(6):
    print(i)
n = 0
while n < 5:
    print(n)
    n += 1

9 嵌套循环

# 实践:输出一个九行九列的*矩形
for i in range(9):
    for j in range(9):
        print('*', end='')
    print('')
# 实践:输出一个三角形
for i in range(9):
    for j in range(1, i + 1):
        print('*', end='')
    print('')

10 注释: 单行注释 , 多行注释

​
'''
多行注释
'''
# 单行注释

​

11 pass 空语句,作用是保证代码完整性

if 5 > 3:
    pass

12 定义函数--调用函数

def add():  # 函数就是代码块
    print('add函数调用了')


# 函数调用
add()

13 函数参数的传递

def sub(x, y):
    print(x - y)


sub(5, 3)

14 关键字参数

def sayhi(name, age):
    print(name + ':' + '我今年' + str(age) + '岁!')


sayhi('张三', 22)

15 默认参数

def tell(name, age, cn='中国'):
    print(name, age, cn)


tell('tom', 22)
tell('jack', 23, '新加坡')

16 可变参数

def totle(*price):
    print(price)
    totle(2, 3, 5)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值