python常用语句

条件语句
1.顺序结构
按照python的语句的顺序依次输出

s='hello,python!'
print(s)
hello,python!

2.if条件语句
if条件语句的结构和函数定义相同;一个语句后面跟着一个缩进的语句体,语句数目没有要求,保证有一条语句,当需求存在没有语句的时候,在这种情况下可以使用return0,

x=6
if x% 2 == 0:
    print('x为偶数')
else:
    print('x为基数')
    x为偶数

def LeapYear(year):
    if year % 4 == 0 and year % 100!= 0:
        print("是润年")
    elif year % 400 == 0:
        print("大闰年")
    else:
        print("平年")
LeapYear(2004)
LeapYear(1111)
LeapYear(8888)

是润年
平年
是润年

条件嵌套
eg:判断a和b的大小

a=5
b=7
if a<b:
    print('a小于b')
else:
    if a>b:
        print('a大于b')
    else:
        print('a等于b')

a小于b

循环语句
while循环

a=0
while a < 10:
    print(a,end='')
    a=a+1
    0123456789

eg:

x=1
while x==1:
    y=input("输入你内心的数")
    print("你输入的数字是:",y)
输入你内心的数1314
你输入的数字是: 1314

eg:

count=4
while count <6:
    print(count,"小于6")
    count =count+1
else:
    print(count,"大于等于6")
    4 小于6
5 小于6
6 大于等于6

for循环
在for循环中,循环的条件是循环条件中是否存在元素,存在时,判断为真,当当中没有元素时,会跳出循环
eg:

for i in range(10):
    if i % 2 ==1:
        print('奇数',i)
奇数 1
奇数 3
奇数 5
奇数 7
奇数 9

嵌套循环
1.while循环嵌套while循环
while判断条件1:
while判断条件2:
python循环2
python循环1
2.for循环嵌套for循环
for 变量1 in 序列 1:
for 变量2 in 序列2:
python语句2
python语句1

3 while循环嵌套for循环
while判断条件:
for 变量 in 序列 :
python语句2
python语句1

4 for循环嵌套while循环
for 变量 in 序列 :
while判断条件:
python语句2
python语句1

for i in range(1,10):
    for j in range(i,i+1):
        print("%d*%d=%2d"%(j,i,j*i),end='')
    print("")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值