Python基本语法

块注释

"""
内容
"""

在内存中删除变量用del关键字

输出(Print)

# 可以输出数字
print(520)
# 可以输出字符串
print("Hello")
# 可以输出运算符的表达式
print(1+2)

# 数据输出到文件当中,注意点1.所指定的盘符需要存在, 2.使用file = fp
fp = open('D:/Test/PythonTest/01/test.txt','a+')#a+ 如果文件不存在就创建,存在就在该文件操作
print('hello',file=fp)
fp.close()

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

转义字符

  • 反斜杠+想实现转义功能的首字母
  • \n n–>newline表示换行
  • \t 一次占用四个字符,表示空格
  • \r 表示回车
  • \b 退一个格,比如hello\bworld输出hellworld,o被退格

输入(input)

#如果接收到的数值要进行比较大的时候,一定要转换为其他的类型
age = int(input('age:'))
#格式化输出
name = 'baishao'
age = 11
print('%s的年龄是%d'%(name,age))

baishao的年龄是11

#f:浮点型
#%.xf(保留小数点后几位)
print('%s的工资为%.8f' %(name,money))

baishao的工资为60000.00000000

#整数的占位不够的位数前面补充
sid = 1
name =  'HHK'
print('%s的学号是%.5d' %(name,sid))

逻辑判断

#注意:代码的缩进为一个tab键,或者四个空格

#在python开发中缩进和空格不能混合使用

if语句
age = 18
if age>18:
    print("ok")
else:
    print("no")

and:

条件1and条件2

两个条件都满足,就返回True

只要有一个条件不满足,就返回False

or:

条件1or条件2

两个条件其中一个满足,返回True

两个都不满足返回False

elif == else if

Socre = 80
if 90 < Socre <= 100:
    grand = 'A'
elif 80 < Socre <= 90:
    grand = B''
else:
    grand = 'C'

print(grand)

闰年判断

year = int(input("Year:"))
if (year % 4 == 0 and year % 100 != 0)or(year % 400 == 0):
    print("%d是闰年"%(year))
else:
    print("%d不是闰年"%(year))
for语句
range(2,11,2) #0-10的偶数
range(1,11,2) #0-10的基数

range(start(0),stop-1,step) step:步长
range(): 是内置函数

#求1~100之和
sum = 0
for i in range(1,101):
    sum += i;
print(sum)

#用户输入数字求阶乘
num = int(input("输入_"))
res = 1
for i in range(1,num+1):
    res *= i;
print("%d result is %d" %(num,res))
"""for 变量 in range(10):	需要执行的代码else:全部循环结束后执行的代码""""""用户登录程序1.输入用户名和密码2.判断用户名和密码是否正确('name = root',password = 'hh')3.为了防止暴力破解,登录次数仅有三次,如果超过三次就会报错"""for i in range(3):    name = input('name:')    psw = input('password:')    if name == 'root' and psw == 'hh':        print("yes")    else:        print("no,you have %d" %(2-i))else:    print('loser')    #break:跳出整个循环#contiune:跳出本次循环,但是循环依然继续#exit():结束整个程序的运行
while语句

9*9乘法表

row = 1while row <= 9:    col =1    while col <= row:        print("%d * %d = %d\t" %(row,col,row*col),end=" ")        col += 1    print(" ")#此作用类似与换行    row += 1

字符串

#索引:0 1 2 3 4(索引是从0开始的)s = "hello"print(s[0])# 打印hprint(s[-1])# 拿出最后一个字符 o#切片s[start:stop:step] 从start开始 都end -1 结束print(s[0:3])print(s[0:4:2])print(s[:])#显示所有字符print(s[:3])#显示前三个字符print(s[::-1])#反转字符串print(s[1:])#除了第一个字符之前的其他字符#重复print(s*10)#连接print('hello'+'python')#成员操作符print('he' in s)print('he' not in s)
hohelhlhellohelollehellohellohellohellohellohellohellohellohellohellohellohellopythonTrueFalse

‘字符串’.istitle()

‘字符串’.isuper() #是否是全部大写

‘字符串’.islower()#是否是全部小写

‘字符串’.upper()#替换为大写

‘字符串’.lower()#替换为小写

‘字符串’.endswith(’ ')#判断后面的字符串是否正确

‘字符串’.startswith(’ ')#判断前面的字符串是否正确

#注意,广义的空格包括\t \n

‘字符串’.lstrip()#去除左边的空格

‘字符串’.rstrip()#去除右边的空格

‘字符串’.strip()#去除所有的空格

#只有有一个元素不满足就是false

‘字符串’.isdigit()#判断是否为数字

‘字符串’.isalpha()#判断是否为字符

‘字符串’.isalum()#可以包含数字和字符

#字符串的对其

print("诗竹白芍".center(30))print("诗竹白芍".center(30,'*'))print("诗竹白芍".ljust(30,'%'))print("诗竹白芍".rjust(30,'%'))
诗竹白芍             *************诗竹白芍*************诗竹白芍%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%诗竹白芍

#字符串的搜索和替换

s = "hello hhk hhello"print(s.find('hhk'))#替换print(s.replace("hello","hhk"))#统计print('hello'.count('l'))#统计有多少个对应字符串print(len(s))#打印长度6hhk hhk hhhk216

#分离和连接

#分离s = "190.287.276.89"s1 = s.split('.')#相当于将其重新组成一个列表print(s1)print(s1[::-1])#连接print("#".join(s))['190', '287', '276', '89']['89', '276', '287', '190']1#9#0#.#2#8#7#.#2#7#6#.#8#9

秋招题目:

要求将hello xiao mi -> mi xiao hello

print(" ".join(input().split()[::-1]))