python基础操作

1.打印操作

print('2222')

2。接收用户输入

name=input('name')

3.if else判断

name='qiao'
name2='师弟'
username=input('输入名字')
if username==name:
print('hello'+name)
elif username==name2:
print('hello'+name2)
else:
print('错误')
4.多条件判断,用and or 连接
sex=input('性别:')
age=int(input('年龄:'))
if sex=='男' and age<=18:
print("肉")
elif sex=='女' and age<=18:
print('花')
else:
print('老')

sex=input('性别:')
if sex=='男' or sex=='女':
print('正常')
else:
print('不正常')

5.两种除法的运算

print(1/2)#除法
print(1//2)#整除取整

6.格式化字符串
#字符串格式化
name=input('shuru')
age=18
chengji=90.1234
print('hello'+name)#加号连接
print('hello',name)#逗号链接
print('hello%s'%name)
print('hello%s,your age is%s'%(name,age))
print('成绩是%d--%f---%.2f'%(chengji,chengji,chengji))
#占位符链接,后面跟变量
#%s跟变量,%d后面跟整数,%f后面跟小数,%.2f后面跟几位小数
print('hello:{your_name},age:{your_age}'.format(your_name=name,your_age=age))
# 可以是有大括号和。format进行连接

7.while循环
count=int(input('从几开始:'))
while count<10:
print('循环次数'+str(count))
if count==5:
break#循环里碰到break立即退出循环
count=count+1
else:#循环也可以写else
print('循环完了')

8.加else的while循环
count=int(input('从几开始:'))
while count<10:
if count==5:
continue#循环里碰到continue停止本次循环
print('循环次数%d次'%count)
count=count+1
else:#循环也可以写else
print('循环完了')

9.for循环
for i in range(10):
print('循环%d'%i)
if i==7:
break

for i in range(10):
if i==7:
continue#跳过本次循环
print('循环%d' % i)
break表示跳出循环
continue表示跳出本次循环,继续下次循环,本次循环的其他内容不执行了

10.加else的for循环
for i in range(2,10):
if i==7:
continue
print('循环%d' % i)
else:#for循环正常结束时,走else
print('over')

python读写文件相关内容

 

1。python读取文件

f = open('E:/info.txt', 'r')#用read的方式打开
a = 0
for line in f.readlines():读取没一行,就是读取所有文件的意思
getstr = line.split()#将一整行文件进行切割,切割后可以下标找到对应位置
if getstr[0] == user_name:#通过下表找到对应位置的信息并进行比较等操作。
print('用户名存在,请重新输入')

f.close()#关闭对应文件

2.python写文件,
f = open('E:/info.txt', 'a')#以append的方式,增加写文件,如果参数不是a,而是w,则表示每次都重写。
f.write('%s %s'%(user_name,password)+' ')#将内容写道对应的文件里。
print("成功")
f.close()#关闭文件
原文地址:https://www.cnblogs.com/xiaoshidi/p/6864553.html