Python基础学习-02

1、运算符

1、 算术运算符

假设变量 a 为 10,变量 b 为 21:

2 、比较运算符

== 等于 - 比较对象是否相等 (a == b) 返回 False
!= 不等于 - 比较两个对象是否不相等 (a != b) 返回 True
> 大于 - 返回 x 是否大于 y (a > b) 返回 False
< 小于 - 返回 x 是否小于 y 。所有比较运算符返回 1 表示真,返回 0
表示假。这分别与特殊的变量 True False 等价。
注意,这些变量名的大写
(a < b) 返回 True
>= 大于等于 - 返回 x 是否大于等于 y (a >= b) 返回 False
运算符 描述 示例
+ - 两个对象相加 a + b 输出结果 31
- - 得到负数或是一个数减去另一个数 a - b 输出结果 -11
* - 两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 210
/ - x 除以 y b / a 输出结果 2.1
% 取模 - 返回除法的余数 b % a 输出结果 1
// 取整除 - 返回除法的整数 b // a 输出结果 2 版权所有 , 联系作者 wx zsy1560707
<= 小于等于 - 返回 x 是否小于等于 y (a <= b) 返回 True

3 、赋值运算符

运算符 描述 示例
= 简单的赋值运算符 c = a + b a + b 的运算结果赋值为 c
+= 加法赋值运算符 c += a 等效于 c = c + a
-= 减法赋值运算符 c -= a 等效于 c = c - a
*= 乘法赋值运算符 c *= a 等效于 c = c * a
/= 除法赋值运算符 c /= a 等效于 c = c / a

4 、逻辑运算符

运算符 逻辑表达式 描述 示例
and x and y 布尔 " " - 如果 x False x and y 返回 False
or x or y 布尔 " " - 如果 x True ,它返回 True 的值。
not not x 布尔 " " - 如果 x True ,返回 False
如果 x False ,它返回 True
print(a == 1 and b == 2) 
print(a == 1 or b == 3) 
print(not(a == 2)) 
print(not '')

2、格式化输出
 

name = '张三'
age = 10
score = 99.87
print("我叫 %s 今年 %d 岁!" % ('小明', 9))
print("我叫 %s 今年 %d 岁! 每次考试都是 %f" % (name, age,score))
print('我叫 {} 今年 {} 岁! 每次考试都是 {}'.format(name, age,score))
print(f'我叫 {name} 今年 {age} 岁! 每次考试都是 {score}')

3、字符串有关方法

1 、count(str, beg= 0,end=len(string))
返回 str 在 string 里面出现的次数。如果 beg 、end 指定了,则返回指定范围
内 str 出现的次数
2、 find(str, beg=0 end=len(string))
返回 str 在 string 中第一次的索引。如果 beg 、 end 指定了,则返回指定范围
内 str 的索引。如果 str 不存在则返回-1
3 、len(string)
返回字符串长度的函数
__len__()
返回字符串长度的方法
4 、ower()
转换字符串中所有大写字符为小写
5、 upper()
转换字符串中的小写字母为大写
6 、lstrip()
截掉字符串左边的空格或指定字符
7 、max(str)
返回字符串 str 中最大字母的函数
8 、min(str)
返回字符串 str 中最小字母的函数
9 、rstrip()
删除字符串末尾(右边)的空格。
10、 split(sep=str, maxsplit=-1)
以 str 为分隔符截取字符串,maxsplit=-1 为次数不限,如果有指定次数,则仅截
取指定次数的字符串。默认以空白字符(空格、换行符)切割。
11、 strip([chars])
删除字符串头尾的空格,相当于同时执行 lstrip()和 rstrip()
#分割替换
str="python"
print(str.split('y'))
print(str.replace('y','k'))

4、列表的增删改查、排序

name_list = ['张三', 'joke', 3.14, 250, 5678]
#增
name_list.append(999)
#删
name_list.remove(3.14)
del name_list[3]
name_list.pop(-1)
#改
name_list[2]='you'
#查
print(name_list)
print("name_list[0]=", name_list[0])
#排序 
num=[2,1,34,4,7]
#升
num.sort()
#降
num.sort(reverse=True)

tup1 = (5, 3.14)
tup2 = ('hello', 'bye') 
# 创建一个新的元组 
tup3 = tup1 + tup2;

 5、列表元组互转

#元组 没有增删功能
tup=(1,3,2,4,5,6,78,98,4,5,66,7)
print(tup[2])
#元组、列表互转
st='abcdefg'
ls=['q','w','e','r','t']
print(''.join(ls))
print(list(st))
print(tuple(ls))

6、字典的增删改查

dict={"zhanghao":"zhangsan","mima":123456,"xuehao":23799,"zhiwu":"banzhang"}
#增
dict['xuexiao']='斯坦福'
print(dict)
#删
dict.pop('zhiwu') # 根据键来移除键值对
print(dict)
#改
dict['mima']=234567
print(dict)
#查
print(dict['xuehao'])

7、获取字典的键值

info_dict2 = {'name': 'libai', 'age': 45, 'class': 'A3',"职务":'员工'}
# 求长度 
print(len(info_dict2))

# 根据键 key 获取值 
print(info_dict2.get('name'))
# 返回键值元组的列表
print(info_dict2.items())
for k, v in info_dict2.items():
    print(k, ':', v)
# 获取所有键 
print(info_dict2.keys())
for key in info_dict2.keys():
    print(key)

8、If(选择)语句

age = 19
# 单分支
if age >= 18:
    print('已成年,可以上网')
# 双分支
age_a=14
if age_a >= 18:
    print('已成年,可以上网')
else:
    print('未成年不可上网')
#多分支
sc = 88
if sc >= 90 and sc <= 100:
    print('A')
elif sc >= 80 and sc <= 90:
    print('B')
elif sc >= 70 and sc <= 80:
    print('C')
elif sc >= 60 and sc <= 70:
    print('D')
else:
    print('不及格')

练习题:判断是否为三角形

# 判断是否为三角形,是什么三角形
a = int(input('请输入第一条边:'))
b = int(input('请输入第二条边:'))
c = int(input('请输入第三条边:'))

if a > 0 and b > 0 and c > 0:
    if a + b > c and a + c > b and b + c > a:
        print('能组成三角形',end=',')
        if a == b and b == c:
            print('且为等边三角形')
        elif a==b or a==c or b==c:
            print('且为等腰三角形')
        else:
            print('为普通三角形')
    else:
        print('不是三角形')
else:
    print('不是三角形')

9、while、for(循环)语句

count = 0
while count < 5:
    print('hello word')
    count += 1

求1~10的和

count = 1
sum = 0
while count <= 10:
    sum = sum + count
    count += 1
print(sum)

求1~10的偶数和

count = 1
sum = 0
while count <= 10:
    count += 1
    if count % 2 == 0:
        sum = sum + count

print(sum)

break:跳出循环

count = 1
sum = 0
while count <= 10:
    if count== 9:
        break
    sum = sum + count
    count += 1
print(sum)

continue:中止本次循环,继续下一次循环

count = 0
sum = 0
while count<10:
    count =count+ 1
    if count == 9:
        continue
    sum = sum + count
print(sum)

while...else.....:没有执行break则执行else

count = 0
sum = 0
while count<10:
    count =count+ 1
    if count == 9:
        break
    sum = sum + count
else:
    print('没有执行break')
print(sum)

打印正方形的*

i = 1
while i <= 4:
    j = 1
    while j <= 4:
        print('*', end=' ')
        j = j + 1
    i = i + 1
    print()


打印三角形的*

i = 1
while i <= 4:
    j = 1
    while j <= i:
        print('*', end=' ')
        j = j + 1
    i = i + 1
    print()

for循环

# 遍历字符串
str_a = 'qwertyui'
for i in str_a:
    print(i)
# 遍历列表
ls = ['篮球', '足球', '乒乓球']
for i in ls:
    print(i)

利用for循环存放下标加元组/列表元素

#元组
str_a = 'qwertyui'
for i,j in enumerate(str_a):
    print(i,j)

#列表
ls = ['篮球', '足球', '乒乓球']
for i,j in enumerate(ls):
    print(i,j)

range方法的for循环

print(list(range(100)))
for i in range(100):
    print(i)

sum=0
for i in range(101):
    sum+=1
print(sum)

break、conyinue方法的for循环

sum = 0
for i in range(101):
    if i == 100:
        break
    sum =sum+i
print(sum)

sum = 0
for i in range(101):
    if i == 99:
       continue
    sum =sum+i
print(sum)

for循环打印正方形、三角形“*”

#正方形
for i in range(4):
    for j in range(4):
        print('*',end=' ')
    print()

#三角形
for i in range(4):
    for j in range(i+1):
        print('*',end=' ')
    print()

完结!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值