Python之路,day2-Python基础1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之路,day2-Python基础1相关的知识,希望对你有一定的参考价值。

python2

range(20)

for i in range(10):

    print(i)

range(1,10)  -----》从1开始到9

 

 

else: #如果for循环正常结束,  就执行else语句,(break为不正常结束)(注:此处的的else为与for循环的for同级别)

 

break #跳出整个当前循环,只跳一层

continue#跳出当次循环,继续下一次循环

 

while循环

1 count = 0
2 while Ture:
3     if count = 10000000
4     print(dsdsfdsfsf)
5     break
6 count+=1
count = 0
while True:
    if count == 10000000
    print(dsdsfdsfsf)
    break
count+=1
count = 0
while count < 100:
    print(dsdsfdsfsf)
    count+=1

 

 

 

 

 

 

变量:  用来记录状态

变量值的变化即状态的变化, 程序运行的本质就是来处理一系列状态的变化

 

数据类型(五大基础数据类型)

1.数字

 整型int

base  用来把字符串转换为10进制的整数

int(‘0b1010‘,base=2)

age=10---->int(10) ------>_init_

长整型

布尔bool

ture 和 false

1和0

浮点数float

复数

2.字符串

msg=‘hello world‘---->str

print (msg[1])字符串字母特定位置

print(msg.capitalize())#首字母大写

print(msg.center(20,‘*’)) 定义居中格数,以及空格处符号(默认为空格)

print(msg.count(‘l’,4,7))  # -1(代表从右边数第一个,相当于10)

print(msg.endswitch(‘l‘) ) #结尾的字母是否为‘l’

msg1=‘a\tb‘

print(msg1.expandtabs(10))#指定tab的空格数,默认4个

print(msg.find(‘d‘))#返回元素在字符串的位置,如果同一元素有多个找到第一个就结束,后面木再找

print(msg.find(‘d‘,0,4))

 

format

print(‘{0}{1}‘.format(‘name‘,‘age‘))

print(‘{name}‘.format(name=‘alex‘))#调用后面的定义变量的值

print(‘{}{}‘.format(‘name‘,‘age‘))#一一对应

 

 

 

字符串判断

msg3=‘a123’

print(msg3.isalnum())#字幕和数字组成的字符串

print(msg3.isalpha())#全是字幕返回true

msg4=‘10’

msg5=‘10.2‘

print(msg5.isdecimal)#浮点数

msg6=‘10‘

print(msg.isdigit())判断是否为整型

msg7=‘10.3‘

print((msg7).isnumeric)

msg8=‘while‘

print(msg.isidentifier)

 

msg9=‘aaa’

print(msg.islower) #小写字母

msg10=‘   aaa’

print(msg10.isspace)#包含空格

 

msg11=‘Hello’

print(msg11.istitle)#单词首字母大写为title

msg12=‘Hello’

print(msg12.isupper)#全是大写

msg13=‘abc’

print(msg13.ljust(10,‘*‘))#左对齐

 

print(msg13.rjust(10,‘*‘))# 右对齐

print(msg13.center(10,‘*‘))# 居中

print(msg13.upper(10,‘*‘))# 将小写转化为大写

 

字符串常用的

 

# str()
# msg=‘hello world‘
# print(msg.capitalize())
#
#
# print(‘{0} {1}‘.format(‘name‘,‘age‘))
#
# print(‘{name}‘.format(name=‘alex‘))
# print(‘{}{}‘.format(‘name‘,‘age‘))
#
# print(msg.endswith( ‘l‘))


# msg13=‘abc‘
#
# print(msg13.ljust(10,‘*‘))
#
# print(msg13.rjust(10,‘*‘))
#
# print(msg13.center(10,‘*‘))
#
# print(msg13.upper(10,‘*‘))
#
#
# #==================================
#
# msg14=‘hello‘
# print(msg14.find(‘w‘))
# print(msg14.index(‘w‘))
#
# msg15=‘ sdff ‘
# print(msg15.strip())#去掉首尾的空格
# print(msg15.lstrip())#去掉左边的空格
# print(msg15.rstrip())#去掉右边的空格
#
# #制造翻译表
# msg16=‘my name is abc‘
# table=str.maketrans(‘abc‘,‘ale‘)
# print(msg16.translate(table))
# #
# #zfill
# msg17=‘abc‘
# print(msg17.zfill(20))#右对齐,不够的用‘0’补
# print(msg17.ljust(20,‘0‘))
# print(msg17.rjust(20,‘0‘))

#字符串常用的操作
#移除空白
# msg19=‘123234423423‘
# print(msg19.strip(‘1‘))
# #分割 ----取范围
# msg20=‘nihao 123‘#
# print(msg20[0:3])
# print(msg20[2:7:2]) #隔两个取一次
# #长度
# len(msg20)#字符串长度
# len(msg20)/2
#
# round(len(msg20)/2)
# #索引 下表
# #切片 分割
#
#
# #==================
# #运算符
# #1.算数运算符 +-*/
# #‘//‘地板除,只取整数部分
# #2.比较运算符
# # == !=
# 赋值运算符
# #age+=1<==> age=age+1
#
# 位运算符
#
# 逻辑运算
#
# 成员运算
#
# 身份运算
#
# count = 0
# while True:
# if count == 10000000
# print(‘dsdsfdsfsf‘)
# break
# count+=1

# count = 0
# while count < 100:
# print(‘dsdsfdsfsf‘)
# count+=1


# age = 20
# count = 0
# while count < 3:
# myage = input(‘myage:‘)
# if myage.isdigit():
# myage = int(myage)
# else:
# continue
# if myage == age:
# print(‘yes‘)
# break
# elif myage < age:
# print(‘猜大点‘)
# else:
# print(‘猜小点‘)
# count+=1

#列表

names = [‘a‘,‘b‘,‘c‘,‘d‘]

#zeng
# names.append(‘e‘)
# print(names)
#
# names.insert(2,‘f‘)
# names.insert(1,‘g‘)
# print(names)
#
# #shan
# names.remove(‘f‘)
# print(names)
#
# del names[1]
# print(names)
#
# names.pop(3)
# print(names)
#
#
# #gai
# names[2] = ‘k‘
# print(names)
#
# #cha
# print(names[-2])
# print(names[0::2])
# print(names[-3:])
#
# print( names.index(‘s‘) )
#
first_index = names.index(‘a‘)
second_index = names[first_index + 1:].index(‘b‘)
print(‘second ‘,second_index+first_index+1)

print(‘count‘,names.count(‘b‘))
n2 = [‘5‘]
names.extend(n2)

names.reverse()
names.sort()

print(names)






n3 = names.copy()
print(n3)

n4= names

names.pop()


#打印下表和值
for i,ele in enumerate(names):
print(i,ele)

3.列表

练习题

购物车程序

 your salary>>:5000

-------shop list------

1.iphone 5800

2.macbook 12800

3.coffee 30

4.bike 2000

------end-----

>>:1

钱不够

>>:3

added [coffee]into your shopping list,your current balance is 4970

>>:

>>:quit

your balance is 4000

已购买商品

1.coffee 30

 

 

 

 

 

 













































































































































































以上是关于Python之路,day2-Python基础1的主要内容,如果未能解决你的问题,请参考以下文章

python学习之路

Day2-python基础2

day2-Python基础2 变量,判断

Day2-Python基础2---浅copy深copy的差别

DAY2-python基础

Day2-Python基础2---字典操作