To learn programming, practice is indispensable, and look at the results brought by the python interpreter, because there is a certain gap between the ideal and the display. These are the questions that I need to operate when I get started. Share with you, hoping to help you.
Introductory programming, the personal method of writing a small editor:
1. First, there is the logic to solve the needs, and the problems are reduced and solved one by one.
2. Pay attention to the indentation. Python is a weak language.
3. Check whether you have typed a wrong word.
Let’s take a look at the exercises:
1. Print the pattern
Use loops to complete pattern printing
'''
逻辑
先打出一个*
然后找到规律,实现循环
*
**
***
****
*****
******
'''
num=1#设置开头的第一个
while num <=5:#实现行列出5排
print("*"*num)#输出*
num+=1#设置跳出循环条件
print("~*"*10)#标记方便观看
'''
* 1
*** 3
***** 5
******* 7
********* 9
找到规律,实现逻辑
'''
#方法一
x=1
while x<10:
print("*"*x)
x+=2
print("~*"*10)#标记方便观看
#方法二
x1=[1,3,5,7,9]
for i in x1:
print("*"*i)
print("~*"*10)#标记方便观看
'''
********* 9
******* 7
***** 5
*** 3
* 1
'''
x2=9
while 1<=x2:
print("*"*x2)
x2-=2
print("~*"*10)#标记方便观看
#方法二就是把题二列表进行修改
'''
* 1
*** 3
***** 5
******* 7
********* 9
******* 7
***** 5
*** 3
* 1
'''
#方法二
l1=[1,3,5,7,9]
for j in l1:
print('*'*j)
l2=[9,7,5,3,1]
for j in l2:
print('*'*j)
'''
*
* *
* *
* *
* *
* *
* *
* *
*
'''
x=range(1,10)
for i in x:
if i!=1 and i!=9:
print("*"*2)
else:
print("*")
'''
*
***
* * *
* * *
*******
* * *
* * *
***
*
'''
x=range(1,10)
for i in x:
if i==1 or i==9:
print("*")
elif i==2 or i==8:
print("*"*3)
elif i==3 or i==4 or i==6 or i==7:
print("* * *")
else:
print("*"*7)
'''
接受用户输入的两个整数,存储到两个变量里面,交换变量存储的值。
1.临时变量
2.求和法
3.异或法
4.python自身提供的方式
至少使用两种方式
'''
#1.临时变量
pc1=int(input("输入整数:"))
pc2=int(input("输入整数:"))
#x=pc1
#pc1=pc2
#pc2=x
#print(pc1)
#print(pc2)
#2.求和法
#x=pc1+pc2
#pc2=pc1
#pc1=x-pc2
#print(pc1)
#print(pc2)
#4.python自身提供的方式
pc1,pc2=pc2,pc1
Determine whether this number is prime
'''
输入数,判断这个数是否是质数
质数:只能被1和自己整除
二是否为4倍数
'''
i=int(input("请输入一个数:"))
j = 2
while j <= i / j :
if not i % j or i%7==0:
break
j = j + 1
if j > i / j: # 证明上面没有break出来
print(i, " 是素数")
、
Positive order 99 multiplication table
#控制行的数量,从第一行往下进行打印
for m in range(1,10):
#控制列的数量,列的数量取决于行的数量(比如第一行输出一列,第二行输出两列)
for n in range(1,m+1):
#end=""阻止输出一个式子就换行,直到n循环结束才换行
print('%s×%s=%s'%(m,n,m*n),end=' ')
#消除阻止换行的end=''
outer=1#99乘法表从1开始
while outer <10:#定义一个外循环循环9次
inner=1#定义从1开始进行计算,每次都从1开始
while inner <outer +1:
print("%d*%d=%d "%(outer ,inner ,outer *inner ),end='')
inner +=1
print("\n")
outer+=1
Reverse 99 multiplication table:
x=y=1
while x<10:
y = x
while y<10:
print("%d*%d=%d" % (y, x, x * y), end=" ")
y+=1
x+=1
Number of daffodils:
'''
水仙花数:数独立个数的次方相加是自己本身
'''
for i in range(100,1000):
x=int(i/100)%10#百位
y=int(i/10)%10#十位
z=i%10#个位
if i==x**3+y**3+z**3:
print(i)