Python实训第一天--基础知识
Python实训第⼀天--基础知识#注释
'''
单⾏注释:#
快捷键:ctrl + /
多⾏注释:三引号
'''
'''
关于变量:
变量的命名规范:
下划线命名:age_of_Tom
以下划线或英⽂字母开头命名
不能以数字开头
定义变量的三⼤特征:
id ⽤来表⽰变量的值在内存中唯⼀的⼀份内存地址;
变量的值⼀样,内存地址是不⼀样的
name1='Tom'
name2='Tom'
python优化机制(⼩计数池):在某个长度内,python把值相同的变量值同意存放在同⼀个内存地址中。
print(id(name1)
print(id(name2)
tyoe 变量值的类型
str1 = 'hello'
print(type(str1)
如何去除汽车异味
value 变量的值
str2 = 'hello'
print(str1 == str2))
'''
#变量的赋值
name = 'Tom'
print(name) #Tom
#变量名规范下划线命名
age_of_Tom = 18
#常量
'''
指的是不变的量。
常量本质上也是变量,在python不会有任何机制限制你不能修改常量。
⽽是程序员认为去限制⾃⼰,凡是遇见⼤写的变量都不能对其进⾏修改。
中国的传统文化有哪些命名规范:
变量名全⼤写。
SCHOOL = ''
'''
最大的变化是什么成语'''
⽤户与程序交互:
输⼊:
input()
输出:
print()
'''
#⽤户输⼊⽤户名
name = input('请输⼊名字')
#输出⽤户名
经典日文歌
print(name)
#在python3中,input输⼊的任何数据类型都是字符串
print(type(name))
#基本数据类型
'''
数字类型:
1.整型:  int
⼈的年龄、⾝份ID号...
2.浮点型:float
⼈的⾝⾼体重、薪资
'''
#int
age = int(18)
print(age)
print(type(age))
age2 = int(19)
print(age2)
print(type(age2))
#float
sa1 = 1.01
print(sal)
print(type(sal))
'''
字符串类型:
str
作⽤:
名字,性别,国籍,地址等描述信息
定义:
在单引号\双引号\三引号内,由⼀串字符组成。
药品底价查询'''
'''
优先掌握的操作:
1.按索引取值(正向取+反向取):只能读
正向取
str1 = 'hello Tom!'
print(str1[0]) #h
print(str1[8]) #m
反向取
print(str1[-2]) #m
2.切⽚(顾头不顾尾,步长)
str1 = 'hello tom!'
print(str[0:5]) #hello
步长
print(str1[0:10]) #hello tom!
print(str1[0:10:2]) #hloo!
3.长度len
print(len(str1)) #10
4.成员运算in和not in
print('h' in str1)  #True
print('h' not in str1)  #False
5.移除空⽩strip
会移除字符串中左右两边的空格
str1 = '    hello tom!'
print(str1)
str1='    hello tom!    '
print(str1.strip())
去除指定字符串
str2 = '!tom!'
print(str2.strip('!'))
6.切分split
str1 = 'hello tom!'
根据str1内的空格进⾏切分
切分出来的值会存放在[]列表⾥。
print(str1.split(''))  #['hello','tom!']
7.循环
对str1字符串进⾏遍历,打印每⼀个字符
for line in str1:
print(line)
'''
'''
字符串格式化输出
'''
#把100替换给了%s
#str1 = '尊敬的⽤户,你好!你本⽉的花费扣除%s元,还剩0元。' % 100
#把100替换给了%s,把50替换给了%d
#str1 = '尊敬的⽤户,你好!你本⽉的花费扣除%s元,还剩%d元。' % ('⼀百’,50)#print(str1)
#报错
#str1 = '尊敬的⽤户,你好!你本⽉的花费扣除%s元,还剩%d元。' % ('⼀百’,'50')#print(str1)
'''
字符串类型:
需要掌握的
'''
# 1、strip, lstrip, rstrip
str1 =  'hello wuyuefengprint'
print(str1)
#去掉两边空格
print(str1. strip())
#去掉左边空格
print(str1. lstrip())
#去掉右边空格
print (str1. rstrip())
# 2、lower, upper
str1 ='hello WuYueFeng'
#转换成⼩写
print (str1. lower())
#转换成⼤写
print (str1. upper())
# 3、startswi th, endswi th
str1 = 'hello WuYueFeng'
# #判断str1字符幵头是否等于hello
print(str1.startswith('hel1o'))  # True
#  #判断str1字符末尾是否等于MuYueFeng520怎么折爱心
dswith('WuYueFeng' )) # True
# 4、format格式化输出)的三种玩法
# str1 = 'my name is %s, my age %s!' % ('tom', 18)
#  print(strI)
# ⽅式⼀:根据位置顺序格式化
print('my name is {}, my age {}!'.format('tom', 18))
# ⽅式⼆:根据索引格式化
print('my name is {0}, my age {1} !'.format('tom', 18))
# ⽅式三:指名道姓地格式化
print('my name is {name}, my age {age} !'.format(age = 18, name = 'tank' )) # 6、join字符串拼接
#报错,只允许字符串拼接
# print(’’. join(['tom’, 18]))
#根据空格,把列表中的每⼀个字符串进⾏拼接
print(' '. join(['tom','18’,’from GZ']))
#根据_,把列表中的每⼀个字符串进⾏拼接
print('_'.join(['tom', '18', 'from GZ']))
# 7. replace: 字符串替换
str1 =  'my name  is  WangWei, my age 18!'
print(str1)
str2 = str1. replace ('WangWei','happy')
print(str2)
# 8、isdigit: 判断字符串是否是数字
choice = input('请选择功能[0,1, 2]:’)'
#判断⽤户输⼊的选择是否是数字
print (choice. isdigit())
'''
课后作业
'''
name = " aleX"
# 1) 移除 name 变量对应的值两边的空格,并输出处理结果
print(name. strip())
# 2) 判断 name 变量对应的值是否以 "al" 开头,并输出结果
print(name.startswith('a1'))
# 3) 判断 name 变量对应的值是否以 "X" 结尾,并输出结果
dswith('X' ))
# 4) 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
name2 = name. replace ('l','p')
print(name2)
# 5) 将 name 变量对应的值根据 “l” 分割,并输出结果。
print(name.split('l'))
# 6) 将 name 变量对应的值变⼤写,并输出结果
print(name.upper())          # 把所有字符中的⼩写字母转换成⼤写字母
# 7) 将 name 变量对应的值变⼩写,并输出结果
print(name.lower())          # 把所有字符中的⼤写字母转换成⼩写字母
# 8) 请输出 name 变量对应的值的第 2 个字符?
print(name[1])
# 9) 请输出 name 变量对应的值的前 3 个字符?
print(name[0:3])
# 10)请输出 name 变量对应的值的后 2 个字符?
print(name[-1:-3])
# 11)请输出 name 变量对应的值中 “e” 所在索引位置?
print(name.find('e'))
# 12)获取⼦序列,去掉最后⼀个字符。如: oldboy 则获取 oldbo。print(name.strip(name[-1]))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。