python学习第一


#python学习day1
#一、变量
#变量命名规范:
#驼峰命名法:AgeOfPlane
#下划线命名(推荐):age_of_plane
#变量格式同C/C++
#注意:变量不以中文命名;变量不宜过长;变量因表达其义

#定义变量的三大特征
#id(变量 值一样,内存地址不一样)

name1='lxh1'
name2='lxh1'
#python优化机制(小计数池):在某个长度内,把变量值相同的变量存放在同一个内存之中
print(id(name1))
print(id(name2)

#type:判断变量的类型
print(type(name1))

#value:判断值是否相同
str1='123'
str2=str1
print(str1==str2)

 



#二、常量
#常量指的是不变的量
#常量本质上也是变量,在python中不会有任何机制限制你不能修改常量,而是python程序员限制自己,凡是遇见大写的变量都不能对其修改
#命名规范:变量名全大写
SCHOOL='合肥学院'

#用户与程序交互
#输入:input()
#输出:print()

name=input('请输入名字')
print(name)

#在python3中input输入的任何数据类型都是字符串
print(type(name))

 

 

#三、注释
#单行注释:#
#多行注释:''' '''或者""" """


'''
基本数据类型:
1.整型(年龄等)
2.浮点型(身高体重等)
'''


#int
age=int(18)
print(age)
print(type(age))

age2=19
print(age2)
print(type(age2))

#float
weight=60.5
print(weight)
print(type(weight))


 


#字符串类型:str

#单引号
str1='天王盖地虎'
print(str1)
print(type(str1))

#双引号
str2="小鸡炖蘑菇"
print(str2)
print(type(str2))

#三引号
str3='''
安徽省
合肥市
合肥学院
'''
print(str3)
print(type(str3))

 



#1、按索引取值(正向取+反向取):只能取
#正向取
str1='hello dear!'
print(str1[0]) #0
print(str1[9]) #r

#反向取
print(str1[-2]) #r

#2、切片(顾头不顾尾,步长)
str1='hello dear!'
#0—(s-1)
print(str1[0:5]) #hell0

#步长
print(str1[0:11]) #hello dear!
print(str1[0:11:2]) #每隔两位取一个hlotn!

#3、长度len
print(len(str1)) #11

#4、成员运算in和not in
print('h'in str1) #Ture
print('h'not in str1) #False

#移除空白strip
#会移除字符串左右两边的空格
str1=' hello dear!'
print(str1)
str1=' hello dear! '
print(str1)
print(str1.strip())

#去除指定字符串
str2='!dear!'
print(str2.strip('!'))

#6、切分split
str1='hello dear!'
#根据str1内的空格进行切分
#切分出来的值会存放在[]列表中
print(str1.split(' ')) #['hello','dear!']

#7、循环
#对str1字符串进行遍历,打印每一个字符
for line in str1:
print(line)

 



#1、格式化输出
#通过某种占位符用于替换字符串中某个位置的字符
str1 = '尊敬的用户,您好!你本月话费扣除%s元,剩余%d元 ' %('一百',50 )
print(str1)

#strip、lstrip、rstrip
str1 = ' hello dear '
print(str1)
#去除两边的空格
print(str1.strip())
#去除左边的空格
str1 = ' hello dear '
print(str1.lstrip())
#去除右边的空格
str1 = ' hello DEAR '
print(str1.rstrip())

#2、lower,upper
str1 = 'hello dear'
#转换成小写
print(str1.lower())
#转换成大写
print(str1.upper())

#3、startswitch,endswitch
str1 = 'hello dear'
#判断str1字符开头是否等于hello
print(str1.startswith('hello')) #Ture
#判断str1字符末尾是否等于dear
print(str1.endswith('dear')) #Ture

#4、format(格式化输出)的三种方式
#方式一:根据位置顺序格式化
print('my name is {},my age is{}!'.format('plane', 18))

#方式二:根据索引格式化
print('my name is {0},my age is{1}!'.format('plane', 18))

#方式三:指名道姓的格式化
print('my name is {name},my age is{age}!'.format(age=18, name='plane'))

#5、切分split
str1='hello dear!'
#根据str1内的空格进行切分
#切分出来的值会存放在[]列表中
print(str1.split(' ')) #['hello','dear!']

#6、join字符串拼接
#报错,只允许字符串拼接
#print(' '.join(['tank',18]))

#根据空格,把列表中的每一个字符串进行拼接
print(' '.join(['plane', '18', 'from anhui']))

#根据_,把列表中的每一个字符串进行拼接
print('_'.join(['plane', '18', 'from anhui']))

#replace:字符串替换
str1 = 'my name is liuxinhang,my age 73!'
print(str1)
str2 = str1.replace('liuxinhang','plane')
print(str2)

#8、isdigit:判断字符串是否是数字
choice = input('请选择功能[0,1,2]:')
#判断用户输入的选择是否是数字
print(choice.isdigit())

 

 
 
  1 print('哈哈哈')
  2 name='plane'
  3 print(name)
  4 
  5 #变量命名规范:
  6 #驼峰命名法:AgeOfPlane
  7 #下划线命名(推荐):age_of_plane
  8 #变量格式同C/C++
  9 #注意:变量不以中文命名;变量不宜过长;变量因表达其义
 10 
 11 #定义变量的三大特征
 12 #id(变量 值一样,内存地址不一样)
 13 
 14 name1='lxh1'
 15 name2='lxh1'
 16 #python优化机制(小计数池):在某个长度内,把变量值相同的变量存放在同一个内存之中
 17 print(id(name1))
 18 print(id(name2))
 19 
 20 #type:判断变量的类型
 21 print(type(name1))
 22 
 23 #value:判断值是否相同
 24 str1='123'
 25 str2=str1
 26 print(str1==str2)
 27 
 28 
 29 #常量
 30 #常量指的是不变的量
 31 #常量本质上也是变量,在python中不会有任何机制限制你不能修改常量,而是python程序员限制自己,凡是遇见大写的变量都不能对其修改
 32 #命名规范:变量名全大写
 33 SCHOOL='合肥学院'
 34 
 35 #用户与程序交互
 36 #输入:input()
 37 #输出:print()
 38 
 39 name=input('请输入名字')
 40 print(name)
 41 
 42 #在python3中input输入的任何数据类型都是字符串
 43 print(type(name))
 44 
 45 #注释
 46 #单行注释:#
 47 #多行注释:''' '''或者"""    """
 48 
 49 
 50 '''
 51 基本数据类型:
 52 1.整型(年龄等)
 53 2.浮点型(身高体重等)
 54 '''
 55 
 56 
 57 #int
 58 age=int(18)
 59 print(age)
 60 print(type(age))
 61 
 62 age2=19
 63 print(age2)
 64 print(type(age2))
 65 
 66 #float
 67 weight=60.5
 68 print(weight)
 69 print(type(weight))
 70 
 71 
 72 
 73 #字符串类型:str
 74 
 75 #单引号
 76 str1='天王盖地虎'
 77 print(str1)
 78 print(type(str1))
 79 
 80 #双引号
 81 str2="小鸡炖蘑菇"
 82 print(str2)
 83 print(type(str2))
 84 
 85 #三引号
 86 str3='''
 87 安徽省
 88 合肥市
 89 合肥学院
 90 '''
 91 print(str3)
 92 print(type(str3))
 93 
 94 #1、按索引取值(正向取+反向取):只能取
 95 #正向取
 96 str1='hello dear!'
 97 print(str1[0])  #0
 98 print(str1[9])  #r
 99 
100 #反向取
101 print(str1[-2]) #r
102 
103 #2、切片(顾头不顾尾,步长)
104 str1='hello dear!'
105 #0—(s-1)
106 print(str1[0:5])  #hell0
107 
108 #步长
109 print(str1[0:11])   #hello dear!
110 print(str1[0:11:2])   #每隔两位取一个hlotn!
111 
112 #3、长度len
113 print(len(str1))   #11
114 
115 #4、成员运算in和not in
116 print('h'in str1)   #Ture
117 print('h'not in str1)  #False
118 
119 #移除空白strip
120 #会移除字符串左右两边的空格
121 str1='     hello dear!'
122 print(str1)
123 str1='     hello dear!    '
124 print(str1)
125 print(str1.strip())
126 
127 #去除指定字符串
128 str2='!dear!'
129 print(str2.strip('!'))
130 
131 #6、切分split
132 str1='hello dear!'
133 #根据str1内的空格进行切分
134 #切分出来的值会存放在[]列表中
135 print(str1.split(' '))   #['hello','dear!']
136 
137 #7、循环
138 #对str1字符串进行遍历,打印每一个字符
139 for line in str1:
140     print(line)
141 
142 
143 #1、格式化输出
144 #通过某种占位符用于替换字符串中某个位置的字符
145 str1 = '尊敬的用户,您好!你本月话费扣除%s元,剩余%d元 ' %('一百',50 )
146 print(str1)
147 
148 #strip、lstrip、rstrip
149 str1 = '  hello dear  '
150 print(str1)
151 #去除两边的空格
152 print(str1.strip())
153 #去除左边的空格
154 str1 = '  hello dear  '
155 print(str1.lstrip())
156 #去除右边的空格
157 str1 = '  hello DEAR  '
158 print(str1.rstrip())
159 
160 #2、lower,upper
161 str1 = 'hello dear'
162 #转换成小写
163 print(str1.lower())
164 #转换成大写
165 print(str1.upper())
166 
167 #3、startswitch,endswitch
168 str1 = 'hello dear'
169 #判断str1字符开头是否等于hello
170 print(str1.startswith('hello'))    #Ture
171 #判断str1字符末尾是否等于dear
172 print(str1.endswith('dear'))    #Ture
173 
174 #4、format(格式化输出)的三种方式
175 #方式一:根据位置顺序格式化
176 print('my name is {},my age is{}!'.format('plane', 18))
177 
178 #方式二:根据索引格式化
179 print('my name is {0},my age is{1}!'.format('plane', 18))
180 
181 #方式三:指名道姓的格式化
182 print('my name is {name},my age is{age}!'.format(age=18, name='plane'))
183 
184 #5、切分
185 
186 
187 
188 #6、join字符串拼接
189 #报错,只允许字符串拼接
190 #print(' '.join(['tank',18]))
191 
192 #根据空格,把列表中的每一个字符串进行拼接
193 print(' '.join(['plane', '18', 'from anhui']))
194 
195 #根据_,把列表中的每一个字符串进行拼接
196 print('_'.join(['plane', '18', 'from anhui']))
197 
198 #replace:字符串替换
199 str1 = 'my name is liuxinhang,my age 73!'
200 print(str1)
201 str2 = str1.replace('liuxinhang','plane')
202 print(str2)
203 
204 #8、isdigit:判断字符串是否是数字
205 choice = input('请选择功能[0,1,2]:')
206 #判断用户输入的选择是否是数字
207 print(choice.isdigit())

 

转载于:https://www.cnblogs.com/lxhdeboke/p/11079246.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值