python基礎編——2(if文)
19083 ワード
一、注釈
説明説明単行コメント:#複数行コメント:「*3,'*3
二、分岐文
if文
1、計算-13//2=-7 or(1/0)>0の結果?True 2、5桁の数字を入力して、それが回文数かどうかを判断します(回文数は12321 56265...)
3、プログラムを設計して、所与の文字列の中のアルファベットの個数を計算してください.
注意:文字列の中のアルファベット4を計算して、学生の成績システムをして、もし成績が80点より大きいならば、出力は優秀で、もし成績が70点より大きくて80点未満ならば、出力は良好で、もし成績が60点より大きくて70点未満ならば、出力は合格して、60点より小さくて出力は不合格です.
5、ある年、ある月に何日あるかを印刷します.
calendar関数の導入:import calendar
print(calendar.calendar(2035))
2035のカレンダー6を出力し、文字を入力し、アルファベットか、数字か、その他かを判断する
7、アルファベットを入力し、大文字と小文字を変換する
8、math関数の導入
9、turtle関数のように——亀ちゃん
lLoveを描く
説明説明単行コメント:#複数行コメント:「*3,'*3
二、分岐文
if文
if |bool :
1
elif |bool
else:
2
1、計算-13//2=-7 or(1/0)>0の結果?True 2、5桁の数字を入力して、それが回文数かどうかを判断します(回文数は12321 56265...)
a = int(input(" :"))
a1 = a//10000
a2 = a//1000%10
a4 = a//10%10
a5 = a%10
if a1==a5 and a2==a4:
print(" ")
else:
print(" ")
3、プログラムを設計して、所与の文字列の中のアルファベットの個数を計算してください.
s = "a1b2c3"
count = 0
for a in s:
if a.isalpha():
count += 1
print(count)
注意:文字列の中のアルファベット4を計算して、学生の成績システムをして、もし成績が80点より大きいならば、出力は優秀で、もし成績が70点より大きくて80点未満ならば、出力は良好で、もし成績が60点より大きくて70点未満ならば、出力は合格して、60点より小さくて出力は不合格です.
score = int(input(" :"))
if 100>=score>=0 :
if score>=80:
print(" ")
elif score>=70:
print(" ")
elif score>=60:
print(" ")
else:
print(" ")
else:
print(" ")
5、ある年、ある月に何日あるかを印刷します.
import calendar
year = int(input(" :"))
month = int(input(" :"))
if month ==2:
#
if calendar.isleap(year):
print(29)
else:
print(28)
else:
if month in (1,3,5,7,8,10,12):
print(31)
else:
print(30)
calendar関数の導入:import calendar
print(calendar.calendar(2035))
2035のカレンダー6を出力し、文字を入力し、アルファベットか、数字か、その他かを判断する
a = input(" :")
if a.isalpha() :
print(" ")
elif a.isdigit() :
print(" ")
else:
print(" ")
7、アルファベットを入力し、大文字と小文字を変換する
a = input(" :")
if a.islower():
print(a.upper())
elif a.isupper():
print(a.lower())
else :
print(" ")
8、math関数の導入
import math
print(math.sqrt(9)) #
print(math.sin(math.pi/6))
print(math.pow(5,3))
print(math.ceil(5.1))
print(math.ceil(-5.1))
print(math.exp(10))
print(math.floor(5.1))
print(math.floor(-5.1))
max()
min()
print(abs(-3))
type()
9、turtle関数のように——亀ちゃん
lLoveを描く
import turtle
turtle.speed(1)
turtle.width(3)
turtle.pencolor('blue')
turtle.up()
turtle.goto(-300,200)
turtle.down()
turtle.goto(-320,-100)
turtle.goto(-220,-100)
turtle.up()
turtle.goto(-140,-100)
turtle.down()
turtle.circle(70)
turtle.up()
turtle.goto(-30,30)
turtle.down()
turtle.goto(30,-100)
turtle.goto(100,30)
turtle.up()
turtle.goto(130,-30)
turtle.down()
turtle.goto(250,-30)
turtle.left(90)
turtle.circle(60,325)