python3基礎之“小練習(1)”



(一)打印3個不同的字符
1 # a=int("123")
2 # b="123"
3 # c=1.2
4 # print(type(a),a)
5 # print(type(b),b)
6 # print(type(c),c)

(二)編寫程序:變量小於10,打印一條消息,大於10,打印不同消息
1 # a=int(input("plase enter a number :"))
2 #
3 # if a<10 :
4 #     print("a<10")
5 # else:
6 #     print('a>10')

 

(三)編寫程序:如果小於10,打印一條消息;大於10或等於25,打印一條不同的消息;大於25,打印另一條不同的消息。
1 # a=int(input("please enter a number:"))
2 #
3 # if a<10:
4 #     print("a<10")
5 # elif a>10 and  a<=25:
6 #     print("a<25")
7 # else:
8 #     print('a>25')

 

(四)編寫一個將兩個變量相除,並打印余數
1 # a=int(input("please enter a number:"))
2 # b=int(input("plase enter a number:"))
3 # print(a%b)

 

(五)編寫一個將兩個變量相除,並打印商的程序
1 # a=int(input("please enter a number:"))
2 # b=int(input("plase enter a number:"))
3 # print(a/b)

 

(六)編寫程序:為變量age賦予一個整數值,根據不同的值打印不同的字符串說明
1 # age=int(input("please enter a number:"))
2 # if age<18:
3 #     print("you are so young!")
4 # elif age>18 and age<30:
5 #     print("you are old")
6 # else:
7 #     print("you are so old!")

 

(七)編寫一個函數,接受輸出,並返回該數字的平方
1 # x=int(input("enter a number:"))
2 # def f(x):
3 #     """
4 #     返回x**2的值
5 #     :param x:int
6 #     """
7 #     return x**2
8 # a=f(x)
9 # print(a)

 

(八)編寫以字符串為參數並將其打印的函數
1 # def even_odd(2):
2 #     if x%2==0:
3 #         print("a")
4 #     alse:
5 #         print("b")
6 # print(even_odd())

 

(九)編寫接受3個必選參數,兩個可選參數的函數。
1 # x=int(input("enter a number"))
2 # y=int(input("enter a number:"))
3 # z=int(input("enter a number:"))
4 #
5 # def d(x,y,z,s=1,w=2):
6 #     return x+y+z-s-w
7 # a=d(x,y,z,s=1,w=2)
8 # print(a)

(十)編寫一個帶兩個函數的程序。第一個函數應接受一個整數為參數,並返回該整數...
除以2 的值。第二個函數應接受一個整數作為參數,並返回該整數乘以4 的值。調用第...
一個函數,將結果保存至變量,並將變量作為參數傳遞給第二個函數
1 # x=int(input("type a number:"))
2 # y=int(input("type a number:"))
3 # def f(x,y):
4 #     return x%2,y*4
5 # bian_ling=x
6 # y=bian_ling
7 #
8 # print(x)
9 # print(y)

 

(十一)創建一個列表,其中包含3~30 內能被3 整除的數字;再使用一個for循環將這個列表中的數字都打印出來。
1 # num=[]
2 #
3 # for i in range(1,31):
4 #     if i%3==0:
5 #         num.append(i)
6 # print(num)

 

(十二)使用列表解析生成一個列表,其中包含前10 個整數的立方。
1 # num=[i**2 for i in range(1,11)]
2 # print(num)

 

如有錯誤,歡迎指正!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM