Python基础
1.格式化输出
第一种
'''
格式化输出
%s --字符串
%d --有符号的十进制整数 (-、+)
%f --浮点数
'''
age = 18
name = 'steven'
weight = 90.2
stu_id= 1
print('今年我的年龄是%d岁' % age)
print('我的名字是%s' % name)
print('我的体重是%f公斤' % weight) #默认保留6位小数
print('我的体重是%.1f公斤' % weight) #显示小数点后的位数
print('我的学号是%d' % stu_id)
print('我的学号是%03d' % stu_id) #表示输出的整数显示位数,不足以0补全,超出当前位数则原样输出
print('我的名字是%s,今年%d岁了' % (name, age))
print('我的名字是%s,明年%d岁了' % (name, age+1))
print('我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%06d' %(name,age,weight,stu_id))
%s;%f;%d都是代表字符串占位符
第二种
name = 'steven'
age = 18
weight = 90.2
print('我的名字是%s,今年%s岁了,体重%s公斤' %(name,age,weight))
'''
f {表达式} 格式化字符串
f-格式化字符串是python3.6中新增的格式化方法,该方法更简单易读。
'''
print(f'我的名字是{name},今年{age}岁了,体重{weight}公斤')
print(f'我的名字是{name},明年{age+1}岁了,体重{weight}公斤')
注意:这用格式化输出仅支持Python3.6以后的版本
2.转义字符串
print('hello')
print('world')
print('hello\npython')
print('\thello python')
\n:换行;\t:制表符 一个tab键的距离
3.结束符
print('hello', end="\n")
print('world', end="\t")
print('hello', end="...")
print('python')
print('输出的内容', end='\n'),在python中,print(),默认自带end=“\n”这个换行结束符,所以导致每两个print直接会换行展示,用户可以按需求更改结束符
4.基本运算符
算数运算符
假设变量:a = 10, b = 20
| 运算符 | 描述 | 实例 |
|---|---|---|
| + | 加 - 两个对象相加 | a + b 输出结果 30 |
| - | 减 - 两个对象相减 | a + b 输出结果 -10 |
| * | 乘 - 两个对象相乘 | a * b 输出结果 200 |
| / | 除 - 两个对象相除 | b / a 输出结果 2 |
| % | 取模 - 返回除法的余数 | b % a 输出结果 0 |
| ** | 幂 - 返回x的y次幂 | a ** b 输出结果 |
| // | 取整除 - 返回商的整数部分 | 9 // 2 输出结果 4 |
比较运算符
假设变量:a = 10, b = 20
| 运算符 | 描述 | 实例 |
|---|---|---|
| == | 等于 - 比较对象是否相等 | (a == b) 返回False |
| != | 不等于 - 比较两个对象是否不相等 | (a != b) 返回True |
| <> | 不等于 - 比较两个对象是否不相等 | (a <> b) 返回True |
| > | 大于 - 返回x是否大于y | (a > b) 返回False |
| < | 小于 - 返回x是否小于y | (a < b) 返回True |
| >= | 大于等于 - 返回x是否大于等于y | (a >= b) 返回False |
| <= | 小于等于 - 返回x是否小于等于y | (a <= b) 返回True |
赋值运算符
假设变量:a = 10, b = 20
| 运算符 | 描述 | 实例 |
|---|---|---|
| = | 简单的赋值运算符 | c = a + b |
| += | 加法赋值运算符 | c += a - c= c + a |
| -= | 减法赋值运算符 | c -= a - c= c - a |
| *= | 乘法赋值运算符 | c *= a - c= c * a |
| /= | 除法赋值运算符 | c /= a - c= c / a |
| //= | 整除赋值运算符 | c //= a - c= c // a |
| %= | 取余赋值运算符 | c %= a - c= c % a |
| **= | 幂赋值运算符 | c **= a |
多变量赋值
num1, float1, str1 = 10, 0.4, 'hello world'
print(num1)
print(float1)
print(str1)
多变量赋相同值
a = b = 10
print(a)
print(b)
逻辑运算符
| 运算符 | 描述 | 实例 |
|---|---|---|
| and | 布尔“与” -- 如果x为false,x and y 返回false,否则它返回y的计算值 | (a and b)返回true |
| or | 布尔“或” -- 如果x为true,它返回true,否则它返回y的计算值 | (a or b)返回true |
| not | 布尔“非” -- 如果x为false,如果x为True,返回False;如果x为False,它返回True | not(a and b) 返回false |
针对逻辑运算符的进一步研究:
1.如果没有()的情况下,not优先级高于and,and优先级高于or,即优先级关系为()>not>and>or,同一优先级从左往右计算。
练习1:
3 > 4 or 4 < 3 and 1==1
1 < 2 and 3 < 4 or 1>2
2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
成员运算
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
| 运算符 | 描述 | 实例 |
|---|---|---|
| in | 如果在指定的序列中找到值,返回True,否则返货False | x 在 y序列中,如果x在y序列中,返回True |
| not in | 如果在指定的序列中没有找到值,返回True,否则返货False | x 不在 y序列中,如果x不在y序列中,返回True |
Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
| 运算符 | 描述 |
|---|---|
| ** | 指数 (最高优先级) |
| ~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
| * / % // | 乘,除,取模和取整除 |
| + - | 加法减法 |
| >> << | 右移,左移运算符 |
| & | 位 'AND' |
| ^ | | 位运算符 |
| <= < > >= | 比较运算符 |
| <> == != | 等于运算符 |
| = %= /= //= -= += *= **= | 赋值运算符 |
| is is not | 身份运算符 |
| in not in | 成员运算符 |
| not and or | 逻辑运算符 |
5.条件
if条件语句
语法:
a = 18
if a >= 18:
print('条件已成立1')
print('条件已成立2')
if..else条件语句
语法:
if a >= 18:
print('条件已成立3')
print('条件已成立4')
else:
print('条件不成立')
多条件语句
语法:
if a > 18:
print('条件已成立5')
elif a == 18:
print("条件已成立6")
else:
print('条件不成立1')
条件语句拓展
age = int(input('请输入您的年龄:'))
if age < 18:
print(f'您输入的年龄是{age},童工')
elif (age >= 18)and(age <= 60):
print(f'您输入的年龄是{age},合法工龄')
else:
print(f'您输入的年龄是{age},可以退休')
age = int(input('请输入您的年龄:'))
if age < 18:
print(f'您输入的年龄是{age},童工')
elif 18 <= age <= 60:
print(f'您输入的年龄是{age},合法工龄')
else:
print(f'您输入的年龄是{age},可以退休')
if嵌套
money = input('请输入有钱没钱:')
if money == 'yes':
print('你可以做公交车')
seat = input('请输入有无座位:')
if seat == 'yes':
print('你可以坐下')
else:
print('你得站着')
else:
print('你不能做公交车')
三目运算符
三目运算符也叫三元运算符或三元表达式
a = 3
b = 5
if a > b:
c = a -b
else:
c = b - a
print(c)
a = 3
b = 5
c = a - b if a < b else b - a
print(c)
6.循环
while
循环的作用:让代码更高效的重复执行
while语法
while 条件:
条件成立重复执行代码1
条件成立重复执行代码2
……
break和continue、pass
break:终止循环
i = 0
while i < 5:
if i == 3:
print(f'我吃{i},我饱了,不吃了')
break
i += 1
continue:退出当前一次循环而执行下一次循环
a = 0
while a < 5:
a += 1
if a == 2:
print(f'第{a}个苹果有虫子')
continue
print(f'我吃了{a}个苹果')
pass时空语句,是为了保持程序结构的完整性
pass不做任何事情
a = 0
while a < 5:
a += 1
if a == 2:
pass
continue
print(f'我吃了{a}个苹果')
while嵌套
i = 0
while i < 3:
j = 0
while j < 3:
print('媳妇,我错了')
j += 1
print('刷晚饭的碗')
i += 1
print(f'第{i}天')
for
用户按照顺序循环可迭代对象的内容
for 临时变量 in 序列:
重复执行的代码1
重复执行的代码2
……
str1 = 'sdaesfa'
for i in str1:
print(i)
while...else 在循环条件为False时执行else语句块
i = 1
while i <= 5:
if i == 6:
i += 1
continue
i += 1
print('媳妇我错了')
else:
print('原谅你了')
for...else 在循环条件为False时执行else语句块
7.字符串操作
下标
下标又叫索引,就是编号。下标从0开始,顺序分配一个编号。使用这个编号能够精准找到某个字符数据
str1 = 'asdafgalfa'
print(str1[3])
切片
切片是指对操作对象截取其中一部分的操作。
name = '0123456789'
print(name[2:5:1]) # 234
print(name[2:5:2]) # 24
print(name[:5]) # 01234
print(name[2:]) # 23456789
print(name[:]) # 0123456789
print(name[::-1]) # 9876543210
print(name[-2::]) # 89
print(name[-4:-1]) # 678
print(name[-4:-1:1]) # 678
print(name[-4:-1:-1]) # 不能选取数据:-1步长:从右往左选取
序列[开始位置下标:结束位置下标:步长]
不包含结束位置的下标对应的数据,正负整数均可
步长是选取间隔,正负整数均可,默认是1
查找
find()
检测某个子串是否包含在这个字符串中,如果存在返回这个子串的位置下标,否则返回-1
字符串序列.find(字串,开始位置下标,结束位置下标)
mystr = "hello world and itcast and itheima and python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) #23
index()
mystr = "hello world and itcast and itheima and python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) #23
如果index查找子串不存在,报错
count()
查找子串出现的次数
mystr = "hello world and itcast and itheima and python"
print(mystr.count('and')) # 3
print(mystr.count('and', 15, 30)) # 1
print(mystr.count('ands')) # 0
rfind()
mystr = "hello world and itcast and itheima and python"
print(mystr.rfind('and')) # 35
和find()功能相同,但查找方向为右侧开始
rindex()
mystr = "hello world and itcast and itheima and python"
print(mystr.rindex('and')) # 35
和index()功能相同,但查找方向为右侧开始
修改
replace()
替换 说明replace函数有返回值,返回值是修改后的字符串
字符串序列.replace(旧子串,新子串,替换次数)
mystr = "hello world and itcast and itheima and python"
print(mystr.replace('and', '和')) # hello world 和 itcast 和 itheima 和 python
print(mystr.replace('and', '和', 1)) # hello world 和 itcast and itheima and python
split()
分割,返回一个列表,丢失分割字符
字符串序列.split(分割字符,num)
mystr = "hello world and itcast and itheima and python"
list1 = mystr.split('and')
print(list1) # ['hello world ', ' itcast ', ' itheima ', ' python']
list1 = mystr.split('and', 2)
print(list1) # ['hello world ', ' itcast ', ' itheima and python']
join()
合并列表里的字符串数据为一个大字符串
list1 = ['chuan', 'zhi', 'bo', 'ke']
new_str = '...'.join(list1)
print(new_str) #chuan...zhi...bo...ke
修改大小写
capitalize()
将字符串第一个字符转换成大写
mystr = "hello world and itcast and itheima and python"
print(mystr.capitalize())
title()
将字符串每个单词首字母转换成大写
mystr = "hello world and itcast and itheima and python"
print(mystr.title())
lower()
将字符串中大写转小写
mystr1 = "Hello World And Itcast And Itheima And Python"
print(mystr1.lower())
upper()
将字符串中小写转大写
mystr1 = "Hello World And Itcast And Itheima And Python"
print(mystr.upper())
删除
lstrip()
删除字符串左侧空白字符
mystr2 = " hello world "
print(mystr2.lstrip()) # hello world
rstrip()
删除字符串右侧空白字符
mystr2 = " hello world "
print(mystr2.rstrip())
strip()
删除字符串两侧空白字符
mystr2 = " hello world "
print(mystr2.strip())
字符串对齐
ljust(长度,填充字符) 左对齐
mystr2 = "hello"
print(mystr2.ljust(10, '.'))
rjust(长度,填充字符) 又对齐
mystr2 = "hello"
print(mystr2.rjust(10, '.'))
center(长度,填充字符) 居中
mystr2 = "hello"
print(mystr2.center(10, '.'))
判断字符串开头或结尾
startswith()
检查字符串是否是以指定子串开头,是则返回True,否则返回False
字符串序列.startswith(子串, 开始位置下标, 结束位置下标)
mystr = "hello world and itcast and itheima and python"
print(mystr.startswith('hello', 0, 5))
endswith()
检查字符串是否是以指定子串结尾,是则返回True,否则返回False
字符串序列.endswith(子串, 开始位置下标, 结束位置下标)
mystr = "hello world and itcast and itheima and python"
print(mystr.endswith('python'))
字符串判断
isalpha()
如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False
isdigit()
如果字符串只包含数字则返回True,否则返回False
mystr1 = '123aaa'
mystr2 = 'aaa'
mystr3 = '123'
print(mystr1.isalpha()) #False
print(mystr2.isalpha()) #True
print(mystr3.isdigit()) #True
浙公网安备 33010602011771号