Python入门(一)

一、Python安装

二、Python语法

2.1 print()函数

#print输出函数
# 输出字符

print(201)
print(2,3)

## 可以输出字符串
print('helloworld')
print("helloworld")

# 含运算符
print(3+2)

# 将数据输出到文件中
fp=open('F:/text.txt','a+') #a+如果文件不存在就创建,存在就在文件内容后追加
print('helloworld',file=fp)
fp.close()

#不进行换行输出(输出内容在一行)
print('hello','world','Python')

总结:open输出到文件当中,使用‘,’会输出到一行

2.2 转义符

#转义字符
print('hello\nworld') #转义功能的首字母 n--> newline首字符表示换行
print('hello\tworld')  #每4个字符 进行空格4个字符
#hello	world   #中间空3个
print('helloooo\tworld')
#helloooo	world   中间空4个

print('hello\rworld') #world将hello进行覆盖
#world
print('hello\bworld') #\b退回一个格,将o退没

#转义符
print('http:\\\\www.baidu.com')
#http:\\www.baidu.com
print('老实说:\'大家好\'')
#老实说:'大家好'

#原字符,不希望字符串中的转义符起作用,就使用原字符,就是在字符串之前加上'r'或者'R'
print(r'hello\nworld')
#hello\nworld

#注意事项,最后一个字符不能使用反斜杠
#print(r'hello\nworld\')
print(r'hello\nworld\\')

2.3 for循环

在这里插入图片描述

# yifan  Python
for item in 'Python' :
    print(item) #遍历
#P
#y
#t
#h
#o
#n

#range() 产生一个整数序列,--》也是一 个可迭代对象
for i in range(10):
    print(i)

#如果在循环体中不需要使用到自定义变量,可将自定义变量写为 “_”
for _ in range(5):
    print('人生苦短,我用python')

print('使用for循环+遍历求偶数和')
sum=0
for item in range(1,101):
    if item %2 == 0:
        sum += item
    #print('1-101之间的偶数和,sum;') #遍历输出if的结果
    #1-101之间的偶数和 0
    #1-101之间的偶数和 2

print('1-101之间的偶数和',sum)
#1-101之间的偶数和 2550
#求100-1000的水仙花数
for item in range(100,1000):
    ge=item%10  #个位
    shi=item//10%10  #十位
    bai=item//100   #百位
#    print(bai,shi,ge)
    #判断
    if ge**3 + shi**3 + bai**3==item:
        print(item)

2.4 Break语句

break的作用:跳出循环
在这里插入图片描述

''''从键盘录入密码,最多录入3次,如果正确就结束循环'''

for item in range(3):
    pwd=input('请输入密码:')
    if pwd=='8888':
        print('密码正确')
        break  #直接退出程序
    else:
		print('密码不正确')

2.5 Continue跳出循环

在这里插入图片描述

'''要求输出1-50之间所有5的倍数,使用continue实现'''

for item in range(1,51):
    if item%5==0:
        print(item)

print('--------------------使用Continue实现-------------------')

for item in range(1,51):
    if item%5!=0:
        continue
    print(item)

三、判断语句

3.1 else 语句

#else语句
for item in range(3):
    pwd=input('请输出密码:')
    if pwd=='8888':
        print('密码正确')
        break
    else:			#if  else这两个连接
        print('密码不正确')
else:				#for  else 
    print('对不起,三次密码均输入错误')

3.2 嵌套循环

在这里插入图片描述

#嵌套循环
print('输出一个三行四列的矩形')
for i in range(1,4):  #行表,执行三次,一次一行i=3;
    for j in range(1,5):  #j=4
        print('*',end='\t')  #不换行输出
    print()

print('----------------------9*9乘法表--------------------------')
for i in range(1,10): #行
    for j in range(1,i+1):  #列
        print(i,'*',j,'=',i*j,end='\t')
    print()

3.3 二重循环中的break和continue

在这里插入图片描述

#流程控制语句break和continue在二重循环中的使用

for i in range(5):
    for j in range(1,11):
        if j%2==0:
            #break
        ##结果1 1 1 1 1 ;没执行一次i,J到2就跳出整个循环
            continue
        ##每执行一次i,continue遇到能被2整除的数,就跳出J的本次循环,继续执行J+1,直到j=10退出循环,
        print(j,end='\t')
    print()

在这里插入图片描述

数据结构

四、列表

4.1 列表(数组)

在这里插入图片描述
在这里插入图片描述

#创建列表的第一种方式使用[]

lst=['hello','world',98]
print(id(lst),type(lst),lst)

#创建列表的第二种方式,使用内置函数list()
lst2=list(['hello','world',98])
print(id(lst2),type(lst2),lst2)

4.2 列表的特点

在这里插入图片描述

#创建列表的第一种方式使用[]

lst=['hello','world',98]
print(id(lst),type(lst),lst)
print(lst[0],id(lst[0]),lst[1],id(lst[1]),lst[2],id(lst[2]))


#2771898397888 <class 'list'> ['hello', 'world', 98]
#hello 2771898317424 world 2771898317616 98 2771897027856
#2771901210944 <class 'list'> ['hello', 'world', 98]

#创建列表的第二种方式,使用内置函数list()
lst2=list(['hello','world',98])
print(id(lst2),type(lst2),lst2)

4.3 列表的查询

在这里插入图片描述

# yifan  Python

lst=['hello','world',98,'hello']
print(lst.index('hello'))   #list.index

#print(lst.index('Python'))  #ValueError: 'Python' is not in list
#print(lst.index('hello',1,3)) #ValueError: 'hello' is not in list 1-2之间

print(lst.index('hello',1,4))  #3

获取单个元素

#获取列表中的某个元素
lst=['hello','world',98,'hello','world',234]
#获取索引为2的元素
print(lst[2])
#获取索引为-3的元素
print(lst[-3])

4.4 列表元素的查询

在这里插入图片描述

# yifan  Python


#获取列表中的多个元素
lst=[10,20,30,40,50,60,70,80]

#start=1,stop=6,step1
#print(lst[1:6:1]) #[20, 30, 40, 50, 60]

print('原列表',id(lst))
lst2=lst[1:6:1]
print('切的片段',id(lst2))  #内存地址;原片段的拷贝

print(lst[1:6])     #默认step为1
print(lst[1:6:])

#start=1,stop=6,step=2
print(lst[1:6:2]) #[20,40,60]
#stop=6,step=2,start采用默认
print(lst[:6:2]) #默认为1   [10,30,50]
#start=1,step=2,stop采用默认
print(lst[1::2])

print('------------------step步长为负数的情况--------------')
print('原列表',lst)    #原列表 [10, 20, 30, 40, 50, 60, 70, 80]
print(lst[::-1])    #[80, 70, 60, 50, 40, 30, 20, 10]

#start=7,stop  step=-1
print(lst[7::-1 ])  #[80, 70, 60, 50, 40, 30, 20, 10]
#start=6,stop=0,step=-2
print(lst[6:0:-2])  #[70, 50, 30]

列表元素查询
在这里插入图片描述

print('p' in 'python')
print('k' not in 'python')

lst=[10,20,'python','hello']
print(10 in lst) #True
print(100 in lst)#False
print(20 not in lst) #False

print('--------------list元素遍历--------------')
for item in lst:
     print(item)

4.5 列表元素的增加

在这里插入图片描述

#列表元素的增加操作
lst=['hello','world',20]
print(lst)

#在列表的末尾添加一个元素
lst.append('新元素')
print(lst)  #['hello
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值