python3练习

1.乘法表

# -*- coding:utf-8 -*-
for x in range(1,10):
    for y in range(1,x+1):
        print(f"{y}x{x}".ljust(6),end='')
    print("")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

for x in range(1,10):  
    for y in range(1,x+1):
        print(f"{y}x{x}={x*y}".ljust(6),end=' ')
    print("")
#x 是乘数
#y 是被乘数
#使用新特性格式化字符串,也可以使
用 format,%等格式化,其中 ljust(6)左对齐,长度为 6,右补空格
print("") #打印一个换行

2.

i = sum = 0
while i <= 4:
    sum += i
    print(i)
    print("=========")
    i = i+1
print(sum)
i = 0 sum = 0
i = 1 sum = 1
i = 2 sum = 3
i = 3 sum = 6
i = 4 sum = 10

3.

and 并且,与
or 或
not 非
逻辑运算的运算顺序:()> not > and > or

4.

#1.避免转义

s = r"This is a rather long string containing\n\ several lines of text much as you would do in C."
print(s)

#2.字符串的格式化

print("我叫%s今年%d!" % ('小明',10)) #使用%
print("我叫{}今年{}岁!".format('小明',10)) #使用字符串的format方法
print("我叫{0}今年{1}岁".format('小明', 10,20))#使用索引,整数20未用到

5 while与for循环

###while循环

# -*- coding:utf-8 -
flag=True
while flag:
    input_str=input("please input something,'q' for quit.-> ")
    print("your input is %s" % input_str)
    if input_str=='q':
        flag=False
print("You're out of circulation.")

sum=0
for i in range(1000):
    sum+=i
print(sum)

PS E:\py-automation> & C:/Python37/python.exe e:/py-automation/test.py
please input something,'q' for quit.-> wegwe
please input something,'q' for quit.-> 123123
your input is 123123
please input something,'q' for quit.-> 123$ff.343243343242342
your input is 123$ff.343243343242342
your input is q
You're out of circulation.

###for循环

sum=0
for i in range(1000):
    sum+=i
print(sum)
PS E:\py-automation> & C:/Python37/python.exe e:/py-automation/test.py
499500

6.循环中break与continue语句

# -*- coding:utf-8 -
print("break--------------")
count=0
while count<5: 
    print("aaa",count)
    count+=1
    if count==2:
        break
    print("bbb",count) 

print("continue--------------") 
count=0 
while count<5: 
    print("aaa",count) 
    count+=1 
    if count==2: 
        continue 
    print("bbb",count)

#输出结果

PS E:\py-automation> & C:/Python37/python.exe e:/py-automation/test.py
break--------------
aaa 0
bbb 1
aaa 1
continue--------------
aaa 0
bbb 1
aaa 1
aaa 2
bbb 3
aaa 3
bbb 4
aaa 4
bbb 5

https://zh.wikipedia.org/wiki/Python#%E6%AD%B7%E5%8F%B2
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值